From a0bc86cad27f6e1c6b0e4deee25c5305e84187f7 Mon Sep 17 00:00:00 2001 From: youngyangyang04 <826123027@qq.com> Date: Fri, 7 May 2021 15:29:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0java=20=E5=92=8C=20python?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + problems/0051.N皇后.md | 121 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+) diff --git a/README.md b/README.md index a1fda9c0..a116da65 100644 --- a/README.md +++ b/README.md @@ -390,6 +390,7 @@ * [分割回文串(对应力扣题目:131.分割回文串)](https://www.bilibili.com/video/BV1c54y1e7k6) * [二叉树理论基础](https://www.bilibili.com/video/BV1Hy4y1t7ij) * [二叉树的递归遍历](https://www.bilibili.com/video/BV1Wh411S7xt) +* [二叉树的非递归遍历(一)](https://www.bilibili.com/video/BV15f4y1W7i2) (持续更新中....) diff --git a/problems/0051.N皇后.md b/problems/0051.N皇后.md index 31ffbf2d..d9ca0d09 100644 --- a/problems/0051.N皇后.md +++ b/problems/0051.N皇后.md @@ -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> res = new ArrayList<>(); + + public List> 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 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=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; + } +} +``` +