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 05fc5cb

Browse files
Merge pull request #3 from zhicheng-lee/zhicheng-lee-patch-2
0051.N皇后 调整Java代码和Python代码的顺序,修改为与其它题解顺序一致
2 parents 6553b5b + b6633fe commit 05fc5cb

File tree

1 file changed

+51
-50
lines changed

1 file changed

+51
-50
lines changed

‎problems/0051.N皇后.md

Lines changed: 51 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -224,56 +224,6 @@ public:
224224

225225
## 其他语言补充
226226

227-
228-
### Python
229-
230-
```python
231-
class Solution:
232-
def solveNQueens(self, n: int) -> List[List[str]]:
233-
if not n: return []
234-
board = [['.'] * n for _ in range(n)]
235-
res = []
236-
def isVaild(board,row, col):
237-
#判断同一列是否冲突
238-
for i in range(len(board)):
239-
if board[i][col] == 'Q':
240-
return False
241-
# 判断左上角是否冲突
242-
i = row -1
243-
j = col -1
244-
while i>=0 and j>=0:
245-
if board[i][j] == 'Q':
246-
return False
247-
i -= 1
248-
j -= 1
249-
# 判断右上角是否冲突
250-
i = row - 1
251-
j = col + 1
252-
while i>=0 and j < len(board):
253-
if board[i][j] == 'Q':
254-
return False
255-
i -= 1
256-
j += 1
257-
return True
258-
259-
def backtracking(board, row, n):
260-
# 如果走到最后一行,说明已经找到一个解
261-
if row == n:
262-
temp_res = []
263-
for temp in board:
264-
temp_str = "".join(temp)
265-
temp_res.append(temp_str)
266-
res.append(temp_res)
267-
for col in range(n):
268-
if not isVaild(board, row, col):
269-
continue
270-
board[row][col] = 'Q'
271-
backtracking(board, row+1, n)
272-
board[row][col] = '.'
273-
backtracking(board, 0, n)
274-
return res
275-
```
276-
277227
### Java
278228

279229
```java
@@ -343,6 +293,55 @@ class Solution {
343293
}
344294
```
345295

296+
### Python
297+
298+
```python
299+
class Solution:
300+
def solveNQueens(self, n: int) -> List[List[str]]:
301+
if not n: return []
302+
board = [['.'] * n for _ in range(n)]
303+
res = []
304+
def isVaild(board,row, col):
305+
#判断同一列是否冲突
306+
for i in range(len(board)):
307+
if board[i][col] == 'Q':
308+
return False
309+
# 判断左上角是否冲突
310+
i = row -1
311+
j = col -1
312+
while i>=0 and j>=0:
313+
if board[i][j] == 'Q':
314+
return False
315+
i -= 1
316+
j -= 1
317+
# 判断右上角是否冲突
318+
i = row - 1
319+
j = col + 1
320+
while i>=0 and j < len(board):
321+
if board[i][j] == 'Q':
322+
return False
323+
i -= 1
324+
j += 1
325+
return True
326+
327+
def backtracking(board, row, n):
328+
# 如果走到最后一行,说明已经找到一个解
329+
if row == n:
330+
temp_res = []
331+
for temp in board:
332+
temp_str = "".join(temp)
333+
temp_res.append(temp_str)
334+
res.append(temp_res)
335+
for col in range(n):
336+
if not isVaild(board, row, col):
337+
continue
338+
board[row][col] = 'Q'
339+
backtracking(board, row+1, n)
340+
board[row][col] = '.'
341+
backtracking(board, 0, n)
342+
return res
343+
```
344+
346345

347346
### Go
348347
```Go
@@ -398,6 +397,8 @@ func isValid(n, row, col int, chessboard [][]string) bool {
398397
return true
399398
}
400399
```
400+
401+
401402
### Javascript
402403
```Javascript
403404
var solveNQueens = function(n) {

0 commit comments

Comments
(0)

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