mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
添加java 和 python版本
This commit is contained in:
@ -390,6 +390,7 @@
|
|||||||
* [分割回文串(对应力扣题目:131.分割回文串)](https://www.bilibili.com/video/BV1c54y1e7k6)
|
* [分割回文串(对应力扣题目:131.分割回文串)](https://www.bilibili.com/video/BV1c54y1e7k6)
|
||||||
* [二叉树理论基础](https://www.bilibili.com/video/BV1Hy4y1t7ij)
|
* [二叉树理论基础](https://www.bilibili.com/video/BV1Hy4y1t7ij)
|
||||||
* [二叉树的递归遍历](https://www.bilibili.com/video/BV1Wh411S7xt)
|
* [二叉树的递归遍历](https://www.bilibili.com/video/BV1Wh411S7xt)
|
||||||
|
* [二叉树的非递归遍历(一)](https://www.bilibili.com/video/BV15f4y1W7i2)
|
||||||
|
|
||||||
(持续更新中....)
|
(持续更新中....)
|
||||||
|
|
||||||
|
@ -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