diff --git a/problems/0037.解数独.md b/problems/0037.解数独.md index 6a9f69bd..5f3f881c 100644 --- a/problems/0037.解数独.md +++ b/problems/0037.解数独.md @@ -366,40 +366,56 @@ class Solution: """ Do not return anything, modify board in-place instead. """ - self.backtracking(board) + row_used = [set() for _ in range(9)] + col_used = [set() for _ in range(9)] + box_used = [set() for _ in range(9)] + for row in range(9): + for col in range(9): + num = board[row][col] + if num == ".": + continue + row_used[row].add(num) + col_used[col].add(num) + box_used[(row // 3) * 3 + col // 3].add(num) + self.backtracking(0, 0, board, row_used, col_used, box_used) - def backtracking(self, board: List[List[str]]) -> bool: - # 若有解,返回True;若无解,返回False - for i in range(len(board)): # 遍历行 - for j in range(len(board[0])): # 遍历列 - # 若空格内已有数字,跳过 - if board[i][j] != '.': continue - for k in range(1, 10): - if self.is_valid(i, j, k, board): - board[i][j] = str(k) - if self.backtracking(board): return True - board[i][j] = '.' - # 若数字1-9都不能成功填入空格,返回False无解 - return False - return True # 有解 + def backtracking( + self, + row: int, + col: int, + board: List[List[str]], + row_used: List[List[int]], + col_used: List[List[int]], + box_used: List[List[int]], + ) -> bool: + if row == 9: + return True - def is_valid(self, row: int, col: int, val: int, board: List[List[str]]) -> bool: - # 判断同一行是否冲突 - for i in range(9): - if board[row][i] == str(val): - return False - # 判断同一列是否冲突 - for j in range(9): - if board[j][col] == str(val): - return False - # 判断同一九宫格是否有冲突 - start_row = (row // 3) * 3 - start_col = (col // 3) * 3 - for i in range(start_row, start_row + 3): - for j in range(start_col, start_col + 3): - if board[i][j] == str(val): - return False - return True + next_row, next_col = (row, col + 1) if col < 8 else (row + 1, 0) + if board[row][col] != ".": + return self.backtracking( + next_row, next_col, board, row_used, col_used, box_used + ) + + for num in map(str, range(1, 10)): + if ( + num not in row_used[row] + and num not in col_used[col] + and num not in box_used[(row // 3) * 3 + col // 3] + ): + board[row][col] = num + row_used[row].add(num) + col_used[col].add(num) + box_used[(row // 3) * 3 + col // 3].add(num) + if self.backtracking( + next_row, next_col, board, row_used, col_used, box_used + ): + return True + board[row][col] = "." + row_used[row].remove(num) + col_used[col].remove(num) + box_used[(row // 3) * 3 + col // 3].remove(num) + return False ``` ### Go diff --git a/problems/0376.摆动序列.md b/problems/0376.摆动序列.md index 9cf1ed8c..e2ea9904 100644 --- a/problems/0376.摆动序列.md +++ b/problems/0376.摆动序列.md @@ -72,7 +72,7 @@ #### 情况一:上下坡中有平坡 -例如 [1,2,2,2,1]这样的数组,如图: +例如 [1,2,2,2,2,1]这样的数组,如图: ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230106170449.png) diff --git a/problems/0922.按奇偶排序数组II.md b/problems/0922.按奇偶排序数组II.md index 1ac6800c..28680dbf 100644 --- a/problems/0922.按奇偶排序数组II.md +++ b/problems/0922.按奇偶排序数组II.md @@ -11,9 +11,9 @@ [力扣题目链接](https://leetcode.cn/problems/sort-array-by-parity-ii/) -给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数。 +给定一个非负整数数组 nums, nums 中一半整数是奇数,一半整数是偶数。 -对数组进行排序,以便当 A[i] 为奇数时,i 也是奇数;当 A[i] 为偶数时, i 也是偶数。 +对数组进行排序,以便当 nums[i] 为奇数时,i 也是奇数;当 nums[i] 为偶数时, i 也是偶数。 你可以返回任何满足上述条件的数组作为答案。 @@ -35,17 +35,17 @@ ```CPP class Solution { public: - vector sortArrayByParityII(vector& A) { - vector even(A.size() / 2); // 初始化就确定数组大小,节省开销 - vector odd(A.size() / 2); - vector result(A.size()); + vector sortArrayByParityII(vector& nums) { + vector even(nums.size() / 2); // 初始化就确定数组大小,节省开销 + vector odd(nums.size() / 2); + vector result(nums.size()); int evenIndex = 0; int oddIndex = 0; int resultIndex = 0; - // 把A数组放进偶数数组,和奇数数组 - for (int i = 0; i < A.size(); i++) { - if (A[i] % 2 == 0) even[evenIndex++] = A[i]; - else odd[oddIndex++] = A[i]; + // 把nums数组放进偶数数组,和奇数数组 + for (int i = 0; i < nums.size(); i++) { + if (nums[i] % 2 == 0) even[evenIndex++] = nums[i]; + else odd[oddIndex++] = nums[i]; } // 把偶数数组,奇数数组分别放进result数组中 for (int i = 0; i < evenIndex; i++) { @@ -62,22 +62,22 @@ public: ### 方法二 -以上代码我是建了两个辅助数组,而且A数组还相当于遍历了两次,用辅助数组的好处就是思路清晰,优化一下就是不用这两个辅助树,代码如下: +以上代码我是建了两个辅助数组,而且nums数组还相当于遍历了两次,用辅助数组的好处就是思路清晰,优化一下就是不用这两个辅助数组,代码如下: ```CPP class Solution { public: - vector sortArrayByParityII(vector& A) { - vector result(A.size()); + vector sortArrayByParityII(vector& nums) { + vector result(nums.size()); int evenIndex = 0; // 偶数下标 int oddIndex = 1; // 奇数下标 - for (int i = 0; i < A.size(); i++) { - if (A[i] % 2 == 0) { - result[evenIndex] = A[i]; + for (int i = 0; i < nums.size(); i++) { + if (nums[i] % 2 == 0) { + result[evenIndex] = nums[i]; evenIndex += 2; } else { - result[oddIndex] = A[i]; + result[oddIndex] = nums[i]; oddIndex += 2; } } @@ -96,15 +96,15 @@ public: ```CPP class Solution { public: - vector sortArrayByParityII(vector& A) { + vector sortArrayByParityII(vector& nums) { int oddIndex = 1; - for (int i = 0; i < A.size(); i += 2) { - if (A[i] % 2 == 1) { // 在偶数位遇到了奇数 - while(A[oddIndex] % 2 != 0) oddIndex += 2; // 在奇数位找一个偶数 - swap(A[i], A[oddIndex]); // 替换 + for (int i = 0; i < nums.size(); i += 2) { + if (nums[i] % 2 == 1) { // 在偶数位遇到了奇数 + while(nums[oddIndex] % 2 != 0) oddIndex += 2; // 在奇数位找一个偶数 + swap(nums[i], nums[oddIndex]); // 替换 } } - return A; + return nums; } }; ``` @@ -253,6 +253,37 @@ func sortArrayByParityII(nums []int) []int { } return result; } + +// 方法二 +func sortArrayByParityII(nums []int) []int { + result := make([]int, len(nums)) + evenIndex := 0 // 偶数下标 + oddIndex := 1 // 奇数下标 + for _, v := range nums { + if v % 2 == 0 { + result[evenIndex] = v + evenIndex += 2 + } else { + result[oddIndex] = v + oddIndex += 2 + } + } + return result +} + +// 方法三 +func sortArrayByParityII(nums []int) []int { + oddIndex := 1 + for i := 0; i < len(nums); i += 2 { + if nums[i] % 2 == 1 { // 在偶数位遇到了奇数 + for nums[oddIndex] % 2 != 0 { + oddIndex += 2 // 在奇数位找一个偶数 + } + nums[i], nums[oddIndex] = nums[oddIndex], nums[i] + } + } + return nums +} ``` ### JavaScript