auto commit
This commit is contained in:
parent
6027156d73
commit
156f7a67f4
@ -2,7 +2,7 @@
|
||||
|
||||
## 题目链接
|
||||
|
||||
[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)
|
||||
[牛客网](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)
|
||||
|
||||
## 题目描述
|
||||
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
## 题目链接
|
||||
|
||||
[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)
|
||||
[牛客网](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)
|
||||
|
||||
## 题目描述
|
||||
|
||||
@ -27,7 +27,7 @@
|
||||
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/508c6e52-9f93-44ed-b6b9-e69050e14807.jpg" width="370px"> </div><br>
|
||||
|
||||
```java
|
||||
public int RectCover(int n) {
|
||||
public int rectCover(int n) {
|
||||
if (n <= 2)
|
||||
return n;
|
||||
int pre2 = 1, pre1 = 2;
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
## 题目链接
|
||||
|
||||
[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)
|
||||
[牛客网](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)
|
||||
|
||||
## 题目描述
|
||||
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
## 题目链接
|
||||
|
||||
[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)
|
||||
[牛客网](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)
|
||||
|
||||
## 题目描述
|
||||
|
||||
@ -15,7 +15,7 @@
|
||||
### 动态规划
|
||||
|
||||
```java
|
||||
public int JumpFloorII(int target) {
|
||||
public int jumpFloorII(int target) {
|
||||
int[] dp = new int[target];
|
||||
Arrays.fill(dp, 1);
|
||||
for (int i = 1; i < target; i++)
|
||||
|
@ -1,6 +1,6 @@
|
||||
# 12. 矩阵中的路径
|
||||
|
||||
[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)
|
||||
[牛客网](https://www.nowcoder.com/practice/69fe7a584f0a445da1b6652978de5c38?tpId=13&tqId=11218&tab=answerKey&from=cyc_github)
|
||||
|
||||
## 题目描述
|
||||
|
||||
@ -19,36 +19,39 @@
|
||||
本题的输入是数组而不是矩阵(二维数组),因此需要先将数组转换成矩阵。
|
||||
|
||||
```java
|
||||
public class Solution {
|
||||
private final static int[][] next = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
|
||||
private int rows;
|
||||
private int cols;
|
||||
|
||||
public boolean hasPath(char[] array, int rows, int cols, char[] str) {
|
||||
public boolean hasPath (String val, int rows, int cols, String path) {
|
||||
if (rows == 0 || cols == 0) return false;
|
||||
this.rows = rows;
|
||||
this.cols = cols;
|
||||
boolean[][] marked = new boolean[rows][cols];
|
||||
char[] array = val.toCharArray();
|
||||
char[][] matrix = buildMatrix(array);
|
||||
char[] pathList = path.toCharArray();
|
||||
boolean[][] marked = new boolean[rows][cols];
|
||||
for (int i = 0; i < rows; i++)
|
||||
for (int j = 0; j < cols; j++)
|
||||
if (backtracking(matrix, str, marked, 0, i, j))
|
||||
if (backtracking(matrix, pathList, marked, 0, i, j))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean backtracking(char[][] matrix, char[] str,
|
||||
private boolean backtracking(char[][] matrix, char[] pathList,
|
||||
boolean[][] marked, int pathLen, int r, int c) {
|
||||
|
||||
if (pathLen == str.length) return true;
|
||||
if (pathLen == pathList.length) return true;
|
||||
if (r < 0 || r >= rows || c < 0 || c >= cols
|
||||
|| matrix[r][c] != str[pathLen] || marked[r][c]) {
|
||||
|| matrix[r][c] != pathList[pathLen] || marked[r][c]) {
|
||||
|
||||
return false;
|
||||
}
|
||||
marked[r][c] = true;
|
||||
for (int[] n : next)
|
||||
if (backtracking(matrix, str, marked, pathLen + 1, r + n[0], c + n[1]))
|
||||
if (backtracking(matrix, pathList, marked, pathLen + 1, r + n[0], c + n[1]))
|
||||
return true;
|
||||
marked[r][c] = false;
|
||||
return false;
|
||||
@ -61,4 +64,15 @@ private char[][] buildMatrix(char[] array) {
|
||||
matrix[r][c] = array[idx++];
|
||||
return matrix;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Solution solution = new Solution();
|
||||
String val = "ABCESFCSADEE";
|
||||
int rows = 3;
|
||||
int cols = 4;
|
||||
String path = "ABCCED";
|
||||
boolean res = solution.hasPath(val, rows, cols, path);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
@ -1,6 +1,6 @@
|
||||
# 13. 机器人的运动范围
|
||||
|
||||
[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)
|
||||
[牛客网](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)
|
||||
|
||||
## 题目描述
|
||||
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
## 题目链接
|
||||
|
||||
[Leetcode](https://leetcode.com/problems/integer-break/description/)
|
||||
[牛客网](https://www.nowcoder.com/practice/57d85990ba5b440ab888fc72b0751bf8?tpId=13&tqId=33257&tab=answerKey&from=cyc_github)
|
||||
|
||||
## 题目描述
|
||||
|
||||
@ -37,7 +37,7 @@ return 36 (10 = 3 + 3 + 4)
|
||||
继续拆成更大的绳子可以发现都比拆成 2 和 3 的效果更差,因此我们只考虑将绳子拆成 2 和 3,并且优先拆成 3,当拆到绳子长度 n 等于 4 时,也就是出现 3+1,此时只能拆成 2+2。
|
||||
|
||||
```java
|
||||
public int integerBreak(int n) {
|
||||
public int cutRope(int n) {
|
||||
if (n < 2)
|
||||
return 0;
|
||||
if (n == 2)
|
||||
@ -55,7 +55,7 @@ public int integerBreak(int n) {
|
||||
### 动态规划
|
||||
|
||||
```java
|
||||
public int integerBreak(int n) {
|
||||
public int cutRope(int n) {
|
||||
int[] dp = new int[n + 1];
|
||||
dp[1] = 1;
|
||||
for (int i = 2; i <= n; i++)
|
||||
|
@ -1,6 +1,6 @@
|
||||
# 18.2 删除链表中重复的结点
|
||||
|
||||
[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)
|
||||
[牛客网](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)
|
||||
|
||||
## 题目描述
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
# 19. 正则表达式匹配
|
||||
|
||||
[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)
|
||||
[牛客网](https://www.nowcoder.com/practice/28970c15befb4ff3a264189087b99ad4?tpId=13&tqId=11205&tab=answerKey&from=cyc_github)
|
||||
|
||||
## 题目描述
|
||||
|
||||
@ -13,22 +13,22 @@
|
||||
应该注意到,'.' 是用来当做一个任意字符,而 '\*' 是用来重复前面的字符。这两个的作用不同,不能把 '.' 的作用和 '\*' 进行类比,从而把它当成重复前面字符一次。
|
||||
|
||||
```java
|
||||
public boolean match(char[] str, char[] pattern) {
|
||||
public boolean match(String str, String pattern) {
|
||||
|
||||
int m = str.length, n = pattern.length;
|
||||
int m = str.length(), n = pattern.length();
|
||||
boolean[][] dp = new boolean[m + 1][n + 1];
|
||||
|
||||
dp[0][0] = true;
|
||||
for (int i = 1; i <= n; i++)
|
||||
if (pattern[i - 1] == '*')
|
||||
if (pattern.charAt(i - 1) == '*')
|
||||
dp[0][i] = dp[0][i - 2];
|
||||
|
||||
for (int i = 1; i <= m; i++)
|
||||
for (int j = 1; j <= n; j++)
|
||||
if (str[i - 1] == pattern[j - 1] || pattern[j - 1] == '.')
|
||||
if (str.charAt(i - 1) == pattern.charAt(j - 1) || pattern.charAt(j - 1) == '.')
|
||||
dp[i][j] = dp[i - 1][j - 1];
|
||||
else if (pattern[j - 1] == '*')
|
||||
if (pattern[j - 2] == str[i - 1] || pattern[j - 2] == '.') {
|
||||
else if (pattern.charAt(j - 1) == '*')
|
||||
if (pattern.charAt(j - 2) == str.charAt(i - 1) || pattern.charAt(j - 2) == '.') {
|
||||
dp[i][j] |= dp[i][j - 1]; // a* counts as single a
|
||||
dp[i][j] |= dp[i - 1][j]; // a* counts as multiple a
|
||||
dp[i][j] |= dp[i][j - 2]; // a* counts as empty
|
||||
|
@ -1,6 +1,6 @@
|
||||
# 20. 表示数值的字符串
|
||||
|
||||
[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)
|
||||
[牛客网](https://www.nowcoder.com/practice/e69148f8528c4039ad89bb2546fd4ff8?tpId=13&tqId=11206&tab=answerKey&from=cyc_github)
|
||||
|
||||
## 题目描述
|
||||
|
||||
@ -41,8 +41,8 @@ false
|
||||
```
|
||||
|
||||
```java
|
||||
public boolean isNumeric(char[] str) {
|
||||
if (str == null || str.length == 0)
|
||||
public boolean isNumeric (String str) {
|
||||
if (str == null || str.length() == 0)
|
||||
return false;
|
||||
return new String(str).matches("[+-]?\\d*(\\.\\d+)?([eE][+-]?\\d+)?");
|
||||
}
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
## 题目链接
|
||||
|
||||
[牛客网](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)
|
||||
[牛客网](https://www.nowcoder.com/practice/ef1f53ef31ca408cada5093c8780f44b?tpId=13&tqId=11166&tab=answerKey&from=cyc_github)
|
||||
|
||||
## 题目描述
|
||||
|
||||
@ -15,7 +15,7 @@
|
||||
方法一:创建一个新数组,时间复杂度 O(N),空间复杂度 O(N)。
|
||||
|
||||
```java
|
||||
public void reOrderArray(int[] nums) {
|
||||
public int[] reOrderArray (int[] nums) {
|
||||
// 奇数个数
|
||||
int oddCnt = 0;
|
||||
for (int x : nums)
|
||||
@ -29,6 +29,7 @@ public void reOrderArray(int[] nums) {
|
||||
else
|
||||
nums[j++] = num;
|
||||
}
|
||||
return nums;
|
||||
}
|
||||
|
||||
private boolean isEven(int x) {
|
||||
@ -39,7 +40,7 @@ private boolean isEven(int x) {
|
||||
方法二:使用冒泡思想,每次都将当前偶数上浮到当前最右边。时间复杂度 O(N<sup>2</sup>),空间复杂度 O(1),时间换空间。
|
||||
|
||||
```java
|
||||
public void reOrderArray(int[] nums) {
|
||||
public int[] reOrderArray(int[] nums) {
|
||||
int N = nums.length;
|
||||
for (int i = N - 1; i > 0; i--) {
|
||||
for (int j = 0; j < i; j++) {
|
||||
@ -48,6 +49,7 @@ public void reOrderArray(int[] nums) {
|
||||
}
|
||||
}
|
||||
}
|
||||
return nums;
|
||||
}
|
||||
|
||||
private boolean isEven(int x) {
|
||||
|
@ -1,6 +1,6 @@
|
||||
# 22. 链表中倒数第 K 个结点
|
||||
|
||||
[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)
|
||||
[牛客网](https://www.nowcoder.com/practice/886370fe658f41b498d40fb34ae76ff9?tpId=13&tqId=11167&tab=answerKey&from=cyc_github)
|
||||
|
||||
## 解题思路
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
# 27. 二叉树的镜像
|
||||
|
||||
[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)
|
||||
[牛客网](https://www.nowcoder.com/practice/a9d0ecbacef9410ca97463e4a5c83be7?tpId=13&tqId=11171&tab=answerKey&from=cyc_github)
|
||||
|
||||
## 题目描述
|
||||
|
||||
@ -9,12 +9,13 @@
|
||||
## 解题思路
|
||||
|
||||
```java
|
||||
public void Mirror(TreeNode root) {
|
||||
public TreeNode Mirror(TreeNode root) {
|
||||
if (root == null)
|
||||
return;
|
||||
return root;
|
||||
swap(root);
|
||||
Mirror(root.left);
|
||||
Mirror(root.right);
|
||||
return root;
|
||||
}
|
||||
|
||||
private void swap(TreeNode root) {
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
## 题目链接
|
||||
|
||||
[牛客网](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)
|
||||
[牛客网](https://www.nowcoder.com/practice/0e26e5551f2b489b9f58bc83aa4b6c68?tpId=13&tqId=11155&tab=answerKey&from=cyc_github)
|
||||
|
||||
## 题目描述
|
||||
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
## 题目链接
|
||||
|
||||
[牛客网](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)
|
||||
[牛客网](https://www.nowcoder.com/practice/389fc1c3d3be4479a154f63f495abff8?tpId=13&tqId=11193&tab=answerKey&from=cyc_github)
|
||||
|
||||
## 题目描述
|
||||
|
||||
@ -19,16 +19,27 @@
|
||||
下面的解法中,num1 和 num2 数组的第一个元素是用来保持返回值的... 实际开发中不推荐这种返回值的方式。
|
||||
|
||||
```java
|
||||
public void FindNumsAppearOnce(int[] nums, int num1[], int num2[]) {
|
||||
public int[] FindNumsAppearOnce (int[] nums) {
|
||||
int[] res = new int[2];
|
||||
int diff = 0;
|
||||
for (int num : nums)
|
||||
diff ^= num;
|
||||
diff &= -diff;
|
||||
for (int num : nums) {
|
||||
if ((num & diff) == 0)
|
||||
num1[0] ^= num;
|
||||
res[0] ^= num;
|
||||
else
|
||||
num2[0] ^= num;
|
||||
res[1] ^= num;
|
||||
}
|
||||
if (res[0] > res[1]) {
|
||||
swap(res);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
private void swap(int[] nums) {
|
||||
int t = nums[0];
|
||||
nums[0] = nums[1];
|
||||
nums[1] = t;
|
||||
}
|
||||
```
|
||||
|
Loading…
Reference in New Issue
Block a user