mirror of
https://github.com/krahets/LeetCode-Book.git
synced 2026-01-12 00:19:02 +08:00
First commit.
This commit is contained in:
190
README.md
Normal file
190
README.md
Normal file
@@ -0,0 +1,190 @@
|
||||
# 图解算法数据结构
|
||||
|
||||
[](https://github.com/krahets)[](https://github.com/krahets)
|
||||
|
||||
[]()[]()[]()
|
||||
|
||||
本 Repo 为 LeetBook《图解算法数据结构》配套代码仓库,便于读者 Debug 代码,已全部在 LeetCode 上测试通过。
|
||||
|
||||
[<img src="https://pic.leetcode-cn.com/1599187248-gDiWnC-image.png" width="500" style="zoom:75%;" />](https://leetcode-cn.com/leetbook/detail/illustration-of-algorithm/)
|
||||
|
||||
## 课程介绍
|
||||
|
||||
本 LeetBook 面向算法初学者、互联网求职者设计,帮助同学们从零至一地搭建算法知识体系。
|
||||
|
||||
### 剑指 Offer 图文题解
|
||||
|
||||
- 图文详解 75 道题目,覆盖主要算法知识点,适合作为学习算法的 **第一份题库** 。
|
||||
|
||||
- 题库活跃于各大公司的笔面试中,可使笔面试准备事半功倍。
|
||||
|
||||
- 作者致力于行文深入浅出、图文搭配,提供简洁的 Python3, Java, C++ 解题代码。
|
||||
|
||||
### 数据结构与算法专栏
|
||||
|
||||
- 基础知识: 时间复杂度、空间复杂度等算法知识。
|
||||
|
||||
- 数据结构: 数组、栈、队列、字符串、链表、树、图、堆、哈希表。
|
||||
|
||||
- 算法专题: 分治算法、动态规划、搜索与回溯算法、查找算法、贪心算法、排序、位运算、双指针、模拟、数学。
|
||||
|
||||
## 适合人群
|
||||
|
||||
- 互联网算法、软件岗位求职者。
|
||||
- 从零开始接触数据结构与算法的同学。
|
||||
- 具有一定编程基础,计划系统学习算法的同学。
|
||||
|
||||
---
|
||||
|
||||
## 刷题计划
|
||||
|
||||
笔者整理了本刷题计划,核心理念为从易到难、从基础类题目到综合类题目,供希望按照知识点类型顺序刷题的小伙伴们参考。行百里者半九十,坚持一个月刷完,一起加油!
|
||||
|
||||
| 日程 | 题目 |
|
||||
| :--------: | :------------------------------------------------ |
|
||||
| **Day 1** | **栈与队列(简单)** |
|
||||
| | [剑指 Offer 09. 用两个栈实现队列](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/5d3i87/) |
|
||||
| | [剑指 Offer 30. 包含 min 函数的栈](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/50bp33/) |
|
||||
| **Day 2** | **链表(简单)** |
|
||||
| | [剑指 Offer 06. 从尾到头打印链表](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/5dt66m/) |
|
||||
| | [剑指 Offer 24. 反转链表](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/9pdjbm/) |
|
||||
| | [剑指 Offer 35. 复杂链表的复制](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/9p0yy1/) |
|
||||
| **Day 3** | **字符串(简单)** |
|
||||
| | [剑指 Offer 05. 替换空格](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/50ywkd/) |
|
||||
| | [剑指 Offer 58 - II. 左旋转字符串](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/589fz2/) |
|
||||
| **Day 4** | **查找算法(简单)** |
|
||||
| | [剑指 Offer 03. 数组中重复的数字](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/59bjss/) |
|
||||
| | [剑指 Offer 53 - I. 在排序数组中查找数字 I](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/5874p1/) |
|
||||
| | [剑指 Offer 53 - II. 0~n-1 中缺失的数字](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/58iqo5/) |
|
||||
| **Day 5** | **查找算法(中等)** |
|
||||
| | [剑指 Offer 04. 二维数组中的查找](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/5v76yi/) |
|
||||
| | [剑指 Offer 11. 旋转数组的最小数字](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/50xofm/) |
|
||||
| | [剑指 Offer 50. 第一个只出现一次的字符](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/5viisg/) |
|
||||
| **Day 6** | **搜索与回溯算法(简单)** |
|
||||
| | [剑指 Offer 32 - I. 从上到下打印二叉树](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/9ackoe/) |
|
||||
| | [剑指 Offer 32 - II. 从上到下打印二叉树 II](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/5vawr3/) |
|
||||
| | [剑指 Offer 32 - III. 从上到下打印二叉树 III](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/5vnp91/) |
|
||||
| **Day 7** | **搜索与回溯算法(简单)** |
|
||||
| | [剑指 Offer 26. 树的子结构](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/5dshwe/) |
|
||||
| | [剑指 Offer 27. 二叉树的镜像](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/59zt5i/) |
|
||||
| | [剑指 Offer 28. 对称的二叉树](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/5d412v/) |
|
||||
| **Day 8** | **动态规划(简单)** |
|
||||
| | [剑指 Offer 10- I. 斐波那契数列](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/50fxu1/) |
|
||||
| | [剑指 Offer 10- II. 青蛙跳台阶问题](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/57hyl5/) |
|
||||
| | [剑指 Offer 63. 股票的最大利润](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/58nn7r/) |
|
||||
| **Day 9** | **动态规划(中等)** |
|
||||
| | [剑指 Offer 42. 连续子数组的最大和](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/59gq9c/) |
|
||||
| | [剑指 Offer 47. 礼物的最大价值](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/5vokvr/) |
|
||||
| **Day 10** | **动态规划(中等)** |
|
||||
| | [剑指 Offer 46. 把数字翻译成字符串](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/99wd55/) |
|
||||
| | [剑指 Offer 48. 最长不含重复字符的子字符串](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/5dgr0c/) |
|
||||
| **Day 11** | **双指针(简单)** |
|
||||
| | [剑指 Offer 18. 删除链表的节点](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/505fc7/) |
|
||||
| | [剑指 Offer 22. 链表中倒数第 k 个节点](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/58tl52/) |
|
||||
| **Day 12** | **双指针(简单)** |
|
||||
| | [剑指 Offer 25. 合并两个排序的链表](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/5vq98s/) |
|
||||
| | [剑指 Offer 52. 两个链表的第一个公共节点](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/oe5os3/) |
|
||||
| **Day 13** | **双指针(简单)** |
|
||||
| | [剑指 Offer 21. 调整数组顺序使奇数位于偶数前面](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/5v8a6t/) |
|
||||
| | [剑指 Offer 57. 和为 s 的两个数字](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/5832fi/) |
|
||||
| | [剑指 Offer 58 - I. 翻转单词顺序](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/586ecg/) |
|
||||
| **Day 14** | **搜索与回溯算法(中等)** |
|
||||
| | [剑指 Offer 12. 矩阵中的路径](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/58wowd/) |
|
||||
| | [剑指 Offer 13. 机器人的运动范围](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/9h6vo2/) |
|
||||
| **Day 15** | **搜索与回溯算法(中等)** |
|
||||
| | [剑指 Offer 34. 二叉树中和为某一值的路径](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/5dy6pt/) |
|
||||
| | [剑指 Offer 36. 二叉搜索树与双向链表](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/5dbies/) |
|
||||
| | [剑指 Offer 54. 二叉搜索树的第 k 大节点](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/58df23/) |
|
||||
| **Day 16** | **排序(简单)** |
|
||||
| | [剑指 Offer 45. 把数组排成最小的数](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/59ypcj/) |
|
||||
| | [剑指 Offer 61. 扑克牌中的顺子](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/57mpoj/) |
|
||||
| **Day 17** | **排序(中等)** |
|
||||
| | [剑指 Offer 40. 最小的 k 个数](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/ohvl0d/) |
|
||||
| | [剑指 Offer 41. 数据流中的中位数](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/5vd1j2/) |
|
||||
| **Day 18** | **搜索与回溯算法(中等)** |
|
||||
| | [剑指 Offer 55 - I. 二叉树的深度](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/9hgr5i/) |
|
||||
| | [剑指 Offer 55 - II. 平衡二叉树](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/9hzffg/) |
|
||||
| **Day 19** | **搜索与回溯算法(中等)** |
|
||||
| | [剑指 Offer 64. 求 1 + 2 + … + n](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/9h44cj/) |
|
||||
| | [剑指 Offer 68 - I. 二叉搜索树的最近公共祖先](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/575kd2/) |
|
||||
| | [剑指 Offer 68 - II. 二叉树的最近公共祖先](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/57euni/) |
|
||||
| **Day 20** | **分治算法(中等)** |
|
||||
| | [剑指 Offer 07. 重建二叉树](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/99lxci/) |
|
||||
| | [剑指 Offer 16. 数值的整数次方](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/57rwmg/) |
|
||||
| | [剑指 Offer 33. 二叉搜索树的后序遍历序列](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/5vwxx5/) |
|
||||
| **Day 21** | **位运算(简单)** |
|
||||
| | [剑指 Offer 15. 二进制中 1 的个数](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/5vk1l3/) |
|
||||
| | [剑指 Offer 65. 不用加减乘除做加法](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/5vz6d1/) |
|
||||
| **Day 22** | **位运算(中等)** |
|
||||
| | [剑指 Offer 56 - I. 数组中数字出现的次数](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/eubbnm/) |
|
||||
| | [剑指 Offer 56 - II. 数组中数字出现的次数 II](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/9hyq1r/) |
|
||||
| **Day 23** | **数学(简单)** |
|
||||
| | [剑指 Offer 39. 数组中出现次数超过一半的数字](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/99iy4g/) |
|
||||
| | [剑指 Offer 66. 构建乘积数组](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/57d8cm/) |
|
||||
| **Day 24** | **数学(中等)** |
|
||||
| | [剑指 Offer 14- I. 剪绳子](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/5v1026/) |
|
||||
| | [剑指 Offer 57 - II. 和为 s 的连续正数序列](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/eufzm7/) |
|
||||
| | [剑指 Offer 62. 圆圈中最后剩下的数字](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/oxrkot/) |
|
||||
| **Day 25** | **模拟(中等)** |
|
||||
| | [剑指 Offer 29. 顺时针打印矩阵](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/5vfh9g/) |
|
||||
| | [剑指 Offer 31. 栈的压入、弹出序列](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/5wh1hj/) |
|
||||
| **Day 26** | **字符串(中等)** |
|
||||
| | [剑指 Offer 20. 表示数值的字符串](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/5d6vi6/) |
|
||||
| | [剑指 Offer 67. 把字符串转换成整数](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/58pq8g/) |
|
||||
| **Day 27** | **栈与队列(困难)** |
|
||||
| | [剑指 Offer 59 - I. 滑动窗口的最大值](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/58o46i/) |
|
||||
| | [剑指 Offer 59 - II. 队列的最大值](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/e2t5ug/) |
|
||||
| **Day 28** | **搜索与回溯算法(困难)** |
|
||||
| | [剑指 Offer 37. 序列化二叉树](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/990pf2/) |
|
||||
| | [剑指 Offer 38. 字符串的排列](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/5dfv5h/) |
|
||||
| **Day 29** | **动态规划(困难)** |
|
||||
| | [剑指 Offer 19. 正则表达式匹配](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/9a1ypc/) |
|
||||
| | [剑指 Offer 49. 丑数](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/9h3im5/) |
|
||||
| | [剑指 Offer 60. n 个骰子的点数](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/ozzl1r/) |
|
||||
| **Day 30** | **分治算法(困难)** |
|
||||
| | [剑指 Offer 17. 打印从 1 到最大的 n 位数](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/594wfg/) |
|
||||
| | [剑指 Offer 51. 数组中的逆序对](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/o58jfs/) |
|
||||
| **Day 31** | **数学(困难)** |
|
||||
| | [剑指 Offer 14- II. 剪绳子 II](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/5vcapc/) |
|
||||
| | [剑指 Offer 43. 1~n 整数中 1 出现的次数](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/572jxs/) |
|
||||
| | [剑指 Offer 44. 数字序列中某一位的数字](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/57vzfh/) |
|
||||
|
||||
---
|
||||
|
||||
## 精选算法专栏
|
||||
|
||||
### [数据结构简介](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/50e446/)
|
||||
|
||||
> 建议对数据结构不熟悉的同学,先看这篇熟悉用法。
|
||||
|
||||
- 常用数据结构「分类」和「基本特点」;
|
||||
- 在算法中,数据结构的「常用操作」;
|
||||
- 在 Python3 , Java , C++ 语言中,各数据结构的初始化与构建方法。
|
||||
|
||||
### [算法复杂度](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/r84gmi/)
|
||||
|
||||
> 复杂度是算法优劣性的有力评价指标,对于理解算法起着至关重要的作用。
|
||||
|
||||
- 什么是时间复杂度、空间复杂度?
|
||||
- 「时间复杂度」和「空间复杂度」的概念定义、符号表示、常见种类、时空权衡。
|
||||
- 时间与空间复杂度的示例解析、示例题目。
|
||||
|
||||
### [动态规划](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/m5zfc1/)
|
||||
|
||||
> 动态规划是算法重难点,值得重点关注、理解与练习。
|
||||
|
||||
- 动态规划问题特点,动态规划和分治算法的联系与区别;
|
||||
- 借助例题介绍重叠子问题和最优子结构分别是什么,以及动态规划是如何解决它们的;
|
||||
- 动态规划的解题框架总结;
|
||||
- 动态规划的练习例题,从易到难排序;
|
||||
|
||||
### [排序算法简介](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/pxal47/)
|
||||
|
||||
> 排序是最经典的算法问题之一,由浅入深的多种算法涵盖多个算法知识点(例如暴力搜索、分治算法、堆数据结构等)。
|
||||
|
||||
- 排序算法分类方法,包括稳定性 、就地性 、自适应性;
|
||||
- 排序算法与二分查找、双指针算法之间的关系;
|
||||
- 各主要排序算法的时间复杂度与空间复杂度;
|
||||
|
||||
---
|
||||
|
||||
32
java/include/ListNode.java
Normal file
32
java/include/ListNode.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package include;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Definition for a singly-linked list node.
|
||||
*/
|
||||
public class ListNode {
|
||||
public int val;
|
||||
public ListNode next;
|
||||
|
||||
public ListNode(int x) {
|
||||
val = x;
|
||||
}
|
||||
|
||||
public static ListNode arrToLinkedList(int[] arr) {
|
||||
ListNode dum = new ListNode(0);
|
||||
ListNode head = dum;
|
||||
for (int val : arr) {
|
||||
head.next = new ListNode(val);
|
||||
head = head.next;
|
||||
}
|
||||
return dum.next;
|
||||
}
|
||||
|
||||
public static ListNode getListNode(ListNode head, int val) {
|
||||
while (head != null && head.val != val) {
|
||||
head = head.next;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
}
|
||||
28
java/include/PrintUtil.java
Normal file
28
java/include/PrintUtil.java
Normal file
@@ -0,0 +1,28 @@
|
||||
package include;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class PrintUtil {
|
||||
// Print a linked list
|
||||
public static void printLinkedList(ListNode head) {
|
||||
List<String> list = new ArrayList<>();
|
||||
while (head != null) {
|
||||
list.add(String.valueOf(head.val));
|
||||
head = head.next;
|
||||
}
|
||||
System.out.println(String.join(" -> ", list));
|
||||
}
|
||||
|
||||
// Print a binary tree (90º counter-clockwise rotated)
|
||||
public static void printTree(TreeNode root) {
|
||||
printTreeHelper(root, 0);
|
||||
}
|
||||
|
||||
private static void printTreeHelper(TreeNode root, int level) {
|
||||
if (root == null)
|
||||
return;
|
||||
printTreeHelper(root.right, level + 1);
|
||||
System.out.println(" ".repeat(4 * level) + "->" + root.val);
|
||||
printTreeHelper(root.left, level + 1);
|
||||
}
|
||||
}
|
||||
64
java/include/TreeNode.java
Normal file
64
java/include/TreeNode.java
Normal file
@@ -0,0 +1,64 @@
|
||||
package include;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
*/
|
||||
public class TreeNode {
|
||||
public int val;
|
||||
public TreeNode left;
|
||||
public TreeNode right;
|
||||
|
||||
public TreeNode(int x) {
|
||||
val = x;
|
||||
}
|
||||
|
||||
public static TreeNode arrToTree(Integer[] arr) {
|
||||
TreeNode root = new TreeNode(arr[0]);
|
||||
Queue<TreeNode> queue = new LinkedList<>() {{ add(root); }};
|
||||
int i = 1;
|
||||
while(!queue.isEmpty()) {
|
||||
TreeNode node = queue.poll();
|
||||
if(arr[i] != null) {
|
||||
node.left = new TreeNode(arr[i]);
|
||||
queue.add(node.left);
|
||||
}
|
||||
i++;
|
||||
if(arr[i] != null) {
|
||||
node.right = new TreeNode(arr[i]);
|
||||
queue.add(node.right);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
public static List<Integer> treeToList(TreeNode root) {
|
||||
List<Integer> list = new ArrayList<>();
|
||||
if(root == null) return list;
|
||||
Queue<TreeNode> queue = new LinkedList<>() {{ add(root); }};
|
||||
while(!queue.isEmpty()) {
|
||||
TreeNode node = queue.poll();
|
||||
if(node != null) {
|
||||
list.add(node.val);
|
||||
queue.add(node.left);
|
||||
queue.add(node.right);
|
||||
}
|
||||
else {
|
||||
list.add(null);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public static TreeNode getTreeNode(TreeNode root, int val) {
|
||||
if (root == null)
|
||||
return null;
|
||||
if (root.val == val)
|
||||
return root;
|
||||
TreeNode left = getTreeNode(root.left, val);
|
||||
TreeNode right = getTreeNode(root.right, val);
|
||||
return left != null ? left : right;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* File: sfo_03_find_duplicate_numbers_in_an_array_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_03_find_duplicate_numbers_in_an_array_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int findRepeatNumber(int[] nums) {
|
||||
Set<Integer> dic = new HashSet<>();
|
||||
for(int num : nums) {
|
||||
if(dic.contains(num)) return num;
|
||||
dic.add(num);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_03_find_duplicate_numbers_in_an_array_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int[] nums = { 2, 3, 1, 0, 2, 5, 3 };
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int res = slt.findRepeatNumber(nums);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* File: sfo_03_find_duplicate_numbers_in_an_array_s2.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_03_find_duplicate_numbers_in_an_array_s2;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int findRepeatNumber(int[] nums) {
|
||||
int i = 0;
|
||||
while(i < nums.length) {
|
||||
if(nums[i] == i) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
if(nums[nums[i]] == nums[i]) return nums[i];
|
||||
int tmp = nums[i];
|
||||
nums[i] = nums[tmp];
|
||||
nums[tmp] = tmp;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_03_find_duplicate_numbers_in_an_array_s2 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int[] nums = { 2, 3, 1, 0, 2, 5, 3 };
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int res = slt.findRepeatNumber(nums);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* File: sfo_04_find_a_number_in_2d_matrix_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_04_find_a_number_in_2d_matrix_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public boolean findNumberIn2DArray(int[][] matrix, int target) {
|
||||
int i = matrix.length - 1, j = 0;
|
||||
while(i >= 0 && j < matrix[0].length)
|
||||
{
|
||||
if(matrix[i][j] > target) i--;
|
||||
else if(matrix[i][j] < target) j++;
|
||||
else return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_04_find_a_number_in_2d_matrix_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int[][] matrix = {
|
||||
{ 1, 4, 7, 11, 15 },
|
||||
{ 2, 5, 8, 12, 19 },
|
||||
{ 3, 6, 9, 16, 22 },
|
||||
{ 10, 13, 14, 17, 24 },
|
||||
{ 18, 21, 23, 26, 30 }
|
||||
};
|
||||
int target = 5;
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
boolean res = slt.findNumberIn2DArray(matrix, target);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
34
java/sfo_05_replace_spaces_s1/sfo_05_replace_spaces_s1.java
Normal file
34
java/sfo_05_replace_spaces_s1/sfo_05_replace_spaces_s1.java
Normal file
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* File: sfo_05_replace_spaces_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_05_replace_spaces_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public String replaceSpace(String s) {
|
||||
StringBuilder res = new StringBuilder();
|
||||
for(Character c : s.toCharArray())
|
||||
{
|
||||
if(c == ' ') res.append("%20");
|
||||
else res.append(c);
|
||||
}
|
||||
return res.toString();
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_05_replace_spaces_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
String s = "We are happy.";
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
String res = slt.replaceSpace(s);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* File: sfo_06_print_a_linked_list_in_reverse_order_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_06_print_a_linked_list_in_reverse_order_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
ArrayList<Integer> tmp = new ArrayList<Integer>();
|
||||
public int[] reversePrint(ListNode head) {
|
||||
recur(head);
|
||||
int[] res = new int[tmp.size()];
|
||||
for(int i = 0; i < res.length; i++)
|
||||
res[i] = tmp.get(i);
|
||||
return res;
|
||||
}
|
||||
void recur(ListNode head) {
|
||||
if(head == null) return;
|
||||
recur(head.next);
|
||||
tmp.add(head.val);
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_06_print_a_linked_list_in_reverse_order_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
ListNode head = ListNode.arrToLinkedList(new int[] { 1, 3, 2 });
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int[] res = slt.reversePrint(head);
|
||||
System.out.println(Arrays.toString(res));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* File: sfo_06_print_a_linked_list_in_reverse_order_s2.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_06_print_a_linked_list_in_reverse_order_s2;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int[] reversePrint(ListNode head) {
|
||||
LinkedList<Integer> stack = new LinkedList<Integer>();
|
||||
while(head != null) {
|
||||
stack.addLast(head.val);
|
||||
head = head.next;
|
||||
}
|
||||
int[] res = new int[stack.size()];
|
||||
for(int i = 0; i < res.length; i++)
|
||||
res[i] = stack.removeLast();
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_06_print_a_linked_list_in_reverse_order_s2 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
ListNode head = ListNode.arrToLinkedList(new int[] { 1, 3, 2 });
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int[] res = slt.reversePrint(head);
|
||||
System.out.println(Arrays.toString(res));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* File: sfo_07_reconstruct_binary_tree_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_07_reconstruct_binary_tree_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
int[] preorder;
|
||||
HashMap<Integer, Integer> dic = new HashMap<>();
|
||||
public TreeNode buildTree(int[] preorder, int[] inorder) {
|
||||
this.preorder = preorder;
|
||||
for(int i = 0; i < inorder.length; i++)
|
||||
dic.put(inorder[i], i);
|
||||
return recur(0, 0, inorder.length - 1);
|
||||
}
|
||||
TreeNode recur(int root, int left, int right) {
|
||||
if(left > right) return null; // 递归终止
|
||||
TreeNode node = new TreeNode(preorder[root]); // 建立根节点
|
||||
int i = dic.get(preorder[root]); // 划分根节点、左子树、右子树
|
||||
node.left = recur(root + 1, left, i - 1); // 开启左子树递归
|
||||
node.right = recur(root + i - left + 1, i + 1, right); // 开启右子树递归
|
||||
return node; // 回溯返回根节点
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_07_reconstruct_binary_tree_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int[] preorder = { 3, 9, 20, 15, 7 };
|
||||
int[] inorder = { 9, 3, 15, 20, 7 };
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
TreeNode res = slt.buildTree(preorder, inorder);
|
||||
PrintUtil.printTree(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* File: sfo_09_implement_a_queue_using_two_stacks_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_09_implement_a_queue_using_two_stacks_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class CQueue {
|
||||
LinkedList<Integer> A, B;
|
||||
public CQueue() {
|
||||
A = new LinkedList<Integer>();
|
||||
B = new LinkedList<Integer>();
|
||||
}
|
||||
public void appendTail(int value) {
|
||||
A.addLast(value);
|
||||
}
|
||||
public int deleteHead() {
|
||||
if(!B.isEmpty()) return B.removeLast();
|
||||
if(A.isEmpty()) return -1;
|
||||
while(!A.isEmpty())
|
||||
B.addLast(A.removeLast());
|
||||
return B.removeLast();
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_09_implement_a_queue_using_two_stacks_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ====== Driver Code ======
|
||||
List<Integer> res = new ArrayList<>();
|
||||
CQueue cQueue = new CQueue();
|
||||
res.add(cQueue.deleteHead());
|
||||
cQueue.appendTail(5);
|
||||
cQueue.appendTail(2);
|
||||
res.add(cQueue.deleteHead());
|
||||
res.add(cQueue.deleteHead());
|
||||
System.out.println(Arrays.toString(res.toArray()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* File: sfo_10i_fibonacci_numbers_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_10i_fibonacci_numbers_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int fib(int n) {
|
||||
int a = 0, b = 1, sum;
|
||||
for(int i = 0; i < n; i++){
|
||||
sum = (a + b) % 1000000007;
|
||||
a = b;
|
||||
b = sum;
|
||||
}
|
||||
return a;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_10i_fibonacci_numbers_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int n = 5;
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int res = slt.fib(n);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
34
java/sfo_10ii_frog_jump_s1/sfo_10ii_frog_jump_s1.java
Normal file
34
java/sfo_10ii_frog_jump_s1/sfo_10ii_frog_jump_s1.java
Normal file
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* File: sfo_10ii_frog_jump_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_10ii_frog_jump_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int numWays(int n) {
|
||||
int a = 1, b = 1, sum;
|
||||
for(int i = 0; i < n; i++){
|
||||
sum = (a + b) % 1000000007;
|
||||
a = b;
|
||||
b = sum;
|
||||
}
|
||||
return a;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_10ii_frog_jump_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int n = 7;
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int res = slt.numWays(n);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* File: sfo_11_find_minimum_in_rotated_sorted_array_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_11_find_minimum_in_rotated_sorted_array_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int minArray(int[] numbers) {
|
||||
int i = 0, j = numbers.length - 1;
|
||||
while (i < j) {
|
||||
int m = (i + j) / 2;
|
||||
if (numbers[m] > numbers[j]) i = m + 1;
|
||||
else if (numbers[m] < numbers[j]) j = m;
|
||||
else j--;
|
||||
}
|
||||
return numbers[i];
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_11_find_minimum_in_rotated_sorted_array_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int[] numbers = { 3, 4, 5, 1, 2 };
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int res = slt.minArray(numbers);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* File: sfo_11_find_minimum_in_rotated_sorted_array_s2.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_11_find_minimum_in_rotated_sorted_array_s2;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int minArray(int[] numbers) {
|
||||
int i = 0, j = numbers.length - 1;
|
||||
while (i < j) {
|
||||
int m = (i + j) / 2;
|
||||
if (numbers[m] > numbers[j]) i = m + 1;
|
||||
else if (numbers[m] < numbers[j]) j = m;
|
||||
else {
|
||||
int x = i;
|
||||
for(int k = i + 1; k < j; k++) {
|
||||
if(numbers[k] < numbers[x]) x = k;
|
||||
}
|
||||
return numbers[x];
|
||||
}
|
||||
}
|
||||
return numbers[i];
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_11_find_minimum_in_rotated_sorted_array_s2 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int[] numbers = { 3, 4, 5, 1, 2 };
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int res = slt.minArray(numbers);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
48
java/sfo_12_word_search_s1/sfo_12_word_search_s1.java
Normal file
48
java/sfo_12_word_search_s1/sfo_12_word_search_s1.java
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* File: sfo_12_word_search_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_12_word_search_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public boolean exist(char[][] board, String word) {
|
||||
char[] words = word.toCharArray();
|
||||
for(int i = 0; i < board.length; i++) {
|
||||
for(int j = 0; j < board[0].length; j++) {
|
||||
if(dfs(board, words, i, j, 0)) return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
boolean dfs(char[][] board, char[] word, int i, int j, int k) {
|
||||
if(i >= board.length || i < 0 || j >= board[0].length || j < 0 || board[i][j] != word[k]) return false;
|
||||
if(k == word.length - 1) return true;
|
||||
board[i][j] = '\0';
|
||||
boolean res = dfs(board, word, i + 1, j, k + 1) || dfs(board, word, i - 1, j, k + 1) ||
|
||||
dfs(board, word, i, j + 1, k + 1) || dfs(board, word, i , j - 1, k + 1);
|
||||
board[i][j] = word[k];
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_12_word_search_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
char[][] board = {
|
||||
{ 'A', 'B', 'C', 'E' },
|
||||
{ 'S', 'F', 'C', 'S' },
|
||||
{ 'A', 'D', 'E', 'E' }
|
||||
};
|
||||
String word = "ABCCED";
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
boolean res = slt.exist(board, word);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* File: sfo_13_range_of_motion_of_a_robot_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_13_range_of_motion_of_a_robot_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
int m, n, k;
|
||||
boolean[][] visited;
|
||||
public int movingCount(int m, int n, int k) {
|
||||
this.m = m; this.n = n; this.k = k;
|
||||
this.visited = new boolean[m][n];
|
||||
return dfs(0, 0, 0, 0);
|
||||
}
|
||||
public int dfs(int i, int j, int si, int sj) {
|
||||
if(i >= m || j >= n || k < si + sj || visited[i][j]) return 0;
|
||||
visited[i][j] = true;
|
||||
return 1 + dfs(i + 1, j, (i + 1) % 10 != 0 ? si + 1 : si - 8, sj) + dfs(i, j + 1, si, (j + 1) % 10 != 0 ? sj + 1 : sj - 8);
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_13_range_of_motion_of_a_robot_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int m = 2;
|
||||
int n = 3;
|
||||
int k = 1;
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int res = slt.movingCount(m, n, k);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* File: sfo_13_range_of_motion_of_a_robot_s2.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_13_range_of_motion_of_a_robot_s2;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int movingCount(int m, int n, int k) {
|
||||
boolean[][] visited = new boolean[m][n];
|
||||
int res = 0;
|
||||
Queue<int[]> queue= new LinkedList<int[]>();
|
||||
queue.add(new int[] { 0, 0, 0, 0 });
|
||||
while(queue.size() > 0) {
|
||||
int[] x = queue.poll();
|
||||
int i = x[0], j = x[1], si = x[2], sj = x[3];
|
||||
if(i >= m || j >= n || k < si + sj || visited[i][j]) continue;
|
||||
visited[i][j] = true;
|
||||
res ++;
|
||||
queue.add(new int[] { i + 1, j, (i + 1) % 10 != 0 ? si + 1 : si - 8, sj });
|
||||
queue.add(new int[] { i, j + 1, si, (j + 1) % 10 != 0 ? sj + 1 : sj - 8 });
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_13_range_of_motion_of_a_robot_s2 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int m = 2;
|
||||
int n = 3;
|
||||
int k = 1;
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int res = slt.movingCount(m, n, k);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* File: sfo_14i_cut_the_rope_i_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_14i_cut_the_rope_i_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int cuttingRope(int n) {
|
||||
if(n <= 3) return n - 1;
|
||||
int a = n / 3, b = n % 3;
|
||||
if(b == 0) return (int)Math.pow(3, a);
|
||||
if(b == 1) return (int)Math.pow(3, a - 1) * 4;
|
||||
return (int)Math.pow(3, a) * 2;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_14i_cut_the_rope_i_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int n = 10;
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int res = slt.cuttingRope(n);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* File: sfo_14ii_cut_the_rope_ii_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_14ii_cut_the_rope_ii_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int cuttingRope(int n) {
|
||||
if(n <= 3) return n - 1;
|
||||
int b = n % 3, p = 1000000007;
|
||||
long rem = 1, x = 3;
|
||||
for(int a = n / 3 - 1; a > 0; a /= 2) {
|
||||
if(a % 2 == 1) rem = (rem * x) % p;
|
||||
x = (x * x) % p;
|
||||
}
|
||||
if(b == 0) return (int)(rem * 3 % p);
|
||||
if(b == 1) return (int)(rem * 4 % p);
|
||||
return (int)(rem * 6 % p);
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_14ii_cut_the_rope_ii_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int n = 10;
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int res = slt.cuttingRope(n);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* File: sfo_15_number_of_1_bits_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_15_number_of_1_bits_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int hammingWeight(int n) {
|
||||
int res = 0;
|
||||
while(n != 0) {
|
||||
res += n & 1;
|
||||
n >>>= 1;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_15_number_of_1_bits_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int n = 11;
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int res = slt.hammingWeight(n);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* File: sfo_15_number_of_1_bits_s2.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_15_number_of_1_bits_s2;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int hammingWeight(int n) {
|
||||
int res = 0;
|
||||
while(n != 0) {
|
||||
res++;
|
||||
n &= n - 1;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_15_number_of_1_bits_s2 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int n = 11;
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int res = slt.hammingWeight(n);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* File: sfo_16_powers_of_integers_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_16_powers_of_integers_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public double myPow(double x, int n) {
|
||||
if(x == 0.0f) return 0.0d;
|
||||
long b = n;
|
||||
double res = 1.0;
|
||||
if(b < 0) {
|
||||
x = 1 / x;
|
||||
b = -b;
|
||||
}
|
||||
while(b > 0) {
|
||||
if((b & 1) == 1) res *= x;
|
||||
x *= x;
|
||||
b >>= 1;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_16_powers_of_integers_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
double x = 2.0;
|
||||
int n = 10;
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
double res = slt.myPow(x, n);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* File: sfo_17_print_from_1_to_the_largest_n_digits_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_17_print_from_1_to_the_largest_n_digits_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int[] printNumbers(int n) {
|
||||
int end = (int)Math.pow(10, n) - 1;
|
||||
int[] res = new int[end];
|
||||
for(int i = 0; i < end; i++)
|
||||
res[i] = i + 1;
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_17_print_from_1_to_the_largest_n_digits_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int n = 1;
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int[] res = slt.printNumbers(n);
|
||||
System.out.println(Arrays.toString(res));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* File: sfo_17_print_from_1_to_the_largest_n_digits_s2.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_17_print_from_1_to_the_largest_n_digits_s2;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
StringBuilder res;
|
||||
int count = 0, n;
|
||||
char[] num, loop = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
|
||||
public String printNumbers(int n) {
|
||||
this.n = n;
|
||||
res = new StringBuilder(); // 数字字符串集
|
||||
num = new char[n]; // 定义长度为 n 的字符列表
|
||||
dfs(0); // 开启全排列递归
|
||||
res.deleteCharAt(res.length() - 1); // 删除最后多余的逗号
|
||||
return res.toString(); // 转化为字符串并返回
|
||||
}
|
||||
void dfs(int x) {
|
||||
if(x == n) { // 终止条件:已固定完所有位
|
||||
res.append(String.valueOf(num) + ","); // 拼接 num 并添加至 res 尾部,使用逗号隔开
|
||||
return;
|
||||
}
|
||||
for(char i : loop) { // 遍历 ‘0‘ - ’9‘
|
||||
num[x] = i; // 固定第 x 位为 i
|
||||
dfs(x + 1); // 开启固定第 x + 1 位
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_17_print_from_1_to_the_largest_n_digits_s2 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int n = 1;
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
String res = slt.printNumbers(n);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* File: sfo_17_print_from_1_to_the_largest_n_digits_s3.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_17_print_from_1_to_the_largest_n_digits_s3;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
StringBuilder res;
|
||||
int nine = 0, count = 0, start, n;
|
||||
char[] num, loop = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
|
||||
public String printNumbers(int n) {
|
||||
this.n = n;
|
||||
res = new StringBuilder();
|
||||
num = new char[n];
|
||||
start = n - 1;
|
||||
dfs(0);
|
||||
res.deleteCharAt(res.length() - 1);
|
||||
return res.toString();
|
||||
}
|
||||
void dfs(int x) {
|
||||
if(x == n) {
|
||||
String s = String.valueOf(num).substring(start);
|
||||
if(!s.equals("0")) res.append(s + ",");
|
||||
if(n - start == nine) start--;
|
||||
return;
|
||||
}
|
||||
for(char i : loop) {
|
||||
if(i == '9') nine++;
|
||||
num[x] = i;
|
||||
dfs(x + 1);
|
||||
}
|
||||
nine--;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_17_print_from_1_to_the_largest_n_digits_s3 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int n = 1;
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
String res = slt.printNumbers(n);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* File: sfo_17_print_from_1_to_the_largest_n_digits_s4.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_17_print_from_1_to_the_largest_n_digits_s4;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
int[] res;
|
||||
int nine = 0, count = 0, start, n;
|
||||
char[] num, loop = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
|
||||
public int[] printNumbers(int n) {
|
||||
this.n = n;
|
||||
res = new int[(int)Math.pow(10, n) - 1];
|
||||
num = new char[n];
|
||||
start = n - 1;
|
||||
dfs(0);
|
||||
return res;
|
||||
}
|
||||
void dfs(int x) {
|
||||
if(x == n) {
|
||||
String s = String.valueOf(num).substring(start);
|
||||
if(!s.equals("0")) res[count++] = Integer.parseInt(s);
|
||||
if(n - start == nine) start--;
|
||||
return;
|
||||
}
|
||||
for(char i : loop) {
|
||||
if(i == '9') nine++;
|
||||
num[x] = i;
|
||||
dfs(x + 1);
|
||||
}
|
||||
nine--;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_17_print_from_1_to_the_largest_n_digits_s4 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int n = 1;
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int[] res = slt.printNumbers(n);
|
||||
System.out.println(Arrays.toString(res));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* File: sfo_18_delete_a_node_from_a_linked_list_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_18_delete_a_node_from_a_linked_list_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public ListNode deleteNode(ListNode head, int val) {
|
||||
if(head.val == val) return head.next;
|
||||
ListNode pre = head, cur = head.next;
|
||||
while(cur != null && cur.val != val) {
|
||||
pre = cur;
|
||||
cur = cur.next;
|
||||
}
|
||||
if(cur != null) pre.next = cur.next;
|
||||
return head;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_18_delete_a_node_from_a_linked_list_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
ListNode head = ListNode.arrToLinkedList(new int[] { 4, 5, 1, 9 });
|
||||
int val = 5;
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
ListNode res = slt.deleteNode(head, val);
|
||||
PrintUtil.printLinkedList(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* File: sfo_19_regular_expression_matching_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_19_regular_expression_matching_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public boolean isMatch(String s, String p) {
|
||||
int m = s.length() + 1, n = p.length() + 1;
|
||||
boolean[][] dp = new boolean[m][n];
|
||||
dp[0][0] = true;
|
||||
for(int j = 2; j < n; j += 2)
|
||||
dp[0][j] = dp[0][j - 2] && p.charAt(j - 1) == '*';
|
||||
for(int i = 1; i < m; i++) {
|
||||
for(int j = 1; j < n; j++) {
|
||||
dp[i][j] = p.charAt(j - 1) == '*' ?
|
||||
dp[i][j - 2] || dp[i - 1][j] && (s.charAt(i - 1) == p.charAt(j - 2) || p.charAt(j - 2) == '.') :
|
||||
dp[i - 1][j - 1] && (p.charAt(j - 1) == '.' || s.charAt(i - 1) == p.charAt(j - 1));
|
||||
}
|
||||
}
|
||||
return dp[m - 1][n - 1];
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_19_regular_expression_matching_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
String s = "mississippi";
|
||||
String p = "mis*is*p*.";
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
boolean res = slt.isMatch(s, p);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* File: sfo_19_regular_expression_matching_s2.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_19_regular_expression_matching_s2;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public boolean isMatch(String s, String p) {
|
||||
int m = s.length() + 1, n = p.length() + 1;
|
||||
boolean[][] dp = new boolean[m][n];
|
||||
dp[0][0] = true;
|
||||
// 初始化首行
|
||||
for(int j = 2; j < n; j += 2)
|
||||
dp[0][j] = dp[0][j - 2] && p.charAt(j - 1) == '*';
|
||||
// 状态转移
|
||||
for(int i = 1; i < m; i++) {
|
||||
for(int j = 1; j < n; j++) {
|
||||
if(p.charAt(j - 1) == '*') {
|
||||
if(dp[i][j - 2]) dp[i][j] = true; // 1.
|
||||
else if(dp[i - 1][j] && s.charAt(i - 1) == p.charAt(j - 2)) dp[i][j] = true; // 2.
|
||||
else if(dp[i - 1][j] && p.charAt(j - 2) == '.') dp[i][j] = true; // 3.
|
||||
} else {
|
||||
if(dp[i - 1][j - 1] && s.charAt(i - 1) == p.charAt(j - 1)) dp[i][j] = true; // 1.
|
||||
else if(dp[i - 1][j - 1] && p.charAt(j - 1) == '.') dp[i][j] = true; // 2.
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[m - 1][n - 1];
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_19_regular_expression_matching_s2 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
String s = "mississippi";
|
||||
String p = "mis*is*p*.";
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
boolean res = slt.isMatch(s, p);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* File: sfo_20_a_string_representing_a_numeric_value_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_20_a_string_representing_a_numeric_value_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public boolean isNumber(String s) {
|
||||
Map[] states = {
|
||||
new HashMap<>() {{ put(' ', 0); put('s', 1); put('d', 2); put('.', 4); }}, // 0.
|
||||
new HashMap<>() {{ put('d', 2); put('.', 4); }}, // 1.
|
||||
new HashMap<>() {{ put('d', 2); put('.', 3); put('e', 5); put(' ', 8); }}, // 2.
|
||||
new HashMap<>() {{ put('d', 3); put('e', 5); put(' ', 8); }}, // 3.
|
||||
new HashMap<>() {{ put('d', 3); }}, // 4.
|
||||
new HashMap<>() {{ put('s', 6); put('d', 7); }}, // 5.
|
||||
new HashMap<>() {{ put('d', 7); }}, // 6.
|
||||
new HashMap<>() {{ put('d', 7); put(' ', 8); }}, // 7.
|
||||
new HashMap<>() {{ put(' ', 8); }} // 8.
|
||||
};
|
||||
int p = 0;
|
||||
char t;
|
||||
for(char c : s.toCharArray()) {
|
||||
if(c >= '0' && c <= '9') t = 'd';
|
||||
else if(c == '+' || c == '-') t = 's';
|
||||
else if(c == 'e' || c == 'E') t = 'e';
|
||||
else if(c == '.' || c == ' ') t = c;
|
||||
else t = '?';
|
||||
if(!states[p].containsKey(t)) return false;
|
||||
p = (int)states[p].get(t);
|
||||
}
|
||||
return p == 2 || p == 3 || p == 7 || p == 8;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_20_a_string_representing_a_numeric_value_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
String s = " .1 ";
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
boolean res = slt.isNumber(s);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* File: sfo_21_adjust_the_order_of_numbers_in_an_array_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_21_adjust_the_order_of_numbers_in_an_array_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int[] exchange(int[] nums) {
|
||||
int i = 0, j = nums.length - 1, tmp;
|
||||
while(i < j) {
|
||||
while(i < j && (nums[i] & 1) == 1) i++;
|
||||
while(i < j && (nums[j] & 1) == 0) j--;
|
||||
tmp = nums[i];
|
||||
nums[i] = nums[j];
|
||||
nums[j] = tmp;
|
||||
}
|
||||
return nums;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_21_adjust_the_order_of_numbers_in_an_array_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int[] nums = { 1, 2, 3, 4 };
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int[] res = slt.exchange(nums);
|
||||
System.out.println(Arrays.toString(res));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* File: sfo_22_the_kth_node_from_the_end_of_a_linked_list_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_22_the_kth_node_from_the_end_of_a_linked_list_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public ListNode getKthFromEnd(ListNode head, int k) {
|
||||
ListNode former = head, latter = head;
|
||||
for(int i = 0; i < k; i++)
|
||||
former = former.next;
|
||||
while(former != null) {
|
||||
former = former.next;
|
||||
latter = latter.next;
|
||||
}
|
||||
return latter;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_22_the_kth_node_from_the_end_of_a_linked_list_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
ListNode head = ListNode.arrToLinkedList(new int[] { 1, 2, 3, 4, 5 });
|
||||
int k = 2;
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
ListNode res = slt.getKthFromEnd(head, k);
|
||||
PrintUtil.printLinkedList(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* File: sfo_22_the_kth_node_from_the_end_of_a_linked_list_s2.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_22_the_kth_node_from_the_end_of_a_linked_list_s2;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public ListNode getKthFromEnd(ListNode head, int k) {
|
||||
ListNode former = head, latter = head;
|
||||
for(int i = 0; i < k; i++) {
|
||||
if(former == null) return null;
|
||||
former = former.next;
|
||||
}
|
||||
while(former != null) {
|
||||
former = former.next;
|
||||
latter = latter.next;
|
||||
}
|
||||
return latter;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_22_the_kth_node_from_the_end_of_a_linked_list_s2 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
ListNode head = ListNode.arrToLinkedList(new int[] { 1, 2, 3, 4, 5 });
|
||||
int k = 2;
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
ListNode res = slt.getKthFromEnd(head, k);
|
||||
PrintUtil.printLinkedList(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* File: sfo_24_reverse_a_linked_list_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_24_reverse_a_linked_list_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public ListNode reverseList(ListNode head) {
|
||||
ListNode cur = head, pre = null;
|
||||
while(cur != null) {
|
||||
ListNode tmp = cur.next; // 暂存后继节点 cur.next
|
||||
cur.next = pre; // 修改 next 引用指向
|
||||
pre = cur; // pre 暂存 cur
|
||||
cur = tmp; // cur 访问下一节点
|
||||
}
|
||||
return pre;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_24_reverse_a_linked_list_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
ListNode head = ListNode.arrToLinkedList(new int[] { 1, 2, 3, 4, 5 });
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
ListNode res = slt.reverseList(head);
|
||||
PrintUtil.printLinkedList(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* File: sfo_24_reverse_a_linked_list_s2.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_24_reverse_a_linked_list_s2;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public ListNode reverseList(ListNode head) {
|
||||
return recur(head, null); // 调用递归并返回
|
||||
}
|
||||
private ListNode recur(ListNode cur, ListNode pre) {
|
||||
if (cur == null) return pre; // 终止条件
|
||||
ListNode res = recur(cur.next, cur); // 递归后继节点
|
||||
cur.next = pre; // 修改节点引用指向
|
||||
return res; // 返回反转链表的头节点
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_24_reverse_a_linked_list_s2 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
ListNode head = ListNode.arrToLinkedList(new int[] { 1, 2, 3, 4, 5 });
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
ListNode res = slt.reverseList(head);
|
||||
PrintUtil.printLinkedList(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* File: sfo_25_combine_two_sorted_linked_lists_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_25_combine_two_sorted_linked_lists_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
|
||||
ListNode dum = new ListNode(0), cur = dum;
|
||||
while(l1 != null && l2 != null) {
|
||||
if(l1.val < l2.val) {
|
||||
cur.next = l1;
|
||||
l1 = l1.next;
|
||||
}
|
||||
else {
|
||||
cur.next = l2;
|
||||
l2 = l2.next;
|
||||
}
|
||||
cur = cur.next;
|
||||
}
|
||||
cur.next = l1 != null ? l1 : l2;
|
||||
return dum.next;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_25_combine_two_sorted_linked_lists_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
ListNode l1 = ListNode.arrToLinkedList(new int[] { 1, 2, 4 });
|
||||
ListNode l2 = ListNode.arrToLinkedList(new int[] { 1, 3, 4 });
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
ListNode res = slt.mergeTwoLists(l1, l2);
|
||||
PrintUtil.printLinkedList(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* File: sfo_26_substructure_of_a_binary_tree_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_26_substructure_of_a_binary_tree_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public boolean isSubStructure(TreeNode A, TreeNode B) {
|
||||
return (A != null && B != null) && (recur(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B));
|
||||
}
|
||||
boolean recur(TreeNode A, TreeNode B) {
|
||||
if(B == null) return true;
|
||||
if(A == null || A.val != B.val) return false;
|
||||
return recur(A.left, B.left) && recur(A.right, B.right);
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_26_substructure_of_a_binary_tree_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
TreeNode A = TreeNode.arrToTree(new Integer[] { 3, 4, 5, 1, 2, null, null, null, null, null, null });
|
||||
TreeNode B = TreeNode.arrToTree(new Integer[] { 4, 1, null, null, null });
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
boolean res = slt.isSubStructure(A, B);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* File: sfo_27_mirror_of_a_binary_tree_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_27_mirror_of_a_binary_tree_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public TreeNode mirrorTree(TreeNode root) {
|
||||
if(root == null) return null;
|
||||
TreeNode tmp = root.left;
|
||||
root.left = mirrorTree(root.right);
|
||||
root.right = mirrorTree(tmp);
|
||||
return root;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_27_mirror_of_a_binary_tree_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
TreeNode root = TreeNode.arrToTree(new Integer[] { 4, 2, 7, 1, 3, 6, 9, null, null, null, null, null, null, null, null });
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
TreeNode res = slt.mirrorTree(root);
|
||||
PrintUtil.printTree(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* File: sfo_27_mirror_of_a_binary_tree_s2.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_27_mirror_of_a_binary_tree_s2;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public TreeNode mirrorTree(TreeNode root) {
|
||||
if(root == null) return null;
|
||||
Stack<TreeNode> stack = new Stack<>() {{ add(root); }};
|
||||
while(!stack.isEmpty()) {
|
||||
TreeNode node = stack.pop();
|
||||
if(node.left != null) stack.add(node.left);
|
||||
if(node.right != null) stack.add(node.right);
|
||||
TreeNode tmp = node.left;
|
||||
node.left = node.right;
|
||||
node.right = tmp;
|
||||
}
|
||||
return root;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_27_mirror_of_a_binary_tree_s2 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
TreeNode root = TreeNode.arrToTree(new Integer[] { 4, 2, 7, 1, 3, 6, 9, null, null, null, null, null, null, null, null });
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
TreeNode res = slt.mirrorTree(root);
|
||||
PrintUtil.printTree(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* File: sfo_28_symmetric_binary_tree_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_28_symmetric_binary_tree_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public boolean isSymmetric(TreeNode root) {
|
||||
return root == null || recur(root.left, root.right);
|
||||
}
|
||||
boolean recur(TreeNode L, TreeNode R) {
|
||||
if(L == null && R == null) return true;
|
||||
if(L == null || R == null || L.val != R.val) return false;
|
||||
return recur(L.left, R.right) && recur(L.right, R.left);
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_28_symmetric_binary_tree_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
TreeNode root = TreeNode.arrToTree(new Integer[] { 1, 2, 2, 3, 4, 4, 3, null, null, null, null, null, null, null, null });
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
boolean res = slt.isSymmetric(root);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* File: sfo_29_print_a_given_matrix_in_spiral_form_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_29_print_a_given_matrix_in_spiral_form_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int[] spiralOrder(int[][] matrix) {
|
||||
if(matrix.length == 0) return new int[0];
|
||||
int l = 0, r = matrix[0].length - 1, t = 0, b = matrix.length - 1, x = 0;
|
||||
int[] res = new int[(r + 1) * (b + 1)];
|
||||
while(true) {
|
||||
for(int i = l; i <= r; i++) res[x++] = matrix[t][i]; // left to right
|
||||
if(++t > b) break;
|
||||
for(int i = t; i <= b; i++) res[x++] = matrix[i][r]; // top to bottom
|
||||
if(l > --r) break;
|
||||
for(int i = r; i >= l; i--) res[x++] = matrix[b][i]; // right to left
|
||||
if(t > --b) break;
|
||||
for(int i = b; i >= t; i--) res[x++] = matrix[i][l]; // bottom to top
|
||||
if(++l > r) break;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_29_print_a_given_matrix_in_spiral_form_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int[][] matrix = {
|
||||
{ 1, 2, 3 },
|
||||
{ 4, 5, 6 },
|
||||
{ 7, 8, 9 }
|
||||
};
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int[] res = slt.spiralOrder(matrix);
|
||||
System.out.println(Arrays.toString(res));
|
||||
}
|
||||
}
|
||||
50
java/sfo_30_min_stack_s1/sfo_30_min_stack_s1.java
Normal file
50
java/sfo_30_min_stack_s1/sfo_30_min_stack_s1.java
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* File: sfo_30_min_stack_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_30_min_stack_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class MinStack {
|
||||
Stack<Integer> A, B;
|
||||
public MinStack() {
|
||||
A = new Stack<>();
|
||||
B = new Stack<>();
|
||||
}
|
||||
public void push(int x) {
|
||||
A.add(x);
|
||||
if(B.empty() || B.peek() >= x)
|
||||
B.add(x);
|
||||
}
|
||||
public void pop() {
|
||||
if(A.pop().equals(B.peek()))
|
||||
B.pop();
|
||||
}
|
||||
public int top() {
|
||||
return A.peek();
|
||||
}
|
||||
public int min() {
|
||||
return B.peek();
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_30_min_stack_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ====== Driver Code ======
|
||||
List<Integer> res = new ArrayList<>();
|
||||
MinStack minStack = new MinStack();
|
||||
minStack.push(-2);
|
||||
minStack.push(0);
|
||||
minStack.push(-3);
|
||||
res.add(minStack.min());
|
||||
minStack.pop();
|
||||
res.add(minStack.top());
|
||||
res.add(minStack.min());
|
||||
System.out.println(Arrays.toString(res.toArray()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* File: sfo_31_validate_stack_sequences_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_31_validate_stack_sequences_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public boolean validateStackSequences(int[] pushed, int[] popped) {
|
||||
Stack<Integer> stack = new Stack<>();
|
||||
int i = 0;
|
||||
for(int num : pushed) {
|
||||
stack.push(num); // num 入栈
|
||||
while(!stack.isEmpty() && stack.peek() == popped[i]) { // 循环判断与出栈
|
||||
stack.pop();
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return stack.isEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_31_validate_stack_sequences_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int[] pushed = { 1, 2, 3, 4, 5 };
|
||||
int[] popped = { 4, 5, 3, 2, 1 };
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
boolean res = slt.validateStackSequences(pushed, popped);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* File: sfo_32i_print_a_binary_tree_topbottom_i_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_32i_print_a_binary_tree_topbottom_i_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int[] levelOrder(TreeNode root) {
|
||||
if(root == null) return new int[0];
|
||||
Queue<TreeNode> queue = new LinkedList<>(){{ add(root); }};
|
||||
ArrayList<Integer> ans = new ArrayList<>();
|
||||
while(!queue.isEmpty()) {
|
||||
TreeNode node = queue.poll();
|
||||
ans.add(node.val);
|
||||
if(node.left != null) queue.add(node.left);
|
||||
if(node.right != null) queue.add(node.right);
|
||||
}
|
||||
int[] res = new int[ans.size()];
|
||||
for(int i = 0; i < ans.size(); i++)
|
||||
res[i] = ans.get(i);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_32i_print_a_binary_tree_topbottom_i_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
TreeNode root = TreeNode.arrToTree(new Integer[] { 3, 9, 20, null, null, 15, 7, null, null, null, null });
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int[] res = slt.levelOrder(root);
|
||||
System.out.println(Arrays.toString(res));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* File: sfo_32ii_print_a_binary_tree_topbottom_ii_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_32ii_print_a_binary_tree_topbottom_ii_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public List<List<Integer>> levelOrder(TreeNode root) {
|
||||
Queue<TreeNode> queue = new LinkedList<>();
|
||||
List<List<Integer>> res = new ArrayList<>();
|
||||
if(root != null) queue.add(root);
|
||||
while(!queue.isEmpty()) {
|
||||
List<Integer> tmp = new ArrayList<>();
|
||||
for(int i = queue.size(); i > 0; i--) {
|
||||
TreeNode node = queue.poll();
|
||||
tmp.add(node.val);
|
||||
if(node.left != null) queue.add(node.left);
|
||||
if(node.right != null) queue.add(node.right);
|
||||
}
|
||||
res.add(tmp);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_32ii_print_a_binary_tree_topbottom_ii_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
TreeNode root = TreeNode.arrToTree(new Integer[] { 3, 9, 20, null, null, 15, 7, null, null, null, null });
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
List<List<Integer>> res = slt.levelOrder(root);
|
||||
System.out.println(Arrays.deepToString(res.toArray()));;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* File: sfo_32iii_print_a_binary_tree_topbottom_iii_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_32iii_print_a_binary_tree_topbottom_iii_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public List<List<Integer>> levelOrder(TreeNode root) {
|
||||
Queue<TreeNode> queue = new LinkedList<>();
|
||||
List<List<Integer>> res = new ArrayList<>();
|
||||
if(root != null) queue.add(root);
|
||||
while(!queue.isEmpty()) {
|
||||
LinkedList<Integer> tmp = new LinkedList<>();
|
||||
for(int i = queue.size(); i > 0; i--) {
|
||||
TreeNode node = queue.poll();
|
||||
if(res.size() % 2 == 0) tmp.addLast(node.val);
|
||||
else tmp.addFirst(node.val);
|
||||
if(node.left != null) queue.add(node.left);
|
||||
if(node.right != null) queue.add(node.right);
|
||||
}
|
||||
res.add(tmp);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_32iii_print_a_binary_tree_topbottom_iii_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
TreeNode root = TreeNode.arrToTree(new Integer[] { 3, 9, 20, null, null, 15, 7, null, null, null, null });
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
List<List<Integer>> res = slt.levelOrder(root);
|
||||
System.out.println(Arrays.deepToString(res.toArray()));;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* File: sfo_32iii_print_a_binary_tree_topbottom_iii_s2.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_32iii_print_a_binary_tree_topbottom_iii_s2;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public List<List<Integer>> levelOrder(TreeNode root) {
|
||||
Deque<TreeNode> deque = new LinkedList<>();
|
||||
List<List<Integer>> res = new ArrayList<>();
|
||||
if(root != null) deque.add(root);
|
||||
while(!deque.isEmpty()) {
|
||||
// 打印奇数层
|
||||
List<Integer> tmp = new ArrayList<>();
|
||||
for(int i = deque.size(); i > 0; i--) {
|
||||
// 从左向右打印
|
||||
TreeNode node = deque.removeFirst();
|
||||
tmp.add(node.val);
|
||||
// 先左后右加入下层节点
|
||||
if(node.left != null) deque.addLast(node.left);
|
||||
if(node.right != null) deque.addLast(node.right);
|
||||
}
|
||||
res.add(tmp);
|
||||
if(deque.isEmpty()) break; // 若为空则提前跳出
|
||||
// 打印偶数层
|
||||
tmp = new ArrayList<>();
|
||||
for(int i = deque.size(); i > 0; i--) {
|
||||
// 从右向左打印
|
||||
TreeNode node = deque.removeLast();
|
||||
tmp.add(node.val);
|
||||
// 先右后左加入下层节点
|
||||
if(node.right != null) deque.addFirst(node.right);
|
||||
if(node.left != null) deque.addFirst(node.left);
|
||||
}
|
||||
res.add(tmp);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_32iii_print_a_binary_tree_topbottom_iii_s2 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
TreeNode root = TreeNode.arrToTree(new Integer[] { 3, 9, 20, null, null, 15, 7, null, null, null, null });
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
List<List<Integer>> res = slt.levelOrder(root);
|
||||
System.out.println(Arrays.deepToString(res.toArray()));;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* File: sfo_32iii_print_a_binary_tree_topbottom_iii_s3.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_32iii_print_a_binary_tree_topbottom_iii_s3;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public List<List<Integer>> levelOrder(TreeNode root) {
|
||||
Queue<TreeNode> queue = new LinkedList<>();
|
||||
List<List<Integer>> res = new ArrayList<>();
|
||||
if(root != null) queue.add(root);
|
||||
while(!queue.isEmpty()) {
|
||||
List<Integer> tmp = new ArrayList<>();
|
||||
for(int i = queue.size(); i > 0; i--) {
|
||||
TreeNode node = queue.poll();
|
||||
tmp.add(node.val);
|
||||
if(node.left != null) queue.add(node.left);
|
||||
if(node.right != null) queue.add(node.right);
|
||||
}
|
||||
if(res.size() % 2 == 1) Collections.reverse(tmp);
|
||||
res.add(tmp);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_32iii_print_a_binary_tree_topbottom_iii_s3 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
TreeNode root = TreeNode.arrToTree(new Integer[] { 3, 9, 20, null, null, 15, 7, null, null, null, null });
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
List<List<Integer>> res = slt.levelOrder(root);
|
||||
System.out.println(Arrays.deepToString(res.toArray()));;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* File: sfo_33_postorder_traversal_of_a_binary_search_tree_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_33_postorder_traversal_of_a_binary_search_tree_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public boolean verifyPostorder(int[] postorder) {
|
||||
return recur(postorder, 0, postorder.length - 1);
|
||||
}
|
||||
boolean recur(int[] postorder, int i, int j) {
|
||||
if(i >= j) return true;
|
||||
int p = i;
|
||||
while(postorder[p] < postorder[j]) p++;
|
||||
int m = p;
|
||||
while(postorder[p] > postorder[j]) p++;
|
||||
return p == j && recur(postorder, i, m - 1) && recur(postorder, m, j - 1);
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_33_postorder_traversal_of_a_binary_search_tree_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int[] postorder = { 1, 6, 3, 2, 5 };
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
boolean res = slt.verifyPostorder(postorder);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* File: sfo_33_postorder_traversal_of_a_binary_search_tree_s2.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_33_postorder_traversal_of_a_binary_search_tree_s2;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public boolean verifyPostorder(int[] postorder) {
|
||||
Stack<Integer> stack = new Stack<>();
|
||||
int root = Integer.MAX_VALUE;
|
||||
for(int i = postorder.length - 1; i >= 0; i--) {
|
||||
if(postorder[i] > root) return false;
|
||||
while(!stack.isEmpty() && stack.peek() > postorder[i])
|
||||
root = stack.pop();
|
||||
stack.add(postorder[i]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_33_postorder_traversal_of_a_binary_search_tree_s2 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int[] postorder = { 1, 6, 3, 2, 5 };
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
boolean res = slt.verifyPostorder(postorder);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* File: sfo_34_all_xsum_paths_in_a_binary_tree_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_34_all_xsum_paths_in_a_binary_tree_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
LinkedList<List<Integer>> res = new LinkedList<>();
|
||||
LinkedList<Integer> path = new LinkedList<>();
|
||||
public List<List<Integer>> pathSum(TreeNode root, int sum) {
|
||||
recur(root, sum);
|
||||
return res;
|
||||
}
|
||||
void recur(TreeNode root, int tar) {
|
||||
if(root == null) return;
|
||||
path.add(root.val);
|
||||
tar -= root.val;
|
||||
if(tar == 0 && root.left == null && root.right == null)
|
||||
res.add(new LinkedList(path));
|
||||
recur(root.left, tar);
|
||||
recur(root.right, tar);
|
||||
path.removeLast();
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_34_all_xsum_paths_in_a_binary_tree_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
TreeNode root = TreeNode.arrToTree(new Integer[] { 5, 4, 8, 11, null, 13, 4, 7, 2, null, null, 5, 1, null, null, null, null, null, null, null, null });
|
||||
int sum = 22;
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
List<List<Integer>> res = slt.pathSum(root, sum);
|
||||
System.out.println(Arrays.deepToString(res.toArray()));;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* File: sfo_35_clone_a_linked_list_with_next_and_random_pointer_s4.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_35_clone_a_linked_list_with_next_and_random_pointer_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// Definition for a Node.
|
||||
class Node {
|
||||
int val;
|
||||
Node next;
|
||||
Node random;
|
||||
|
||||
public Node(int val) {
|
||||
this.val = val;
|
||||
this.next = null;
|
||||
this.random = null;
|
||||
}
|
||||
}
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public Node copyRandomList(Node head) {
|
||||
if(head == null) return null;
|
||||
Node cur = head;
|
||||
Map<Node, Node> map = new HashMap<>();
|
||||
// 3. 复制各节点,并建立 “原节点 -> 新节点” 的 Map 映射
|
||||
while(cur != null) {
|
||||
map.put(cur, new Node(cur.val));
|
||||
cur = cur.next;
|
||||
}
|
||||
cur = head;
|
||||
// 4. 构建新链表的 next 和 random 指向
|
||||
while(cur != null) {
|
||||
map.get(cur).next = map.get(cur.next);
|
||||
map.get(cur).random = map.get(cur.random);
|
||||
cur = cur.next;
|
||||
}
|
||||
// 5. 返回新链表的头节点
|
||||
return map.get(head);
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_35_clone_a_linked_list_with_next_and_random_pointer_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int[] nodesVal = new int[] { 7, 13, 11, 10, 1 };
|
||||
Integer[] nodesRandom = new Integer[] { null, 0, 4, 2, 0 };
|
||||
// Construct nodes
|
||||
List<Node> nodeList = new ArrayList<>();
|
||||
for (int val : nodesVal) {
|
||||
nodeList.add(new Node(val));
|
||||
}
|
||||
// Build next reference
|
||||
for (int i = 0; i < nodesVal.length - 1; i++) {
|
||||
nodeList.get(i).next = nodeList.get(i + 1);
|
||||
}
|
||||
// Build random reference
|
||||
for (int i = 0; i < nodesVal.length; i++) {
|
||||
if (nodesRandom[i] != null)
|
||||
nodeList.get(i).random = nodeList.get(nodesRandom[i]);
|
||||
}
|
||||
// Get the head of the linked list
|
||||
Node head = nodeList.get(0);
|
||||
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
Node res = slt.copyRandomList(head);
|
||||
// Print the copied linked list
|
||||
List<Node> nodeListNew = new ArrayList<>();
|
||||
while (res != null) {
|
||||
nodeListNew.add(res);
|
||||
res = res.next;
|
||||
}
|
||||
Integer[][] printArr = new Integer[nodesVal.length][2];
|
||||
for (int i = 0; i < nodeListNew.size(); i++) {
|
||||
Node node = nodeListNew.get(i);
|
||||
printArr[i][0] = node.val;
|
||||
printArr[i][1] = node.random != null ? nodeListNew.indexOf(node.random) : null;
|
||||
}
|
||||
System.out.println(Arrays.deepToString(printArr));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* File: sfo_35_clone_a_linked_list_with_next_and_random_pointer_s5.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_35_clone_a_linked_list_with_next_and_random_pointer_s2;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// Definition for a Node.
|
||||
class Node {
|
||||
int val;
|
||||
Node next;
|
||||
Node random;
|
||||
|
||||
public Node(int val) {
|
||||
this.val = val;
|
||||
this.next = null;
|
||||
this.random = null;
|
||||
}
|
||||
}
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public Node copyRandomList(Node head) {
|
||||
if(head == null) return null;
|
||||
Node cur = head;
|
||||
// 1. 复制各节点,并构建拼接链表
|
||||
while(cur != null) {
|
||||
Node tmp = new Node(cur.val);
|
||||
tmp.next = cur.next;
|
||||
cur.next = tmp;
|
||||
cur = tmp.next;
|
||||
}
|
||||
// 2. 构建各新节点的 random 指向
|
||||
cur = head;
|
||||
while(cur != null) {
|
||||
if(cur.random != null)
|
||||
cur.next.random = cur.random.next;
|
||||
cur = cur.next.next;
|
||||
}
|
||||
// 3. 拆分两链表
|
||||
cur = head.next;
|
||||
Node pre = head, res = head.next;
|
||||
while(cur.next != null) {
|
||||
pre.next = pre.next.next;
|
||||
cur.next = cur.next.next;
|
||||
pre = pre.next;
|
||||
cur = cur.next;
|
||||
}
|
||||
pre.next = null; // 单独处理原链表尾节点
|
||||
return res; // 返回新链表头节点
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_35_clone_a_linked_list_with_next_and_random_pointer_s2 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int[] nodesVal = new int[] { 7, 13, 11, 10, 1 };
|
||||
Integer[] nodesRandom = new Integer[] { null, 0, 4, 2, 0 };
|
||||
// Construct nodes
|
||||
List<Node> nodeList = new ArrayList<>();
|
||||
for (int val : nodesVal) {
|
||||
nodeList.add(new Node(val));
|
||||
}
|
||||
// Build next reference
|
||||
for (int i = 0; i < nodesVal.length - 1; i++) {
|
||||
nodeList.get(i).next = nodeList.get(i + 1);
|
||||
}
|
||||
// Build random reference
|
||||
for (int i = 0; i < nodesVal.length; i++) {
|
||||
if (nodesRandom[i] != null)
|
||||
nodeList.get(i).random = nodeList.get(nodesRandom[i]);
|
||||
}
|
||||
// Get the head of the linked list
|
||||
Node head = nodeList.get(0);
|
||||
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
Node res = slt.copyRandomList(head);
|
||||
// Print the copied linked list
|
||||
List<Node> nodeListNew = new ArrayList<>();
|
||||
while (res != null) {
|
||||
nodeListNew.add(res);
|
||||
res = res.next;
|
||||
}
|
||||
Integer[][] printArr = new Integer[nodesVal.length][2];
|
||||
for (int i = 0; i < nodeListNew.size(); i++) {
|
||||
Node node = nodeListNew.get(i);
|
||||
printArr[i][0] = node.val;
|
||||
printArr[i][1] = node.random != null ? nodeListNew.indexOf(node.random) : null;
|
||||
}
|
||||
System.out.println(Arrays.deepToString(printArr));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* File: sfo_36_binary_search_tree_and_doubly_linked_list_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_36_binary_search_tree_and_doubly_linked_list_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// Definition for a Node.
|
||||
class Node {
|
||||
public int val;
|
||||
public Node left;
|
||||
public Node right;
|
||||
|
||||
public Node(int val) {
|
||||
this.val = val;
|
||||
this.left = null;
|
||||
this.right = null;
|
||||
}
|
||||
};
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
Node pre, head;
|
||||
public Node treeToDoublyList(Node root) {
|
||||
if(root == null) return null;
|
||||
dfs(root);
|
||||
head.left = pre;
|
||||
pre.right = head;
|
||||
return head;
|
||||
}
|
||||
void dfs(Node cur) {
|
||||
if(cur == null) return;
|
||||
dfs(cur.left);
|
||||
if(pre != null) pre.right = cur;
|
||||
else head = cur;
|
||||
cur.left = pre;
|
||||
pre = cur;
|
||||
dfs(cur.right);
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_36_binary_search_tree_and_doubly_linked_list_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
List<Node> nodeList = new ArrayList<>() {{
|
||||
add(new Node(1));
|
||||
add(new Node(2));
|
||||
add(new Node(3));
|
||||
add(new Node(4));
|
||||
add(new Node(5));
|
||||
}};
|
||||
nodeList.get(3).left = nodeList.get(1);
|
||||
nodeList.get(3).right = nodeList.get(4);
|
||||
nodeList.get(1).left = nodeList.get(0);
|
||||
nodeList.get(1).right = nodeList.get(2);
|
||||
Node root = nodeList.get(3);
|
||||
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
Node res = slt.treeToDoublyList(root);
|
||||
// Print the Doubly circular linked list
|
||||
List<String> nodesVal = new ArrayList<>();
|
||||
for (int i = 0; i <= nodeList.size(); i++) {
|
||||
nodesVal.add(String.valueOf(res.val));
|
||||
res = res.right;
|
||||
}
|
||||
System.out.println(String.join(" <-> ", nodesVal));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* File: sfo_37_serialize_and_deserialize_a_binary_tree_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_37_serialize_and_deserialize_a_binary_tree_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Codec {
|
||||
public String serialize(TreeNode root) {
|
||||
if(root == null) return "[]";
|
||||
StringBuilder res = new StringBuilder("[");
|
||||
Queue<TreeNode> queue = new LinkedList<>() {{ add(root); }};
|
||||
while(!queue.isEmpty()) {
|
||||
TreeNode node = queue.poll();
|
||||
if(node != null) {
|
||||
res.append(node.val + ",");
|
||||
queue.add(node.left);
|
||||
queue.add(node.right);
|
||||
}
|
||||
else res.append("null,");
|
||||
}
|
||||
res.deleteCharAt(res.length() - 1);
|
||||
res.append("]");
|
||||
return res.toString();
|
||||
}
|
||||
|
||||
public TreeNode deserialize(String data) {
|
||||
if(data.equals("[]")) return null;
|
||||
String[] vals = data.substring(1, data.length() - 1).split(",");
|
||||
TreeNode root = new TreeNode(Integer.parseInt(vals[0]));
|
||||
Queue<TreeNode> queue = new LinkedList<>() {{ add(root); }};
|
||||
int i = 1;
|
||||
while(!queue.isEmpty()) {
|
||||
TreeNode node = queue.poll();
|
||||
if(!vals[i].equals("null")) {
|
||||
node.left = new TreeNode(Integer.parseInt(vals[i]));
|
||||
queue.add(node.left);
|
||||
}
|
||||
i++;
|
||||
if(!vals[i].equals("null")) {
|
||||
node.right = new TreeNode(Integer.parseInt(vals[i]));
|
||||
queue.add(node.right);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return root;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_37_serialize_and_deserialize_a_binary_tree_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
String data = "[1,2,3,null,null,4,5,null,null,null,null]";
|
||||
// ====== Driver Code ======
|
||||
Codec codec = new Codec();
|
||||
TreeNode root = codec.deserialize(data);
|
||||
String res = codec.serialize(root);
|
||||
PrintUtil.printTree(root);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* File: sfo_38_all_permutations_of_a_string_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_38_all_permutations_of_a_string_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
List<String> res = new LinkedList<>();
|
||||
char[] c;
|
||||
public String[] permutation(String s) {
|
||||
c = s.toCharArray();
|
||||
dfs(0);
|
||||
return res.toArray(new String[res.size()]);
|
||||
}
|
||||
void dfs(int x) {
|
||||
if(x == c.length - 1) {
|
||||
res.add(String.valueOf(c)); // 添加排列方案
|
||||
return;
|
||||
}
|
||||
HashSet<Character> set = new HashSet<>();
|
||||
for(int i = x; i < c.length; i++) {
|
||||
if(set.contains(c[i])) continue; // 重复,因此剪枝
|
||||
set.add(c[i]);
|
||||
swap(i, x); // 交换,将 c[i] 固定在第 x 位
|
||||
dfs(x + 1); // 开启固定第 x + 1 位字符
|
||||
swap(i, x); // 恢复交换
|
||||
}
|
||||
}
|
||||
void swap(int a, int b) {
|
||||
char tmp = c[a];
|
||||
c[a] = c[b];
|
||||
c[b] = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_38_all_permutations_of_a_string_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
String s = "abc";
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
String[] res = slt.permutation(s);
|
||||
System.out.println(Arrays.toString(res));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* File: sfo_39_the_majority_element_in_an_array_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_39_the_majority_element_in_an_array_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int majorityElement(int[] nums) {
|
||||
int x = 0, votes = 0;
|
||||
for(int num : nums){
|
||||
if(votes == 0) x = num;
|
||||
votes += num == x ? 1 : -1;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_39_the_majority_element_in_an_array_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int[] nums = { 1, 2, 3, 2, 2, 2, 5, 4, 2 };
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int res = slt.majorityElement(nums);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* File: sfo_39_the_majority_element_in_an_array_s2.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_39_the_majority_element_in_an_array_s2;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int majorityElement(int[] nums) {
|
||||
int x = 0, votes = 0, count = 0;
|
||||
for(int num : nums){
|
||||
if(votes == 0) x = num;
|
||||
votes += num == x ? 1 : -1;
|
||||
}
|
||||
// 验证 x 是否为众数
|
||||
for(int num : nums)
|
||||
if(num == x) count++;
|
||||
return count > nums.length / 2 ? x : 0; // 当无众数时返回 0
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_39_the_majority_element_in_an_array_s2 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int[] nums = { 1, 2, 3, 2, 2, 2, 5, 4, 2 };
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int res = slt.majorityElement(nums);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* File: sfo_40_the_smallest_k_numbers_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_40_the_smallest_k_numbers_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int[] getLeastNumbers(int[] arr, int k) {
|
||||
quickSort(arr, 0, arr.length - 1);
|
||||
return Arrays.copyOf(arr, k);
|
||||
}
|
||||
private void quickSort(int[] arr, int l, int r) {
|
||||
// 子数组长度为 1 时终止递归
|
||||
if (l >= r) return;
|
||||
// 哨兵划分操作(以 arr[l] 作为基准数)
|
||||
int i = l, j = r;
|
||||
while (i < j) {
|
||||
while (i < j && arr[j] >= arr[l]) j--;
|
||||
while (i < j && arr[i] <= arr[l]) i++;
|
||||
swap(arr, i, j);
|
||||
}
|
||||
swap(arr, i, l);
|
||||
// 递归左(右)子数组执行哨兵划分
|
||||
quickSort(arr, l, i - 1);
|
||||
quickSort(arr, i + 1, r);
|
||||
}
|
||||
private void swap(int[] arr, int i, int j) {
|
||||
int tmp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_40_the_smallest_k_numbers_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int[] arr = { 3, 2, 1 };
|
||||
int k = 2;
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int[] res = slt.getLeastNumbers(arr, k);
|
||||
System.out.println(Arrays.toString(res));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* File: sfo_40_the_smallest_k_numbers_s2.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_40_the_smallest_k_numbers_s2;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int[] getLeastNumbers(int[] arr, int k) {
|
||||
if (k >= arr.length) return arr;
|
||||
return quickSort(arr, k, 0, arr.length - 1);
|
||||
}
|
||||
private int[] quickSort(int[] arr, int k, int l, int r) {
|
||||
int i = l, j = r;
|
||||
while (i < j) {
|
||||
while (i < j && arr[j] >= arr[l]) j--;
|
||||
while (i < j && arr[i] <= arr[l]) i++;
|
||||
swap(arr, i, j);
|
||||
}
|
||||
swap(arr, i, l);
|
||||
if (i > k) return quickSort(arr, k, l, i - 1);
|
||||
if (i < k) return quickSort(arr, k, i + 1, r);
|
||||
return Arrays.copyOf(arr, k);
|
||||
}
|
||||
private void swap(int[] arr, int i, int j) {
|
||||
int tmp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_40_the_smallest_k_numbers_s2 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int[] arr = { 3, 2, 1 };
|
||||
int k = 2;
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int[] res = slt.getLeastNumbers(arr, k);
|
||||
System.out.println(Arrays.toString(res));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* File: sfo_41_find_median_from_data_stream_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_41_find_median_from_data_stream_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class MedianFinder {
|
||||
Queue<Integer> A, B;
|
||||
public MedianFinder() {
|
||||
A = new PriorityQueue<>(); // 小顶堆,保存较大的一半
|
||||
B = new PriorityQueue<>((x, y) -> (y - x)); // 大顶堆,保存较小的一半
|
||||
}
|
||||
public void addNum(int num) {
|
||||
if(A.size() != B.size()) {
|
||||
A.add(num);
|
||||
B.add(A.poll());
|
||||
} else {
|
||||
B.add(num);
|
||||
A.add(B.poll());
|
||||
}
|
||||
}
|
||||
public double findMedian() {
|
||||
return A.size() != B.size() ? A.peek() : (A.peek() + B.peek()) / 2.0;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_41_find_median_from_data_stream_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ====== Driver Code ======
|
||||
List<Double> res = new ArrayList<>();
|
||||
MedianFinder medianFinder = new MedianFinder();
|
||||
medianFinder.addNum(1);
|
||||
medianFinder.addNum(2);
|
||||
res.add(medianFinder.findMedian());
|
||||
medianFinder.addNum(3);
|
||||
res.add(medianFinder.findMedian());
|
||||
System.out.println(Arrays.toString(res.toArray()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* File: sfo_42_largest_sum_contiguous_subarray_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_42_largest_sum_contiguous_subarray_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int maxSubArray(int[] nums) {
|
||||
int res = nums[0];
|
||||
for(int i = 1; i < nums.length; i++) {
|
||||
nums[i] += Math.max(nums[i - 1], 0);
|
||||
res = Math.max(res, nums[i]);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_42_largest_sum_contiguous_subarray_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int[] nums = { -2, 1, -3, 4, -1, 2, 1, -5, 4 };
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int res = slt.maxSubArray(nums);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* File: sfo_43_total_number_of_1_in_integers_from_1_to_n_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_43_total_number_of_1_in_integers_from_1_to_n_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int countDigitOne(int n) {
|
||||
int digit = 1, res = 0;
|
||||
int high = n / 10, cur = n % 10, low = 0;
|
||||
while(high != 0 || cur != 0) {
|
||||
if(cur == 0) res += high * digit;
|
||||
else if(cur == 1) res += high * digit + low + 1;
|
||||
else res += (high + 1) * digit;
|
||||
low += cur * digit;
|
||||
cur = high % 10;
|
||||
high /= 10;
|
||||
digit *= 10;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_43_total_number_of_1_in_integers_from_1_to_n_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int n = 12;
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int res = slt.countDigitOne(n);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
38
java/sfo_44_nth_digit_s1/sfo_44_nth_digit_s1.java
Normal file
38
java/sfo_44_nth_digit_s1/sfo_44_nth_digit_s1.java
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* File: sfo_44_nth_digit_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_44_nth_digit_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int findNthDigit(int n) {
|
||||
int digit = 1;
|
||||
long start = 1;
|
||||
long count = 9;
|
||||
while (n > count) { // 1.
|
||||
n -= count;
|
||||
start *= 10;
|
||||
digit += 1;
|
||||
count = digit * start * 9;
|
||||
}
|
||||
long num = start + (n - 1) / digit; // 2.
|
||||
return Long.toString(num).charAt((n - 1) % digit) - '0'; // 3.
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_44_nth_digit_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int n = 3;
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int res = slt.findNthDigit(n);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* File: sfo_45_arrange_an_array_into_the_smallest_number_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_45_arrange_an_array_into_the_smallest_number_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public String minNumber(int[] nums) {
|
||||
String[] strs = new String[nums.length];
|
||||
for(int i = 0; i < nums.length; i++)
|
||||
strs[i] = String.valueOf(nums[i]);
|
||||
quickSort(strs, 0, strs.length - 1);
|
||||
StringBuilder res = new StringBuilder();
|
||||
for(String s : strs)
|
||||
res.append(s);
|
||||
return res.toString();
|
||||
}
|
||||
void quickSort(String[] strs, int l, int r) {
|
||||
if(l >= r) return;
|
||||
int i = l, j = r;
|
||||
String tmp = strs[i];
|
||||
while(i < j) {
|
||||
while((strs[j] + strs[l]).compareTo(strs[l] + strs[j]) >= 0 && i < j) j--;
|
||||
while((strs[i] + strs[l]).compareTo(strs[l] + strs[i]) <= 0 && i < j) i++;
|
||||
tmp = strs[i];
|
||||
strs[i] = strs[j];
|
||||
strs[j] = tmp;
|
||||
}
|
||||
strs[i] = strs[l];
|
||||
strs[l] = tmp;
|
||||
quickSort(strs, l, i - 1);
|
||||
quickSort(strs, i + 1, r);
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_45_arrange_an_array_into_the_smallest_number_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int[] nums = { 3, 30, 34, 5, 9 };
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
String res = slt.minNumber(nums);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* File: sfo_45_arrange_an_array_into_the_smallest_number_s2.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_45_arrange_an_array_into_the_smallest_number_s2;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public String minNumber(int[] nums) {
|
||||
String[] strs = new String[nums.length];
|
||||
for(int i = 0; i < nums.length; i++)
|
||||
strs[i] = String.valueOf(nums[i]);
|
||||
Arrays.sort(strs, (x, y) -> (x + y).compareTo(y + x));
|
||||
StringBuilder res = new StringBuilder();
|
||||
for(String s : strs)
|
||||
res.append(s);
|
||||
return res.toString();
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_45_arrange_an_array_into_the_smallest_number_s2 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int[] nums = { 3, 30, 34, 5, 9 };
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
String res = slt.minNumber(nums);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* File: sfo_46_translate_numbers_into_strings_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_46_translate_numbers_into_strings_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int translateNum(int num) {
|
||||
String s = String.valueOf(num);
|
||||
int a = 1, b = 1;
|
||||
for(int i = 2; i <= s.length(); i++) {
|
||||
String tmp = s.substring(i - 2, i);
|
||||
int c = tmp.compareTo("10") >= 0 && tmp.compareTo("25") <= 0 ? a + b : a;
|
||||
b = a;
|
||||
a = c;
|
||||
}
|
||||
return a;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_46_translate_numbers_into_strings_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int num = 12258;
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int res = slt.translateNum(num);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* File: sfo_46_translate_numbers_into_strings_s2.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_46_translate_numbers_into_strings_s2;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int translateNum(int num) {
|
||||
String s = String.valueOf(num);
|
||||
int a = 1, b = 1;
|
||||
for(int i = s.length() - 2; i > -1; i--) {
|
||||
String tmp = s.substring(i, i + 2);
|
||||
int c = tmp.compareTo("10") >= 0 && tmp.compareTo("25") <= 0 ? a + b : a;
|
||||
b = a;
|
||||
a = c;
|
||||
}
|
||||
return a;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_46_translate_numbers_into_strings_s2 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int num = 12258;
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int res = slt.translateNum(num);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* File: sfo_46_translate_numbers_into_strings_s3.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_46_translate_numbers_into_strings_s3;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int translateNum(int num) {
|
||||
int a = 1, b = 1, x, y = num % 10;
|
||||
while(num > 9) {
|
||||
num /= 10;
|
||||
x = num % 10;
|
||||
int tmp = 10 * x + y;
|
||||
int c = (tmp >= 10 && tmp <= 25) ? a + b : a;
|
||||
b = a;
|
||||
a = c;
|
||||
y = x;
|
||||
}
|
||||
return a;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_46_translate_numbers_into_strings_s3 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int num = 12258;
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int res = slt.translateNum(num);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* File: sfo_47_the_maximum_value_of_gifts_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_47_the_maximum_value_of_gifts_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int maxValue(int[][] grid) {
|
||||
int m = grid.length, n = grid[0].length;
|
||||
for(int i = 0; i < m; i++) {
|
||||
for(int j = 0; j < n; j++) {
|
||||
if(i == 0 && j == 0) continue;
|
||||
if(i == 0) grid[i][j] += grid[i][j - 1] ;
|
||||
else if(j == 0) grid[i][j] += grid[i - 1][j];
|
||||
else grid[i][j] += Math.max(grid[i][j - 1], grid[i - 1][j]);
|
||||
}
|
||||
}
|
||||
return grid[m - 1][n - 1];
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_47_the_maximum_value_of_gifts_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int[][] grid = {
|
||||
{ 1, 3, 1 },
|
||||
{ 1, 5, 1 },
|
||||
{ 4, 2, 1 }
|
||||
};
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int res = slt.maxValue(grid);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* File: sfo_47_the_maximum_value_of_gifts_s2.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_47_the_maximum_value_of_gifts_s2;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int maxValue(int[][] grid) {
|
||||
int m = grid.length, n = grid[0].length;
|
||||
for(int j = 1; j < n; j++) // 初始化第一行
|
||||
grid[0][j] += grid[0][j - 1];
|
||||
for(int i = 1; i < m; i++) // 初始化第一列
|
||||
grid[i][0] += grid[i - 1][0];
|
||||
for(int i = 1; i < m; i++)
|
||||
for(int j = 1; j < n; j++)
|
||||
grid[i][j] += Math.max(grid[i][j - 1], grid[i - 1][j]);
|
||||
return grid[m - 1][n - 1];
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_47_the_maximum_value_of_gifts_s2 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int[][] grid = {
|
||||
{ 1, 3, 1 },
|
||||
{ 1, 5, 1 },
|
||||
{ 4, 2, 1 }
|
||||
};
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int res = slt.maxValue(grid);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* File: sfo_48_the_longest_substring_without_repeated_characters_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_48_the_longest_substring_without_repeated_characters_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int lengthOfLongestSubstring(String s) {
|
||||
Map<Character, Integer> dic = new HashMap<>();
|
||||
int res = 0, tmp = 0, len = s.length();
|
||||
for(int j = 0; j < len; j++) {
|
||||
int i = dic.getOrDefault(s.charAt(j), -1); // 获取索引 i
|
||||
dic.put(s.charAt(j), j); // 更新哈希表
|
||||
tmp = tmp < j - i ? tmp + 1 : j - i; // dp[j - 1] -> dp[j]
|
||||
res = Math.max(res, tmp); // max(dp[j - 1], dp[j])
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_48_the_longest_substring_without_repeated_characters_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
String s = "abcabcbb";
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int res = slt.lengthOfLongestSubstring(s);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* File: sfo_48_the_longest_substring_without_repeated_characters_s2.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_48_the_longest_substring_without_repeated_characters_s2;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int lengthOfLongestSubstring(String s) {
|
||||
int res = 0, tmp = 0, len = s.length();
|
||||
for(int j = 0; j < len; j++) {
|
||||
int i = j - 1;
|
||||
while(i >= 0 && s.charAt(i) != s.charAt(j)) i--; // 线性查找 i
|
||||
tmp = tmp < j - i ? tmp + 1 : j - i; // dp[j - 1] -> dp[j]
|
||||
res = Math.max(res, tmp); // max(dp[j - 1], dp[j])
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_48_the_longest_substring_without_repeated_characters_s2 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
String s = "abcabcbb";
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int res = slt.lengthOfLongestSubstring(s);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* File: sfo_48_the_longest_substring_without_repeated_characters_s3.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_48_the_longest_substring_without_repeated_characters_s3;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int lengthOfLongestSubstring(String s) {
|
||||
Map<Character, Integer> dic = new HashMap<>();
|
||||
int i = -1, res = 0, len = s.length();
|
||||
for(int j = 0; j < len; j++) {
|
||||
if(dic.containsKey(s.charAt(j)))
|
||||
i = Math.max(i, dic.get(s.charAt(j))); // 更新左指针 i
|
||||
dic.put(s.charAt(j), j); // 哈希表记录
|
||||
res = Math.max(res, j - i); // 更新结果
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_48_the_longest_substring_without_repeated_characters_s3 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
String s = "abcabcbb";
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int res = slt.lengthOfLongestSubstring(s);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
38
java/sfo_49_ugly_numbers_s1/sfo_49_ugly_numbers_s1.java
Normal file
38
java/sfo_49_ugly_numbers_s1/sfo_49_ugly_numbers_s1.java
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* File: sfo_49_ugly_numbers_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_49_ugly_numbers_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int nthUglyNumber(int n) {
|
||||
int a = 0, b = 0, c = 0;
|
||||
int[] dp = new int[n];
|
||||
dp[0] = 1;
|
||||
for(int i = 1; i < n; i++) {
|
||||
int n2 = dp[a] * 2, n3 = dp[b] * 3, n5 = dp[c] * 5;
|
||||
dp[i] = Math.min(Math.min(n2, n3), n5);
|
||||
if(dp[i] == n2) a++;
|
||||
if(dp[i] == n3) b++;
|
||||
if(dp[i] == n5) c++;
|
||||
}
|
||||
return dp[n - 1];
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_49_ugly_numbers_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int n = 10;
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int res = slt.nthUglyNumber(n);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* File: sfo_50_find_the_first_nonrepeating_character_in_a_string_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_50_find_the_first_nonrepeating_character_in_a_string_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public char firstUniqChar(String s) {
|
||||
HashMap<Character, Boolean> dic = new HashMap<>();
|
||||
char[] sc = s.toCharArray();
|
||||
for(char c : sc)
|
||||
dic.put(c, !dic.containsKey(c));
|
||||
for(char c : sc)
|
||||
if(dic.get(c)) return c;
|
||||
return ' ';
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_50_find_the_first_nonrepeating_character_in_a_string_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
String s = "abaccdeff";
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
char res = slt.firstUniqChar(s);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* File: sfo_50_find_the_first_nonrepeating_character_in_a_string_s2.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_50_find_the_first_nonrepeating_character_in_a_string_s2;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public char firstUniqChar(String s) {
|
||||
Map<Character, Boolean> dic = new LinkedHashMap<>();
|
||||
char[] sc = s.toCharArray();
|
||||
for(char c : sc)
|
||||
dic.put(c, !dic.containsKey(c));
|
||||
for(Map.Entry<Character, Boolean> d : dic.entrySet()){
|
||||
if(d.getValue()) return d.getKey();
|
||||
}
|
||||
return ' ';
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_50_find_the_first_nonrepeating_character_in_a_string_s2 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
String s = "abaccdeff";
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
char res = slt.firstUniqChar(s);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* File: sfo_51_reversed_pairs_in_an_array_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_51_reversed_pairs_in_an_array_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
int[] nums, tmp;
|
||||
public int reversePairs(int[] nums) {
|
||||
this.nums = nums;
|
||||
tmp = new int[nums.length];
|
||||
return mergeSort(0, nums.length - 1);
|
||||
}
|
||||
private int mergeSort(int l, int r) {
|
||||
// 终止条件
|
||||
if (l >= r) return 0;
|
||||
// 递归划分
|
||||
int m = (l + r) / 2;
|
||||
int res = mergeSort(l, m) + mergeSort(m + 1, r);
|
||||
// 合并阶段
|
||||
int i = l, j = m + 1;
|
||||
for (int k = l; k <= r; k++)
|
||||
tmp[k] = nums[k];
|
||||
for (int k = l; k <= r; k++) {
|
||||
if (i == m + 1)
|
||||
nums[k] = tmp[j++];
|
||||
else if (j == r + 1 || tmp[i] <= tmp[j])
|
||||
nums[k] = tmp[i++];
|
||||
else {
|
||||
nums[k] = tmp[j++];
|
||||
res += m - i + 1; // 统计逆序对
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_51_reversed_pairs_in_an_array_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int[] nums = { 7, 5, 6, 4 };
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int res = slt.reversePairs(nums);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* File: sfo_52_the_first_common_node_in_two_linked_lists_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_52_the_first_common_node_in_two_linked_lists_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
|
||||
ListNode A = headA, B = headB;
|
||||
while (A != B) {
|
||||
A = A != null ? A.next : headB;
|
||||
B = B != null ? B.next : headA;
|
||||
}
|
||||
return A;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_52_the_first_common_node_in_two_linked_lists_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
// Build two linked lists with intersection of 8
|
||||
// headA = 4 -> 1 -> 8 -> 4 -> 5
|
||||
// ↑
|
||||
// headB = 5 -> 0 -> 1
|
||||
ListNode headA = ListNode.arrToLinkedList(new int[] { 4, 1, 8, 4, 5 });
|
||||
ListNode headB = ListNode.arrToLinkedList(new int[] { 5, 0, 1, 8, 4, 5 });
|
||||
ListNode intersectA = ListNode.getListNode(headA, 8);
|
||||
ListNode intersectB = ListNode.getListNode(headB, 1);
|
||||
intersectB.next = intersectA; // Concatenate the two lists
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
ListNode res = slt.getIntersectionNode(headA, headB);
|
||||
System.out.println(res.val);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* File: sfo_53i_find_a_number_in_a_sorted_array_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_53i_find_a_number_in_a_sorted_array_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int search(int[] nums, int target) {
|
||||
// 搜索右边界 right
|
||||
int i = 0, j = nums.length - 1;
|
||||
while(i <= j) {
|
||||
int m = (i + j) / 2;
|
||||
if(nums[m] <= target) i = m + 1;
|
||||
else j = m - 1;
|
||||
}
|
||||
int right = i;
|
||||
// 若数组中无 target ,则提前返回
|
||||
if(j >= 0 && nums[j] != target) return 0;
|
||||
// 搜索左边界 right
|
||||
i = 0; j = nums.length - 1;
|
||||
while(i <= j) {
|
||||
int m = (i + j) / 2;
|
||||
if(nums[m] < target) i = m + 1;
|
||||
else j = m - 1;
|
||||
}
|
||||
int left = j;
|
||||
return right - left - 1;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_53i_find_a_number_in_a_sorted_array_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int[] nums = { 5, 7, 7, 8, 8, 10 };
|
||||
int target = 8;
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int res = slt.search(nums, target);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* File: sfo_53i_find_a_number_in_a_sorted_array_s2.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_53i_find_a_number_in_a_sorted_array_s2;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int search(int[] nums, int target) {
|
||||
return helper(nums, target) - helper(nums, target - 1);
|
||||
}
|
||||
int helper(int[] nums, int tar) {
|
||||
int i = 0, j = nums.length - 1;
|
||||
while(i <= j) {
|
||||
int m = (i + j) / 2;
|
||||
if(nums[m] <= tar) i = m + 1;
|
||||
else j = m - 1;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_53i_find_a_number_in_a_sorted_array_s2 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int[] nums = { 5, 7, 7, 8, 8, 10 };
|
||||
int target = 8;
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int res = slt.search(nums, target);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* File: sfo_53ii_the_missing_number_from_0_to_n1_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_53ii_the_missing_number_from_0_to_n1_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int missingNumber(int[] nums) {
|
||||
int i = 0, j = nums.length - 1;
|
||||
while(i <= j) {
|
||||
int m = (i + j) / 2;
|
||||
if(nums[m] == m) i = m + 1;
|
||||
else j = m - 1;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_53ii_the_missing_number_from_0_to_n1_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int[] nums = { 0, 1, 3 };
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int res = slt.missingNumber(nums);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* File: sfo_54_the_kth_largest_node_of_a_binary_search_tree_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_54_the_kth_largest_node_of_a_binary_search_tree_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
int res, k;
|
||||
public int kthLargest(TreeNode root, int k) {
|
||||
this.k = k;
|
||||
dfs(root);
|
||||
return res;
|
||||
}
|
||||
void dfs(TreeNode root) {
|
||||
if(root == null) return;
|
||||
dfs(root.right);
|
||||
if(k == 0) return;
|
||||
if(--k == 0) res = root.val;
|
||||
dfs(root.left);
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_54_the_kth_largest_node_of_a_binary_search_tree_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
TreeNode root = TreeNode.arrToTree(new Integer[] { 3, 1, 4, null, 2, null, null, null, null });
|
||||
int k = 1;
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int res = slt.kthLargest(root, k);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* File: sfo_55i_depth_of_a_binary_tree_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_55i_depth_of_a_binary_tree_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int maxDepth(TreeNode root) {
|
||||
if(root == null) return 0;
|
||||
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_55i_depth_of_a_binary_tree_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
TreeNode root = TreeNode.arrToTree(new Integer[] { 3, 9, 20, null, null, 15, 7, null, null, null, null });
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int res = slt.maxDepth(root);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* File: sfo_55i_depth_of_a_binary_tree_s2.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_55i_depth_of_a_binary_tree_s2;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int maxDepth(TreeNode root) {
|
||||
if(root == null) return 0;
|
||||
List<TreeNode> queue = new LinkedList<>() {{ add(root); }}, tmp;
|
||||
int res = 0;
|
||||
while(!queue.isEmpty()) {
|
||||
tmp = new LinkedList<>();
|
||||
for(TreeNode node : queue) {
|
||||
if(node.left != null) tmp.add(node.left);
|
||||
if(node.right != null) tmp.add(node.right);
|
||||
}
|
||||
queue = tmp;
|
||||
res++;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_55i_depth_of_a_binary_tree_s2 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
TreeNode root = TreeNode.arrToTree(new Integer[] { 3, 9, 20, null, null, 15, 7, null, null, null, null });
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int res = slt.maxDepth(root);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* File: sfo_55ii_balanced_binary_tree_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_55ii_balanced_binary_tree_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public boolean isBalanced(TreeNode root) {
|
||||
return recur(root) != -1;
|
||||
}
|
||||
|
||||
private int recur(TreeNode root) {
|
||||
if (root == null) return 0;
|
||||
int left = recur(root.left);
|
||||
if(left == -1) return -1;
|
||||
int right = recur(root.right);
|
||||
if(right == -1) return -1;
|
||||
return Math.abs(left - right) < 2 ? Math.max(left, right) + 1 : -1;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_55ii_balanced_binary_tree_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
TreeNode root = TreeNode.arrToTree(new Integer[] { 3, 9, 20, null, null, 15, 7, null, null, null, null });
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
boolean res = slt.isBalanced(root);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* File: sfo_55ii_balanced_binary_tree_s2.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_55ii_balanced_binary_tree_s2;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public boolean isBalanced(TreeNode root) {
|
||||
if (root == null) return true;
|
||||
return Math.abs(depth(root.left) - depth(root.right)) <= 1 && isBalanced(root.left) && isBalanced(root.right);
|
||||
}
|
||||
|
||||
private int depth(TreeNode root) {
|
||||
if (root == null) return 0;
|
||||
return Math.max(depth(root.left), depth(root.right)) + 1;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_55ii_balanced_binary_tree_s2 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
TreeNode root = TreeNode.arrToTree(new Integer[] { 3, 9, 20, null, null, 15, 7, null, null, null, null });
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
boolean res = slt.isBalanced(root);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* File: sfo_56i_single_number_i_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_56i_single_number_i_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int[] singleNumbers(int[] nums) {
|
||||
int x = 0, y = 0, n = 0, m = 1;
|
||||
for(int num : nums) // 1. 遍历异或
|
||||
n ^= num;
|
||||
while((n & m) == 0) // 2. 循环左移,计算 m
|
||||
m <<= 1;
|
||||
for(int num: nums) { // 3. 遍历 nums 分组
|
||||
if((num & m) != 0) x ^= num; // 4. 当 num & m != 0
|
||||
else y ^= num; // 4. 当 num & m == 0
|
||||
}
|
||||
return new int[] {x, y}; // 5. 返回出现一次的数字
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_56i_single_number_i_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int[] nums = { 4, 1, 4, 6 };
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int[] res = slt.singleNumbers(nums);
|
||||
System.out.println(Arrays.toString(res));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* File: sfo_56ii_single_number_ii_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_56ii_single_number_ii_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int singleNumber(int[] nums) {
|
||||
int ones = 0, twos = 0;
|
||||
for(int num : nums){
|
||||
ones = ones ^ num & ~twos;
|
||||
twos = twos ^ num & ~ones;
|
||||
}
|
||||
return ones;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_56ii_single_number_ii_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int[] nums = { 3, 4, 3, 3 };
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int res = slt.singleNumber(nums);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* File: sfo_56ii_single_number_ii_s2.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_56ii_single_number_ii_s2;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int singleNumber(int[] nums) {
|
||||
int[] counts = new int[32];
|
||||
for(int num : nums) {
|
||||
for(int i = 0; i < 32; i++) {
|
||||
counts[i] += num & 1; // 更新第 i 位 1 的个数之和
|
||||
num >>= 1; // 第 i 位 --> 第 i 位
|
||||
}
|
||||
}
|
||||
int res = 0, m = 3;
|
||||
for(int i = 31; i >= 0; i--) {
|
||||
res <<= 1;
|
||||
res |= counts[i] % m; // 恢复第 i 位
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_56ii_single_number_ii_s2 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int[] nums = { 3, 4, 3, 3 };
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int res = slt.singleNumber(nums);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* File: sfo_57_two_numbers_with_sum_s_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_57_two_numbers_with_sum_s_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int[] twoSum(int[] nums, int target) {
|
||||
int i = 0, j = nums.length - 1;
|
||||
while(i < j) {
|
||||
int s = nums[i] + nums[j];
|
||||
if(s < target) i++;
|
||||
else if(s > target) j--;
|
||||
else return new int[] { nums[i], nums[j] };
|
||||
}
|
||||
return new int[0];
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_57_two_numbers_with_sum_s_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int[] nums = { 2, 7, 11, 15 };
|
||||
int target = 9;
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int[] res = slt.twoSum(nums, target);
|
||||
System.out.println(Arrays.toString(res));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* File: sfo_57ii_consecutive_numbers_with_sum_s_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_57ii_consecutive_numbers_with_sum_s_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int[][] findContinuousSequence(int target) {
|
||||
int i = 1;
|
||||
double j = 2.0;
|
||||
List<int[]> res = new ArrayList<>();
|
||||
while(i < j) {
|
||||
j = (-1 + Math.sqrt(1 + 4 * (2 * target + (long) i * i - i))) / 2;
|
||||
if(i < j && j == (int)j) {
|
||||
int[] ans = new int[(int)j - i + 1];
|
||||
for(int k = i; k <= (int)j; k++)
|
||||
ans[k - i] = k;
|
||||
res.add(ans);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return res.toArray(new int[0][]);
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_57ii_consecutive_numbers_with_sum_s_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int target = 9;
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int[][] res = slt.findContinuousSequence(target);
|
||||
System.out.println(Arrays.deepToString(res));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* File: sfo_57ii_consecutive_numbers_with_sum_s_s2.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_57ii_consecutive_numbers_with_sum_s_s2;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public int[][] findContinuousSequence(int target) {
|
||||
int i = 1, j = 2, s = 3;
|
||||
List<int[]> res = new ArrayList<>();
|
||||
while(i < j) {
|
||||
if(s == target) {
|
||||
int[] ans = new int[j - i + 1];
|
||||
for(int k = i; k <= j; k++)
|
||||
ans[k - i] = k;
|
||||
res.add(ans);
|
||||
}
|
||||
if(s >= target) {
|
||||
s -= i;
|
||||
i++;
|
||||
} else {
|
||||
j++;
|
||||
s += j;
|
||||
}
|
||||
}
|
||||
return res.toArray(new int[0][]);
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_57ii_consecutive_numbers_with_sum_s_s2 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int target = 9;
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
int[][] res = slt.findContinuousSequence(target);
|
||||
System.out.println(Arrays.deepToString(res));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* File: sfo_58i_reverse_order_of_words_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_58i_reverse_order_of_words_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public String reverseWords(String s) {
|
||||
s = s.trim(); // 删除首尾空格
|
||||
int j = s.length() - 1, i = j;
|
||||
StringBuilder res = new StringBuilder();
|
||||
while (i >= 0) {
|
||||
while (i >= 0 && s.charAt(i) != ' ') i--; // 搜索首个空格
|
||||
res.append(s.substring(i + 1, j + 1) + " "); // 添加单词
|
||||
while (i >= 0 && s.charAt(i) == ' ') i--; // 跳过单词间空格
|
||||
j = i; // j 指向下个单词的尾字符
|
||||
}
|
||||
return res.toString().trim(); // 转化为字符串并返回
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_58i_reverse_order_of_words_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
String s = "the sky is blue";
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
String res = slt.reverseWords(s);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* File: sfo_58i_reverse_order_of_words_s2.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_58i_reverse_order_of_words_s2;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public String reverseWords(String s) {
|
||||
String[] strs = s.trim().split(" "); // 删除首尾空格,分割字符串
|
||||
StringBuilder res = new StringBuilder();
|
||||
for (int i = strs.length - 1; i >= 0; i--) { // 倒序遍历单词列表
|
||||
if(strs[i].equals("")) continue; // 遇到空单词则跳过
|
||||
res.append(strs[i] + " "); // 将单词拼接至 StringBuilder
|
||||
}
|
||||
return res.toString().trim(); // 转化为字符串,删除尾部空格,并返回
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_58i_reverse_order_of_words_s2 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
String s = "the sky is blue";
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
String res = slt.reverseWords(s);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* File: sfo_58ii_left_rotation_of_a_string_s1.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_58ii_left_rotation_of_a_string_s1;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public String reverseLeftWords(String s, int n) {
|
||||
return s.substring(n, s.length()) + s.substring(0, n);
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_58ii_left_rotation_of_a_string_s1 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
String s = "abcdefg";
|
||||
int n = 2;
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
String res = slt.reverseLeftWords(s, n);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* File: sfo_58ii_left_rotation_of_a_string_s2.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_58ii_left_rotation_of_a_string_s2;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public String reverseLeftWords(String s, int n) {
|
||||
StringBuilder res = new StringBuilder();
|
||||
for(int i = n; i < s.length(); i++)
|
||||
res.append(s.charAt(i));
|
||||
for(int i = 0; i < n; i++)
|
||||
res.append(s.charAt(i));
|
||||
return res.toString();
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_58ii_left_rotation_of_a_string_s2 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
String s = "abcdefg";
|
||||
int n = 2;
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
String res = slt.reverseLeftWords(s, n);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* File: sfo_58ii_left_rotation_of_a_string_s3.java
|
||||
* Created Time: 2021-12-09
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package sfo_58ii_left_rotation_of_a_string_s3;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
// ===== Solution Code =====
|
||||
class Solution {
|
||||
public String reverseLeftWords(String s, int n) {
|
||||
StringBuilder res = new StringBuilder();
|
||||
for(int i = n; i < n + s.length(); i++)
|
||||
res.append(s.charAt(i % s.length()));
|
||||
return res.toString();
|
||||
}
|
||||
}
|
||||
|
||||
public class sfo_58ii_left_rotation_of_a_string_s3 {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
String s = "abcdefg";
|
||||
int n = 2;
|
||||
// ====== Driver Code ======
|
||||
Solution slt = new Solution();
|
||||
String res = slt.reverseLeftWords(s, n);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user