Skip to content

Commit 156f7a6

Browse files
committed
auto commit
1 parent 6027156 commit 156f7a6

15 files changed

+96
-68
lines changed

notes/10.1 斐波那契数列.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 题目链接
44

5-
[NowCoder](https://www.nowcoder.com/practice/c6c7742f5ba7442aada113136ddea0c3?tpId=13&tqId=11160&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking&from=cyc_github)
5+
[牛客网](https://www.nowcoder.com/practice/c6c7742f5ba7442aada113136ddea0c3?tpId=13&tqId=11160&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking&from=cyc_github)
66

77
## 题目描述
88

notes/10.2 矩形覆盖.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 题目链接
44

5-
[NowCoder](https://www.nowcoder.com/practice/72a5a919508a4251859fb2cfb987a0e6?tpId=13&tqId=11163&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking&from=cyc_github)
5+
[牛客网](https://www.nowcoder.com/practice/72a5a919508a4251859fb2cfb987a0e6?tpId=13&tqId=11163&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking&from=cyc_github)
66

77
## 题目描述
88

@@ -27,7 +27,7 @@
2727
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/508c6e52-9f93-44ed-b6b9-e69050e14807.jpg" width="370px"> </div><br>
2828

2929
```java
30-
public int RectCover(int n) {
30+
public int rectCover(int n) {
3131
if (n <= 2)
3232
return n;
3333
int pre2 = 1, pre1 = 2;

notes/10.3 跳台阶.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 题目链接
44

5-
[NowCoder](https://www.nowcoder.com/practice/8c82a5b80378478f9484d87d1c5f12a4?tpId=13&tqId=11161&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking&from=cyc_github)
5+
[牛客网](https://www.nowcoder.com/practice/8c82a5b80378478f9484d87d1c5f12a4?tpId=13&tqId=11161&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking&from=cyc_github)
66

77
## 题目描述
88

notes/10.4 变态跳台阶.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 题目链接
44

5-
[NowCoder](https://www.nowcoder.com/practice/22243d016f6b47f2a6928b4313c85387?tpId=13&tqId=11162&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking&from=cyc_github)
5+
[牛客网](https://www.nowcoder.com/practice/22243d016f6b47f2a6928b4313c85387?tpId=13&tqId=11162&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking&from=cyc_github)
66

77
## 题目描述
88

@@ -15,7 +15,7 @@
1515
### 动态规划
1616

1717
```java
18-
public int JumpFloorII(int target) {
18+
public int jumpFloorII(int target) {
1919
int[] dp = new int[target];
2020
Arrays.fill(dp, 1);
2121
for (int i = 1; i < target; i++)

notes/12. 矩阵中的路径.md

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 12. 矩阵中的路径
22

3-
[NowCoder](https://www.nowcoder.com/practice/c61c6999eecb4b8f88a98f66b273a3cc?tpId=13&tqId=11218&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking&from=cyc_github)
3+
[牛客网](https://www.nowcoder.com/practice/69fe7a584f0a445da1b6652978de5c38?tpId=13&tqId=11218&tab=answerKey&from=cyc_github)
44

55
## 题目描述
66

@@ -19,46 +19,60 @@
1919
本题的输入是数组而不是矩阵(二维数组),因此需要先将数组转换成矩阵。
2020

2121
```java
22-
private final static int[][] next = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
23-
private int rows;
24-
private int cols;
25-
26-
public boolean hasPath(char[] array, int rows, int cols, char[] str) {
27-
if (rows == 0 || cols == 0) return false;
28-
this.rows = rows;
29-
this.cols = cols;
30-
boolean[][] marked = new boolean[rows][cols];
31-
char[][] matrix = buildMatrix(array);
32-
for (int i = 0; i < rows; i++)
33-
for (int j = 0; j < cols; j++)
34-
if (backtracking(matrix, str, marked, 0, i, j))
35-
return true;
22+
public class Solution {
23+
private final static int[][] next = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
24+
private int rows;
25+
private int cols;
26+
27+
public boolean hasPath (String val, int rows, int cols, String path) {
28+
if (rows == 0 || cols == 0) return false;
29+
this.rows = rows;
30+
this.cols = cols;
31+
char[] array = val.toCharArray();
32+
char[][] matrix = buildMatrix(array);
33+
char[] pathList = path.toCharArray();
34+
boolean[][] marked = new boolean[rows][cols];
35+
for (int i = 0; i < rows; i++)
36+
for (int j = 0; j < cols; j++)
37+
if (backtracking(matrix, pathList, marked, 0, i, j))
38+
return true;
3639

37-
return false;
38-
}
40+
return false;
41+
}
3942

40-
private boolean backtracking(char[][] matrix, char[] str,
41-
boolean[][] marked, int pathLen, int r, int c) {
43+
private boolean backtracking(char[][] matrix, char[] pathList,
44+
boolean[][] marked, int pathLen, int r, int c) {
4245

43-
if (pathLen == str.length) return true;
44-
if (r < 0 || r >= rows || c < 0 || c >= cols
45-
|| matrix[r][c] != str[pathLen] || marked[r][c]) {
46+
if (pathLen == pathList.length) return true;
47+
if (r < 0 || r >= rows || c < 0 || c >= cols
48+
|| matrix[r][c] != pathList[pathLen] || marked[r][c]) {
4649

50+
return false;
51+
}
52+
marked[r][c] = true;
53+
for (int[] n : next)
54+
if (backtracking(matrix, pathList, marked, pathLen + 1, r + n[0], c + n[1]))
55+
return true;
56+
marked[r][c] = false;
4757
return false;
4858
}
49-
marked[r][c] = true;
50-
for (int[] n : next)
51-
if (backtracking(matrix, str, marked, pathLen + 1, r + n[0], c + n[1]))
52-
return true;
53-
marked[r][c] = false;
54-
return false;
55-
}
5659

57-
private char[][] buildMatrix(char[] array) {
58-
char[][] matrix = new char[rows][cols];
59-
for (int r = 0, idx = 0; r < rows; r++)
60-
for (int c = 0; c < cols; c++)
61-
matrix[r][c] = array[idx++];
62-
return matrix;
60+
private char[][] buildMatrix(char[] array) {
61+
char[][] matrix = new char[rows][cols];
62+
for (int r = 0, idx = 0; r < rows; r++)
63+
for (int c = 0; c < cols; c++)
64+
matrix[r][c] = array[idx++];
65+
return matrix;
66+
}
67+
68+
public static void main(String[] args) {
69+
Solution solution = new Solution();
70+
String val = "ABCESFCSADEE";
71+
int rows = 3;
72+
int cols = 4;
73+
String path = "ABCCED";
74+
boolean res = solution.hasPath(val, rows, cols, path);
75+
System.out.println(res);
76+
}
6377
}
6478
```

notes/13. 机器人的运动范围.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 13. 机器人的运动范围
22

3-
[NowCoder](https://www.nowcoder.com/practice/6e5207314b5241fb83f2329e89fdecc8?tpId=13&tqId=11219&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking&from=cyc_github)
3+
[牛客网](https://www.nowcoder.com/practice/6e5207314b5241fb83f2329e89fdecc8?tpId=13&tqId=11219&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking&from=cyc_github)
44

55
## 题目描述
66

notes/14. 剪绳子.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 题目链接
44

5-
[Leetcode](https://leetcode.com/problems/integer-break/description/)
5+
[牛客网](https://www.nowcoder.com/practice/57d85990ba5b440ab888fc72b0751bf8?tpId=13&tqId=33257&tab=answerKey&from=cyc_github)
66

77
## 题目描述
88

@@ -37,7 +37,7 @@ return 36 (10 = 3 + 3 + 4)
3737
继续拆成更大的绳子可以发现都比拆成 2 和 3 的效果更差,因此我们只考虑将绳子拆成 2 和 3,并且优先拆成 3,当拆到绳子长度 n 等于 4 时,也就是出现 3+1,此时只能拆成 2+2。
3838

3939
```java
40-
public int integerBreak(int n) {
40+
public int cutRope(int n) {
4141
if (n < 2)
4242
return 0;
4343
if (n == 2)
@@ -55,7 +55,7 @@ public int integerBreak(int n) {
5555
### 动态规划
5656

5757
```java
58-
public int integerBreak(int n) {
58+
public int cutRope(int n) {
5959
int[] dp = new int[n + 1];
6060
dp[1] = 1;
6161
for (int i = 2; i <= n; i++)

notes/18.2 删除链表中重复的结点.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 18.2 删除链表中重复的结点
22

3-
[NowCoder](https://www.nowcoder.com/practice/fc533c45b73a41b0b44ccba763f866ef?tpId=13&tqId=11209&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking&from=cyc_github)
3+
[牛客网](https://www.nowcoder.com/practice/fc533c45b73a41b0b44ccba763f866ef?tpId=13&tqId=11209&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking&from=cyc_github)
44

55
## 题目描述
66

notes/19. 正则表达式匹配.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 19. 正则表达式匹配
22

3-
[NowCoder](https://www.nowcoder.com/practice/45327ae22b7b413ea21df13ee7d6429c?tpId=13&tqId=11205&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking&from=cyc_github)
3+
[牛客网](https://www.nowcoder.com/practice/28970c15befb4ff3a264189087b99ad4?tpId=13&tqId=11205&tab=answerKey&from=cyc_github)
44

55
## 题目描述
66

@@ -13,22 +13,22 @@
1313
应该注意到,'.' 是用来当做一个任意字符,而 '\*' 是用来重复前面的字符。这两个的作用不同,不能把 '.' 的作用和 '\*' 进行类比,从而把它当成重复前面字符一次。
1414

1515
```java
16-
public boolean match(char[] str, char[] pattern) {
16+
public boolean match(String str, String pattern) {
1717

18-
int m = str.length, n = pattern.length;
18+
int m = str.length(), n = pattern.length();
1919
boolean[][] dp = new boolean[m + 1][n + 1];
2020

2121
dp[0][0] = true;
2222
for (int i = 1; i <= n; i++)
23-
if (pattern[i - 1] == '*')
23+
if (pattern.charAt(i - 1) == '*')
2424
dp[0][i] = dp[0][i - 2];
2525

2626
for (int i = 1; i <= m; i++)
2727
for (int j = 1; j <= n; j++)
28-
if (str[i - 1] == pattern[j - 1] || pattern[j - 1] == '.')
28+
if (str.charAt(i - 1) == pattern.charAt(j - 1) || pattern.charAt(j - 1) == '.')
2929
dp[i][j] = dp[i - 1][j - 1];
30-
else if (pattern[j - 1] == '*')
31-
if (pattern[j - 2] == str[i - 1] || pattern[j - 2] == '.') {
30+
else if (pattern.charAt(j - 1) == '*')
31+
if (pattern.charAt(j - 2) == str.charAt(i - 1) || pattern.charAt(j - 2) == '.') {
3232
dp[i][j] |= dp[i][j - 1]; // a* counts as single a
3333
dp[i][j] |= dp[i - 1][j]; // a* counts as multiple a
3434
dp[i][j] |= dp[i][j - 2]; // a* counts as empty

notes/20. 表示数值的字符串.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 20. 表示数值的字符串
22

3-
[NowCoder](https://www.nowcoder.com/practice/6f8c901d091949a5837e24bb82a731f2?tpId=13&tqId=11206&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking&from=cyc_github)
3+
[牛客网](https://www.nowcoder.com/practice/e69148f8528c4039ad89bb2546fd4ff8?tpId=13&tqId=11206&tab=answerKey&from=cyc_github)
44

55
## 题目描述
66

@@ -41,8 +41,8 @@ false
4141
```
4242

4343
```java
44-
public boolean isNumeric(char[] str) {
45-
if (str == null || str.length == 0)
44+
public boolean isNumeric (String str) {
45+
if (str == null || str.length() == 0)
4646
return false;
4747
return new String(str).matches("[+-]?\\d*(\\.\\d+)?([eE][+-]?\\d+)?");
4848
}

notes/21. 调整数组顺序使奇数位于偶数前面.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 题目链接
44

5-
[牛客网](https://www.nowcoder.com/practice/beb5aa231adc45b2a5dcc5b62c93f593?tpId=13&tqId=11166&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking&from=cyc_github)
5+
[牛客网](https://www.nowcoder.com/practice/ef1f53ef31ca408cada5093c8780f44b?tpId=13&tqId=11166&tab=answerKey&from=cyc_github)
66

77
## 题目描述
88

@@ -15,7 +15,7 @@
1515
方法一:创建一个新数组,时间复杂度 O(N),空间复杂度 O(N)。
1616

1717
```java
18-
public void reOrderArray(int[] nums) {
18+
public int[] reOrderArray (int[] nums) {
1919
// 奇数个数
2020
int oddCnt = 0;
2121
for (int x : nums)
@@ -29,6 +29,7 @@ public void reOrderArray(int[] nums) {
2929
else
3030
nums[j++] = num;
3131
}
32+
return nums;
3233
}
3334

3435
private boolean isEven(int x) {
@@ -39,7 +40,7 @@ private boolean isEven(int x) {
3940
方法二:使用冒泡思想,每次都将当前偶数上浮到当前最右边。时间复杂度 O(N<sup>2</sup>),空间复杂度 O(1),时间换空间。
4041

4142
```java
42-
public void reOrderArray(int[] nums) {
43+
public int[] reOrderArray(int[] nums) {
4344
int N = nums.length;
4445
for (int i = N - 1; i > 0; i--) {
4546
for (int j = 0; j < i; j++) {
@@ -48,6 +49,7 @@ public void reOrderArray(int[] nums) {
4849
}
4950
}
5051
}
52+
return nums;
5153
}
5254

5355
private boolean isEven(int x) {

notes/22. 链表中倒数第 K 个结点.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 22. 链表中倒数第 K 个结点
22

3-
[NowCoder](https://www.nowcoder.com/practice/529d3ae5a407492994ad2a246518148a?tpId=13&tqId=11167&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking&from=cyc_github)
3+
[牛客网](https://www.nowcoder.com/practice/886370fe658f41b498d40fb34ae76ff9?tpId=13&tqId=11167&tab=answerKey&from=cyc_github)
44

55
## 解题思路
66

notes/27. 二叉树的镜像.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 27. 二叉树的镜像
22

3-
[NowCoder](https://www.nowcoder.com/practice/564f4c26aa584921bc75623e48ca3011?tpId=13&tqId=11171&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking&from=cyc_github)
3+
[牛客网](https://www.nowcoder.com/practice/a9d0ecbacef9410ca97463e4a5c83be7?tpId=13&tqId=11171&tab=answerKey&from=cyc_github)
44

55
## 题目描述
66

@@ -9,12 +9,13 @@
99
## 解题思路
1010

1111
```java
12-
public void Mirror(TreeNode root) {
12+
public TreeNode Mirror(TreeNode root) {
1313
if (root == null)
14-
return;
14+
return root;
1515
swap(root);
1616
Mirror(root.left);
1717
Mirror(root.right);
18+
return root;
1819
}
1920

2021
private void swap(TreeNode root) {

notes/5. 替换空格.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 题目链接
44

5-
[牛客网](https://www.nowcoder.com/practice/4060ac7e3e404ad1a894ef3e17650423?tpId=13&tqId=11155&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking&from=cyc_github)
5+
[牛客网](https://www.nowcoder.com/practice/0e26e5551f2b489b9f58bc83aa4b6c68?tpId=13&tqId=11155&tab=answerKey&from=cyc_github)
66

77
## 题目描述
88

notes/56. 数组中只出现一次的数字.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 题目链接
44

5-
[牛客网](https://www.nowcoder.com/practice/e02fdb54d7524710a7d664d082bb7811?tpId=13&tqId=11193&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking&from=cyc_github)
5+
[牛客网](https://www.nowcoder.com/practice/389fc1c3d3be4479a154f63f495abff8?tpId=13&tqId=11193&tab=answerKey&from=cyc_github)
66

77
## 题目描述
88

@@ -19,16 +19,27 @@
1919
下面的解法中,num1 和 num2 数组的第一个元素是用来保持返回值的... 实际开发中不推荐这种返回值的方式。
2020

2121
```java
22-
public void FindNumsAppearOnce(int[] nums, int num1[], int num2[]) {
22+
public int[] FindNumsAppearOnce (int[] nums) {
23+
int[] res = new int[2];
2324
int diff = 0;
2425
for (int num : nums)
2526
diff ^= num;
2627
diff &= -diff;
2728
for (int num : nums) {
2829
if ((num & diff) == 0)
29-
num1[0] ^= num;
30+
res[0] ^= num;
3031
else
31-
num2[0] ^= num;
32+
res[1] ^= num;
3233
}
34+
if (res[0] > res[1]) {
35+
swap(res);
36+
}
37+
return res;
38+
}
39+
40+
private void swap(int[] nums) {
41+
int t = nums[0];
42+
nums[0] = nums[1];
43+
nums[1] = t;
3344
}
3445
```

0 commit comments

Comments
 (0)