From b6633feb6996a46aa0858d73b1803370d5bafc8e Mon Sep 17 00:00:00 2001 From: zhicheng lee <904688436@qq.com> Date: Sun, 10 Apr 2022 10:27:02 +0800 Subject: [PATCH] =?UTF-8?q?0051.N=E7=9A=87=E5=90=8E=20=E8=B0=83=E6=95=B4Ja?= =?UTF-8?q?va=E4=BB=A3=E7=A0=81=E5=92=8CPython=E4=BB=A3=E7=A0=81=E7=9A=84?= =?UTF-8?q?=E9=A1=BA=E5=BA=8F=EF=BC=8C=E4=BF=AE=E6=94=B9=E4=B8=BA=E4=B8=8E?= =?UTF-8?q?=E5=85=B6=E5=AE=83=E9=A2=98=E8=A7=A3=E9=A1=BA=E5=BA=8F=E4=B8=80?= =?UTF-8?q?=E8=87=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0051.N\347\232\207\345\220\216.md" | 101 ++++++++++--------- 1 file changed, 51 insertions(+), 50 deletions(-) diff --git "a/problems/0051.N\347\232\207\345\220\216.md" "b/problems/0051.N\347\232\207\345\220\216.md" index 7eb0d7a0b1..1b0cf0026b 100644 --- "a/problems/0051.N\347\232\207\345\220\216.md" +++ "b/problems/0051.N\347\232\207\345\220\216.md" @@ -224,56 +224,6 @@ public: ## 其他语言补充 - -### Python - -```python -class Solution: - def solveNQueens(self, n: int) -> List[List[str]]: - if not n: return [] - board = [['.'] * n for _ in range(n)] - res = [] - def isVaild(board,row, col): - #判断同一列是否冲突 - for i in range(len(board)): - if board[i][col] == 'Q': - return False - # 判断左上角是否冲突 - i = row -1 - j = col -1 - while i>=0 and j>=0: - if board[i][j] == 'Q': - return False - i -= 1 - j -= 1 - # 判断右上角是否冲突 - i = row - 1 - j = col + 1 - while i>=0 and j < len(board): - if board[i][j] == 'Q': - return False - i -= 1 - j += 1 - return True - - def backtracking(board, row, n): - # 如果走到最后一行,说明已经找到一个解 - if row == n: - temp_res = [] - for temp in board: - temp_str = "".join(temp) - temp_res.append(temp_str) - res.append(temp_res) - for col in range(n): - if not isVaild(board, row, col): - continue - board[row][col] = 'Q' - backtracking(board, row+1, n) - board[row][col] = '.' - backtracking(board, 0, n) - return res -``` - ### Java ```java @@ -343,6 +293,55 @@ class Solution { } ``` +### Python + +```python +class Solution: + def solveNQueens(self, n: int) -> List[List[str]]: + if not n: return [] + board = [['.'] * n for _ in range(n)] + res = [] + def isVaild(board,row, col): + #判断同一列是否冲突 + for i in range(len(board)): + if board[i][col] == 'Q': + return False + # 判断左上角是否冲突 + i = row -1 + j = col -1 + while i>=0 and j>=0: + if board[i][j] == 'Q': + return False + i -= 1 + j -= 1 + # 判断右上角是否冲突 + i = row - 1 + j = col + 1 + while i>=0 and j < len(board): + if board[i][j] == 'Q': + return False + i -= 1 + j += 1 + return True + + def backtracking(board, row, n): + # 如果走到最后一行,说明已经找到一个解 + if row == n: + temp_res = [] + for temp in board: + temp_str = "".join(temp) + temp_res.append(temp_str) + res.append(temp_res) + for col in range(n): + if not isVaild(board, row, col): + continue + board[row][col] = 'Q' + backtracking(board, row+1, n) + board[row][col] = '.' + backtracking(board, 0, n) + return res +``` + ### Go ```Go @@ -398,6 +397,8 @@ func isValid(n, row, col int, chessboard [][]string) bool { return true } ``` + + ### Javascript ```Javascript var solveNQueens = function(n) {