@@ -224,56 +224,6 @@ public:
224
224
225
225
## 其他语言补充
226
226
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
-
277
227
### Java
278
228
279
229
``` java
@@ -343,6 +293,55 @@ class Solution {
343
293
}
344
294
```
345
295
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
+
346
345
347
346
### Go
348
347
``` Go
@@ -398,6 +397,8 @@ func isValid(n, row, col int, chessboard [][]string) bool {
398
397
return true
399
398
}
400
399
```
400
+
401
+
401
402
### Javascript
402
403
``` Javascript
403
404
var solveNQueens = function (n ) {
0 commit comments