mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-13 06:06:11 +08:00
添加java 和 python版本
This commit is contained in:
@ -231,3 +231,124 @@ 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
|
||||
class Solution {
|
||||
List<List<String>> res = new ArrayList<>();
|
||||
|
||||
public List<List<String>> solveNQueens(int n) {
|
||||
char[][] chessboard = new char[n][n];
|
||||
for (char[] c : chessboard) {
|
||||
Arrays.fill(c, '.');
|
||||
}
|
||||
backTrack(n, 0, chessboard);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
public void backTrack(int n, int row, char[][] chessboard) {
|
||||
if (row == n) {
|
||||
res.add(Array2List(chessboard));
|
||||
return;
|
||||
}
|
||||
|
||||
for (int col = 0;col < n; ++col) {
|
||||
if (isValid (row, col, n, chessboard)) {
|
||||
chessboard[row][col] = 'Q';
|
||||
backTrack(n, row+1, chessboard);
|
||||
chessboard[row][col] = '.';
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public List Array2List(char[][] chessboard) {
|
||||
List<String> list = new ArrayList<>();
|
||||
|
||||
for (char[] c : chessboard) {
|
||||
list.add(String.copyValueOf(c));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
public boolean isValid(int row, int col, int n, char[][] chessboard) {
|
||||
// 检查列
|
||||
for (int i=0; i<n; ++i) {
|
||||
if (chessboard[i][col] == 'Q') {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// 检查45度对角线
|
||||
for (int i=row-1, j=col-1; i>=0 && j>=0; i--, j--) {
|
||||
if (chessboard[i][j] == 'Q') {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// 检查135度对角线
|
||||
for (int i=row-1, j=col+1; i>=0 && j<=n-1; i--, j++) {
|
||||
if (chessboard[i][j] == 'Q') {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
Reference in New Issue
Block a user