diff --git "a/notes/10.1 \346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227.md" "b/notes/10.1 \346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227.md" index 63fd6d7fac656b8d2e6fd10105565f04a62545d5..fd9e836bc188a4f887393660c4e9f43c2431389e 100644 --- "a/notes/10.1 \346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227.md" +++ "b/notes/10.1 \346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227.md" @@ -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) ## 题目描述 diff --git "a/notes/10.2 \347\237\251\345\275\242\350\246\206\347\233\226.md" "b/notes/10.2 \347\237\251\345\275\242\350\246\206\347\233\226.md" index 56e244a57d7ab3675974fcc2da44430badf98cf1..01e833f70908add7dc816b81b7f708801d581346 100644 --- "a/notes/10.2 \347\237\251\345\275\242\350\246\206\347\233\226.md" +++ "b/notes/10.2 \347\237\251\345\275\242\350\246\206\347\233\226.md" @@ -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 @@

```java -public int RectCover(int n) { +public int rectCover(int n) { if (n <= 2) return n; int pre2 = 1, pre1 = 2; diff --git "a/notes/10.3 \350\267\263\345\217\260\351\230\266.md" "b/notes/10.3 \350\267\263\345\217\260\351\230\266.md" index 4cf0ee43f686f324e91f7194106aaf5a2143b2b2..2072cb90089ab56ba9ca44bae6361c9394833b56 100644 --- "a/notes/10.3 \350\267\263\345\217\260\351\230\266.md" +++ "b/notes/10.3 \350\267\263\345\217\260\351\230\266.md" @@ -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) ## 题目描述 diff --git "a/notes/10.4 \345\217\230\346\200\201\350\267\263\345\217\260\351\230\266.md" "b/notes/10.4 \345\217\230\346\200\201\350\267\263\345\217\260\351\230\266.md" index 605ce19637fd355c186d8d0cf87df3fa10d02dd2..f6099a1723cdfbe8768e5ec0d724e94dba1c1892 100644 --- "a/notes/10.4 \345\217\230\346\200\201\350\267\263\345\217\260\351\230\266.md" +++ "b/notes/10.4 \345\217\230\346\200\201\350\267\263\345\217\260\351\230\266.md" @@ -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++) diff --git "a/notes/12. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204.md" "b/notes/12. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204.md" index 913d9fe39ed817b7ea8d9a4da1ea0eaec6690541..157785374b3fa646159e43cbe22e5dbb78c8ee5e 100644 --- "a/notes/12. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204.md" +++ "b/notes/12. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204.md" @@ -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,46 +19,60 @@ 本题的输入是数组而不是矩阵(二维数组),因此需要先将数组转换成矩阵。 ```java -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) { - if (rows == 0 || cols == 0) return false; - this.rows = rows; - this.cols = cols; - boolean[][] marked = new boolean[rows][cols]; - char[][] matrix = buildMatrix(array); - for (int i = 0; i < rows; i++) - for (int j = 0; j < cols; j++) - if (backtracking(matrix, str, marked, 0, i, j)) - return true; +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 (String val, int rows, int cols, String path) { + if (rows == 0 || cols == 0) return false; + this.rows = rows; + this.cols = 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, pathList, marked, 0, i, j)) + return true; - return false; -} + return false; + } -private boolean backtracking(char[][] matrix, char[] str, - boolean[][] marked, int pathLen, int r, int c) { + private boolean backtracking(char[][] matrix, char[] pathList, + boolean[][] marked, int pathLen, int r, int c) { - if (pathLen == str.length) return true; - if (r < 0 || r >= rows || c < 0 || c >= cols - || matrix[r][c] != str[pathLen] || marked[r][c]) { + if (pathLen == pathList.length) return true; + if (r < 0 || r >= rows || c < 0 || c >= cols + || matrix[r][c] != pathList[pathLen] || marked[r][c]) { + return false; + } + marked[r][c] = true; + for (int[] n : next) + if (backtracking(matrix, pathList, marked, pathLen + 1, r + n[0], c + n[1])) + return true; + marked[r][c] = false; return false; } - marked[r][c] = true; - for (int[] n : next) - if (backtracking(matrix, str, marked, pathLen + 1, r + n[0], c + n[1])) - return true; - marked[r][c] = false; - return false; -} -private char[][] buildMatrix(char[] array) { - char[][] matrix = new char[rows][cols]; - for (int r = 0, idx = 0; r < rows; r++) - for (int c = 0; c < cols; c++) - matrix[r][c] = array[idx++]; - return matrix; + private char[][] buildMatrix(char[] array) { + char[][] matrix = new char[rows][cols]; + for (int r = 0, idx = 0; r < rows; r++) + for (int c = 0; c < cols; c++) + 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); + } } ``` diff --git "a/notes/13. \346\234\272\345\231\250\344\272\272\347\232\204\350\277\220\345\212\250\350\214\203\345\233\264.md" "b/notes/13. \346\234\272\345\231\250\344\272\272\347\232\204\350\277\220\345\212\250\350\214\203\345\233\264.md" index 766b1bd64b2f57730cb43b490df00c4e91513de8..dd96086ed2a9276e312bed3d616a96feac881c6e 100644 --- "a/notes/13. \346\234\272\345\231\250\344\272\272\347\232\204\350\277\220\345\212\250\350\214\203\345\233\264.md" +++ "b/notes/13. \346\234\272\345\231\250\344\272\272\347\232\204\350\277\220\345\212\250\350\214\203\345\233\264.md" @@ -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) ## 题目描述 diff --git "a/notes/14. \345\211\252\347\273\263\345\255\220.md" "b/notes/14. \345\211\252\347\273\263\345\255\220.md" index 16ca2493af554ce8cdd379198c77c4cf387ce30b..9108b6353673d3371e0a8d1daca7bd05f153ac67 100644 --- "a/notes/14. \345\211\252\347\273\263\345\255\220.md" +++ "b/notes/14. \345\211\252\347\273\263\345\255\220.md" @@ -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++) diff --git "a/notes/18.2 \345\210\240\351\231\244\351\223\276\350\241\250\344\270\255\351\207\215\345\244\215\347\232\204\347\273\223\347\202\271.md" "b/notes/18.2 \345\210\240\351\231\244\351\223\276\350\241\250\344\270\255\351\207\215\345\244\215\347\232\204\347\273\223\347\202\271.md" index 57e247bfbbfe19461772ce8e66cfc9bb5d2b2a58..5d1f871ba8394397815b28eb22ffd55aa20a9c15 100644 --- "a/notes/18.2 \345\210\240\351\231\244\351\223\276\350\241\250\344\270\255\351\207\215\345\244\215\347\232\204\347\273\223\347\202\271.md" +++ "b/notes/18.2 \345\210\240\351\231\244\351\223\276\350\241\250\344\270\255\351\207\215\345\244\215\347\232\204\347\273\223\347\202\271.md" @@ -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) ## 题目描述 diff --git "a/notes/19. \346\255\243\345\210\231\350\241\250\350\276\276\345\274\217\345\214\271\351\205\215.md" "b/notes/19. \346\255\243\345\210\231\350\241\250\350\276\276\345\274\217\345\214\271\351\205\215.md" index 97de47d7b620a6b3fab57ed987508af3d401b0d3..a0ad0053b2bb9ebf717726d3f7b00a3984fe73e4 100644 --- "a/notes/19. \346\255\243\345\210\231\350\241\250\350\276\276\345\274\217\345\214\271\351\205\215.md" +++ "b/notes/19. \346\255\243\345\210\231\350\241\250\350\276\276\345\274\217\345\214\271\351\205\215.md" @@ -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 diff --git "a/notes/20. \350\241\250\347\244\272\346\225\260\345\200\274\347\232\204\345\255\227\347\254\246\344\270\262.md" "b/notes/20. \350\241\250\347\244\272\346\225\260\345\200\274\347\232\204\345\255\227\347\254\246\344\270\262.md" index c03369bafea236b6452a1b92462a638c1cf2ed3f..da4b4bf685fd2d91d8e22a624a1522779cd677a6 100644 --- "a/notes/20. \350\241\250\347\244\272\346\225\260\345\200\274\347\232\204\345\255\227\347\254\246\344\270\262.md" +++ "b/notes/20. \350\241\250\347\244\272\346\225\260\345\200\274\347\232\204\345\255\227\347\254\246\344\270\262.md" @@ -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+)?"); } diff --git "a/notes/21. \350\260\203\346\225\264\346\225\260\347\273\204\351\241\272\345\272\217\344\275\277\345\245\207\346\225\260\344\275\215\344\272\216\345\201\266\346\225\260\345\211\215\351\235\242.md" "b/notes/21. \350\260\203\346\225\264\346\225\260\347\273\204\351\241\272\345\272\217\344\275\277\345\245\207\346\225\260\344\275\215\344\272\216\345\201\266\346\225\260\345\211\215\351\235\242.md" index 7b2e1699da4e1b55c2037e525bfba28eedfd25d9..13ca98e5c7a6f2cc70d337bde46e9ba9912f15a6 100644 --- "a/notes/21. \350\260\203\346\225\264\346\225\260\347\273\204\351\241\272\345\272\217\344\275\277\345\245\207\346\225\260\344\275\215\344\272\216\345\201\266\346\225\260\345\211\215\351\235\242.md" +++ "b/notes/21. \350\260\203\346\225\264\346\225\260\347\273\204\351\241\272\345\272\217\344\275\277\345\245\207\346\225\260\344\275\215\344\272\216\345\201\266\346\225\260\345\211\215\351\235\242.md" @@ -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(N2),空间复杂度 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) { diff --git "a/notes/22. \351\223\276\350\241\250\344\270\255\345\200\222\346\225\260\347\254\254 K \344\270\252\347\273\223\347\202\271.md" "b/notes/22. \351\223\276\350\241\250\344\270\255\345\200\222\346\225\260\347\254\254 K \344\270\252\347\273\223\347\202\271.md" index f77964e7fd4cb5db5db7613399b58e5fa784ec4a..aa00d274af269b4e111eb5792a84da2145cb573d 100644 --- "a/notes/22. \351\223\276\350\241\250\344\270\255\345\200\222\346\225\260\347\254\254 K \344\270\252\347\273\223\347\202\271.md" +++ "b/notes/22. \351\223\276\350\241\250\344\270\255\345\200\222\346\225\260\347\254\254 K \344\270\252\347\273\223\347\202\271.md" @@ -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) ## 解题思路 diff --git "a/notes/27. \344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217.md" "b/notes/27. \344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217.md" index f9ca746a503e5701639ff403b90e425f1c9432b1..18b5f0fa1886dcc5ad269004cabf11bbbd5e5396 100644 --- "a/notes/27. \344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217.md" +++ "b/notes/27. \344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217.md" @@ -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) { diff --git "a/notes/5. \346\233\277\346\215\242\347\251\272\346\240\274.md" "b/notes/5. \346\233\277\346\215\242\347\251\272\346\240\274.md" index 211ae0c6a339a81f734dc887761c316eef93b17f..bd8086161c4e3dcff019bbb02b4c241f0245a29c 100644 --- "a/notes/5. \346\233\277\346\215\242\347\251\272\346\240\274.md" +++ "b/notes/5. \346\233\277\346\215\242\347\251\272\346\240\274.md" @@ -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) ## 题目描述 diff --git "a/notes/56. \346\225\260\347\273\204\344\270\255\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227.md" "b/notes/56. \346\225\260\347\273\204\344\270\255\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227.md" index 21c1c3dc779562d5326da21efe5b12b4545ce1bd..4ab74ad3c3e29d0915e9c7cfd4c5d8b8849034fb 100644 --- "a/notes/56. \346\225\260\347\273\204\344\270\255\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227.md" +++ "b/notes/56. \346\225\260\347\273\204\344\270\255\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227.md" @@ -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; } ```