Skip to content

Commit 7cfdb12

Browse files
committed
Merge remote-tracking branch 'origin/master'
# Conflicts: # .idea/workspace.xml # README.md
2 parents 67c2adc + 522f0c7 commit 7cfdb12

File tree

15 files changed

+458
-150
lines changed

15 files changed

+458
-150
lines changed

.idea/workspace.xml

Lines changed: 128 additions & 149 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
### 链表操作
1010

1111
* [q2_两数相加](/src/链表操作/q2_两数相加)
12+
* [q19_删除链表的倒数第N个节点](/src/链表操作/q19_删除链表的倒数第N个节点)
13+
* [q61_旋转链表](/src/链表操作/q61_旋转链表)
1214
* [q138_复制带随机指针的链表](/src/链表操作/q138_复制带随机指针的链表)
1315
* [q206_反转链表](/src/链表操作/q206_反转链表)
1416

@@ -52,10 +54,13 @@
5254

5355
* [q54_螺旋矩阵](/src/数组操作/q54_螺旋矩阵)
5456
* [q73_矩阵置零](/src/数组操作/q73_矩阵置零)
57+
* [q945_使数组唯一的最小增量](/src/数组操作/q945_使数组唯一的最小增量)
5558

5659
### 栈相关
5760

5861
* [q20_有效的括号](/src/栈相关/q20_有效的括号)
62+
* [q32_最长有效括号](/src/栈相关/q32_最长有效括号)
63+
* [q155_最小栈](/src/栈相关/q155_最小栈)
5964
* [q224_基本计算器](/src/栈相关/q224_基本计算器)
6065
* [q316_去除重复字母](/src/栈相关/q316_去除重复字母)
6166

@@ -68,18 +73,22 @@
6873

6974
* [q21_合并两个有序链表](/src/递归/q21_合并两个有序链表)
7075
* [q101_对称二叉树](/src/递归/q101_对称二叉树)
76+
* [q104_二叉树的最大深度](/src/递归/q104_二叉树的最大深度)
7177
* [q226_翻转二叉树](/src/递归/q226_翻转二叉树)
7278
* [q236_二叉树的最近公共祖先](/src/递归/q236_二叉树的最近公共祖先)
7379

7480
### 分治法/二分法
7581

7682
* [q23_合并K个排序链表](/src/分治法/q23_合并K个排序链表)
7783
* [q33_搜索旋转排序数组](/src/分治法/q33_搜索旋转排序数组)
84+
* [q34_在排序数组中查找元素的第一个和最后一个位置](/src/分治法/q34_在排序数组中查找元素的第一个和最后一个位置)
7885

7986
### 动态规划
8087

8188
* [q5_最长回文子串](/src/动态规划/q5_最长回文子串)
8289
* [q53_最大子序和](/src/动态规划/q53_最大子序和)
90+
* [q64_最小路径和](/src/动态规划/q64_最小路径和)
91+
* [q70_爬楼梯](/src/动态规划/q70_爬楼梯)
8392
* [q118_杨辉三角](/src/动态规划/q118_杨辉三角)
8493
* [q300_最长上升子序列](/src/动态规划/q300_最长上升子序列)
8594
* [q746_使用最小花费爬楼梯](/src/动态规划/q746_使用最小花费爬楼梯)

Rocket.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,6 @@ TCP是一个双向通信协议,通信双方都有能力发送信息,并接
601601

602602
因为当Server端收到Client端的SYN连接请求报文后,可以直接发送SYN+ACK报文。其中ACK报文是用来应答的,SYN报文是用来同步的。但是关闭连接时,当Server端收到FIN报文时,很可能并不会立即关闭SOCKET,所以只能先回复一个ACK报文,告诉Client端,"你发的FIN报文我收到了"。只有等到我Server端所有的报文都发送完了,我才能发送FIN报文,因此不能一起发送。故需要四步握手。
603603

604-
605604
## 数据结构与算法
606605

607606
### 排序算法
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package 分治法.q34_在排序数组中查找元素的第一个和最后一个位置;
2+
3+
/**
4+
* 二分法 o(log(n))
5+
*/
6+
public class Solution {
7+
8+
public int[] searchRange(int[] nums, int target) {
9+
if (nums == null || nums.length < 1) {
10+
return new int[]{-1, -1};
11+
}
12+
int midIndex = find(0, nums.length - 1, nums, target);
13+
int[] rs = new int[2];
14+
rs[0] = midIndex;
15+
rs[1] = midIndex;
16+
if (midIndex == -1) {
17+
return rs;
18+
}
19+
while (nums[rs[0]] == target && rs[0] > 0) {
20+
int temp = find(0, rs[0] - 1, nums, target);
21+
if (temp == -1) {
22+
break;
23+
} else {
24+
rs[0] = temp;
25+
}
26+
}
27+
28+
while (nums[rs[1]] == target && rs[1] < nums.length - 1) {
29+
int temp = find(rs[1] + 1, nums.length - 1, nums, target);
30+
if (temp == -1) {
31+
break;
32+
} else {
33+
rs[1] = temp;
34+
}
35+
}
36+
return rs;
37+
}
38+
39+
public int find(int beginIndex, int endIndex, int[] nums, int target) {
40+
if (beginIndex == endIndex) {
41+
if (nums[beginIndex] == target) {
42+
return beginIndex;
43+
} else {
44+
return -1;
45+
}
46+
}
47+
int mid = (endIndex - beginIndex) / 2 + beginIndex;
48+
if (nums[mid] > target) {
49+
return find(beginIndex, mid, nums, target);
50+
} else if (nums[mid] < target) {
51+
return find(mid + 1, endIndex, nums, target);
52+
} else {
53+
return mid;
54+
}
55+
}
56+
57+
public static void main(String[] args) {
58+
new Solution().searchRange(new int[]{2, 2}, 2);
59+
}
60+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package 动态规划.q64_最小路径和;
2+
3+
/**
4+
* 动态规划 dp(j)=grid(i,j)+min(dp(j),dp(j+1)) o(m*n)
5+
*/
6+
public class Solution {
7+
8+
public int minPathSum(int[][] grid) {
9+
int[] dp = new int[grid[0].length];
10+
for (int i = grid.length - 1; i >= 0; i--) {
11+
for (int j = grid[0].length - 1; j >= 0; j--) {
12+
if (i == grid.length - 1 && j != grid[0].length - 1) {
13+
dp[j] = grid[i][j] + dp[j + 1];
14+
} else if (j == grid[0].length - 1 && i != grid.length - 1) {
15+
dp[j] = grid[i][j] + dp[j];
16+
} else if (j != grid[0].length - 1 && i != grid.length - 1) {
17+
dp[j] = grid[i][j] + Math.min(dp[j], dp[j + 1]);
18+
19+
} else {
20+
dp[j] = grid[i][j];
21+
}
22+
}
23+
}
24+
return dp[0];
25+
}
26+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package 动态规划.q70_爬楼梯;
2+
3+
/**
4+
* 动态规划 dp[i]表示到达第i阶的方法总数dp[i]=dp[i−1]+dp[i−2] o(n)
5+
*/
6+
public class Solution {
7+
8+
public int climbStairs(int n) {
9+
if (n == 1) {
10+
return 1;
11+
}
12+
int[] dp = new int[n + 1];
13+
dp[1] = 1;
14+
dp[2] = 2;
15+
for (int i = 3; i <= n; i++) {
16+
dp[i] = dp[i - 1] + dp[i - 2];
17+
}
18+
return dp[n];
19+
}
20+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package 数组操作.q945_使数组唯一的最小增量;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* 先排序再遍历一次 o(n*log(n))
7+
*/
8+
public class Solution {
9+
10+
public int minIncrementForUnique(int[] A) {
11+
if (A == null || A.length == 0 || A.length == 1) {
12+
return 0;
13+
}
14+
15+
int rs = 0;
16+
Arrays.sort(A);
17+
18+
int t = A[0];
19+
for (int i = 1; i < A.length; i++) {
20+
if (A[i] <= t) {
21+
rs = rs + t - A[i] + 1;
22+
A[i] = t + 1;
23+
}
24+
t = A[i];
25+
}
26+
return rs;
27+
}
28+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package 栈相关.q155_最小栈;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* 不使用辅助栈,每次push两个元素
7+
*/
8+
public class MinStack {
9+
10+
private Stack<Integer> stack;
11+
12+
public MinStack() {
13+
stack = new Stack<>();
14+
}
15+
16+
public void push(int x) {
17+
if (stack.isEmpty()) {
18+
stack.push(x);
19+
stack.push(x);
20+
} else {
21+
int tmp = stack.peek();
22+
stack.push(x);
23+
if (tmp < x) {
24+
stack.push(tmp);
25+
} else {
26+
stack.push(x);
27+
}
28+
}
29+
}
30+
31+
public void pop() {
32+
stack.pop();
33+
stack.pop();
34+
}
35+
36+
public int top() {
37+
return stack.get(stack.size() - 2);
38+
}
39+
40+
public int getMin() {
41+
return stack.peek();
42+
}
43+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package 栈相关.q32_最长有效括号;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* 利用索引栈 o(n)
7+
*/
8+
public class Solution {
9+
10+
public int longestValidParentheses(String s) {
11+
if (s == null || s.length() < 2) {
12+
return 0;
13+
}
14+
15+
int maxLen = 0;
16+
Stack<Integer> stack = new Stack<>();
17+
stack.push(-1);
18+
for (int i = 0; i < s.length(); i++) {
19+
char temp = s.charAt(i);
20+
if (temp == '(') {
21+
stack.push(i);
22+
} else {
23+
stack.pop();
24+
if (stack.empty()) {
25+
stack.push(i);
26+
} else {
27+
maxLen = Math.max(maxLen, i - stack.peek());
28+
}
29+
}
30+
}
31+
32+
return maxLen;
33+
}
34+
35+
public static void main(String[] args) {
36+
System.out.println(new Solution().longestValidParentheses(")()())"));
37+
}
38+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package 递归.q104_二叉树的最大深度;
2+
3+
/**
4+
* 递归 o(n)
5+
*/
6+
public class Solution {
7+
8+
public int maxDepth(TreeNode root) {
9+
if (root == null) {
10+
return 0;
11+
} else {
12+
int leftHeight = maxDepth(root.left);
13+
int rightHeight = maxDepth(root.right);
14+
return Math.max(leftHeight, rightHeight) + 1;
15+
}
16+
}
17+
}

0 commit comments

Comments
 (0)