Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c865c46

Browse files
authored
Merge branch 'youngyangyang04:master' into master
2 parents fb9186f + 4d36947 commit c865c46

File tree

3 files changed

+105
-58
lines changed

3 files changed

+105
-58
lines changed

‎problems/0037.解数独.md

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -366,40 +366,56 @@ class Solution:
366366
"""
367367
Do not return anything, modify board in-place instead.
368368
"""
369-
self.backtracking(board)
370-
371-
def backtracking(self, board: List[List[str]]) -> bool:
372-
# 若有解,返回True;若无解,返回False
373-
for i in range(len(board)): # 遍历行
374-
for j in range(len(board[0])): # 遍历列
375-
# 若空格内已有数字,跳过
376-
if board[i][j] != '.': continue
377-
for k in range(1, 10):
378-
if self.is_valid(i, j, k, board):
379-
board[i][j] = str(k)
380-
if self.backtracking(board): return True
381-
board[i][j] = '.'
382-
# 若数字1-9都不能成功填入空格,返回False无解
383-
return False
384-
return True # 有解
385-
386-
def is_valid(self, row: int, col: int, val: int, board: List[List[str]]) -> bool:
387-
# 判断同一行是否冲突
388-
for i in range(9):
389-
if board[row][i] == str(val):
390-
return False
391-
# 判断同一列是否冲突
392-
for j in range(9):
393-
if board[j][col] == str(val):
394-
return False
395-
# 判断同一九宫格是否有冲突
396-
start_row = (row // 3) * 3
397-
start_col = (col // 3) * 3
398-
for i in range(start_row, start_row + 3):
399-
for j in range(start_col, start_col + 3):
400-
if board[i][j] == str(val):
401-
return False
402-
return True
369+
row_used = [set() for _ in range(9)]
370+
col_used = [set() for _ in range(9)]
371+
box_used = [set() for _ in range(9)]
372+
for row in range(9):
373+
for col in range(9):
374+
num = board[row][col]
375+
if num == ".":
376+
continue
377+
row_used[row].add(num)
378+
col_used[col].add(num)
379+
box_used[(row // 3) * 3 + col // 3].add(num)
380+
self.backtracking(0, 0, board, row_used, col_used, box_used)
381+
382+
def backtracking(
383+
self,
384+
row: int,
385+
col: int,
386+
board: List[List[str]],
387+
row_used: List[List[int]],
388+
col_used: List[List[int]],
389+
box_used: List[List[int]],
390+
) -> bool:
391+
if row == 9:
392+
return True
393+
394+
next_row, next_col = (row, col + 1) if col < 8 else (row + 1, 0)
395+
if board[row][col] != ".":
396+
return self.backtracking(
397+
next_row, next_col, board, row_used, col_used, box_used
398+
)
399+
400+
for num in map(str, range(1, 10)):
401+
if (
402+
num not in row_used[row]
403+
and num not in col_used[col]
404+
and num not in box_used[(row // 3) * 3 + col // 3]
405+
):
406+
board[row][col] = num
407+
row_used[row].add(num)
408+
col_used[col].add(num)
409+
box_used[(row // 3) * 3 + col // 3].add(num)
410+
if self.backtracking(
411+
next_row, next_col, board, row_used, col_used, box_used
412+
):
413+
return True
414+
board[row][col] = "."
415+
row_used[row].remove(num)
416+
col_used[col].remove(num)
417+
box_used[(row // 3) * 3 + col // 3].remove(num)
418+
return False
403419
```
404420

405421
### Go

‎problems/0376.摆动序列.md

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

7373
#### 情况一:上下坡中有平坡
7474

75-
例如 [1,2,2,2,1]这样的数组,如图:
75+
例如 [1,2,2,2,2,1]这样的数组,如图:
7676

7777
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230106170449.png)
7878

‎problems/0922.按奇偶排序数组II.md

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
[力扣题目链接](https://leetcode.cn/problems/sort-array-by-parity-ii/)
1313

14-
给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数。
14+
给定一个非负整数数组 nums, nums 中一半整数是奇数,一半整数是偶数。
1515

16-
对数组进行排序,以便当 A[i] 为奇数时,i 也是奇数;当 A[i] 为偶数时, i 也是偶数。
16+
对数组进行排序,以便当 nums[i] 为奇数时,i 也是奇数;当 nums[i] 为偶数时, i 也是偶数。
1717

1818
你可以返回任何满足上述条件的数组作为答案。
1919

@@ -35,17 +35,17 @@
3535
```CPP
3636
class Solution {
3737
public:
38-
vector<int> sortArrayByParityII(vector<int>& A) {
39-
vector<int> even(A.size() / 2); // 初始化就确定数组大小,节省开销
40-
vector<int> odd(A.size() / 2);
41-
vector<int> result(A.size());
38+
vector<int> sortArrayByParityII(vector<int>& nums) {
39+
vector<int> even(nums.size() / 2); // 初始化就确定数组大小,节省开销
40+
vector<int> odd(nums.size() / 2);
41+
vector<int> result(nums.size());
4242
int evenIndex = 0;
4343
int oddIndex = 0;
4444
int resultIndex = 0;
45-
// 把A数组放进偶数数组,和奇数数组
46-
for (int i = 0; i < A.size(); i++) {
47-
if (A[i] % 2 == 0) even[evenIndex++] = A[i];
48-
else odd[oddIndex++] = A[i];
45+
// 把nums数组放进偶数数组,和奇数数组
46+
for (int i = 0; i < nums.size(); i++) {
47+
if (nums[i] % 2 == 0) even[evenIndex++] = nums[i];
48+
else odd[oddIndex++] = nums[i];
4949
}
5050
// 把偶数数组,奇数数组分别放进result数组中
5151
for (int i = 0; i < evenIndex; i++) {
@@ -62,22 +62,22 @@ public:
6262
6363
### 方法二
6464
65-
以上代码我是建了两个辅助数组,而且A数组还相当于遍历了两次,用辅助数组的好处就是思路清晰,优化一下就是不用这两个辅助树,代码如下:
65+
以上代码我是建了两个辅助数组,而且nums数组还相当于遍历了两次,用辅助数组的好处就是思路清晰,优化一下就是不用这两个辅助数组,代码如下:
6666
6767
```CPP
6868
class Solution {
6969
public:
70-
vector<int> sortArrayByParityII(vector<int>& A) {
71-
vector<int> result(A.size());
70+
vector<int> sortArrayByParityII(vector<int>& nums) {
71+
vector<int> result(nums.size());
7272
int evenIndex = 0; // 偶数下标
7373
int oddIndex = 1; // 奇数下标
74-
for (int i = 0; i < A.size(); i++) {
75-
if (A[i] % 2 == 0) {
76-
result[evenIndex] = A[i];
74+
for (int i = 0; i < nums.size(); i++) {
75+
if (nums[i] % 2 == 0) {
76+
result[evenIndex] = nums[i];
7777
evenIndex += 2;
7878
}
7979
else {
80-
result[oddIndex] = A[i];
80+
result[oddIndex] = nums[i];
8181
oddIndex += 2;
8282
}
8383
}
@@ -96,15 +96,15 @@ public:
9696
```CPP
9797
class Solution {
9898
public:
99-
vector<int> sortArrayByParityII(vector<int>& A) {
99+
vector<int> sortArrayByParityII(vector<int>& nums) {
100100
int oddIndex = 1;
101-
for (int i = 0; i < A.size(); i += 2) {
102-
if (A[i] % 2 == 1) { // 在偶数位遇到了奇数
103-
while(A[oddIndex] % 2 != 0) oddIndex += 2; // 在奇数位找一个偶数
104-
swap(A[i], A[oddIndex]); // 替换
101+
for (int i = 0; i < nums.size(); i += 2) {
102+
if (nums[i] % 2 == 1) { // 在偶数位遇到了奇数
103+
while(nums[oddIndex] % 2 != 0) oddIndex += 2; // 在奇数位找一个偶数
104+
swap(nums[i], nums[oddIndex]); // 替换
105105
}
106106
}
107-
return A;
107+
return nums;
108108
}
109109
};
110110
```
@@ -253,6 +253,37 @@ func sortArrayByParityII(nums []int) []int {
253253
}
254254
return result;
255255
}
256+
257+
// 方法二
258+
func sortArrayByParityII(nums []int) []int {
259+
result := make([]int, len(nums))
260+
evenIndex := 0 // 偶数下标
261+
oddIndex := 1 // 奇数下标
262+
for _, v := range nums {
263+
if v % 2 == 0 {
264+
result[evenIndex] = v
265+
evenIndex += 2
266+
} else {
267+
result[oddIndex] = v
268+
oddIndex += 2
269+
}
270+
}
271+
return result
272+
}
273+
274+
// 方法三
275+
func sortArrayByParityII(nums []int) []int {
276+
oddIndex := 1
277+
for i := 0; i < len(nums); i += 2 {
278+
if nums[i] % 2 == 1 { // 在偶数位遇到了奇数
279+
for nums[oddIndex] % 2 != 0 {
280+
oddIndex += 2 // 在奇数位找一个偶数
281+
}
282+
nums[i], nums[oddIndex] = nums[oddIndex], nums[i]
283+
}
284+
}
285+
return nums
286+
}
256287
```
257288

258289
### JavaScript

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /