mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 20:40:39 +08:00
Merge pull request #1602 from zhicheng-lee/master
更新 0332.重新安排行程 0051.N皇后 0077.组合优化
This commit is contained in:
@ -222,56 +222,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
|
||||||
|
|
||||||
```java
|
```java
|
||||||
@ -341,6 +291,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
|
||||||
```Go
|
```Go
|
||||||
@ -396,6 +395,8 @@ func isValid(n, row, col int, chessboard [][]string) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### Javascript
|
### Javascript
|
||||||
```Javascript
|
```Javascript
|
||||||
var solveNQueens = function(n) {
|
var solveNQueens = function(n) {
|
||||||
|
@ -20,7 +20,7 @@
|
|||||||
|
|
||||||
大家先回忆一下[77. 组合]给出的回溯法的代码:
|
大家先回忆一下[77. 组合]给出的回溯法的代码:
|
||||||
|
|
||||||
```c++
|
```CPP
|
||||||
class Solution {
|
class Solution {
|
||||||
private:
|
private:
|
||||||
vector<vector<int>> result; // 存放符合条件结果的集合
|
vector<vector<int>> result; // 存放符合条件结果的集合
|
||||||
@ -52,7 +52,7 @@ public:
|
|||||||
|
|
||||||
在遍历的过程中有如下代码:
|
在遍历的过程中有如下代码:
|
||||||
|
|
||||||
```c++
|
```CPP
|
||||||
for (int i = startIndex; i <= n; i++) {
|
for (int i = startIndex; i <= n; i++) {
|
||||||
path.push_back(i);
|
path.push_back(i);
|
||||||
backtracking(n, k, i + 1);
|
backtracking(n, k, i + 1);
|
||||||
@ -76,7 +76,7 @@ for (int i = startIndex; i <= n; i++) {
|
|||||||
**如果for循环选择的起始位置之后的元素个数 已经不足 我们需要的元素个数了,那么就没有必要搜索了**。
|
**如果for循环选择的起始位置之后的元素个数 已经不足 我们需要的元素个数了,那么就没有必要搜索了**。
|
||||||
|
|
||||||
注意代码中i,就是for循环里选择的起始位置。
|
注意代码中i,就是for循环里选择的起始位置。
|
||||||
```c++
|
```CPP
|
||||||
for (int i = startIndex; i <= n; i++) {
|
for (int i = startIndex; i <= n; i++) {
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -98,13 +98,13 @@ for (int i = startIndex; i <= n; i++) {
|
|||||||
|
|
||||||
所以优化之后的for循环是:
|
所以优化之后的for循环是:
|
||||||
|
|
||||||
```c++
|
```CPP
|
||||||
for (int i = startIndex; i <= n - (k - path.size()) + 1; i++) // i为本次搜索的起始位置
|
for (int i = startIndex; i <= n - (k - path.size()) + 1; i++) // i为本次搜索的起始位置
|
||||||
```
|
```
|
||||||
|
|
||||||
优化后整体代码如下:
|
优化后整体代码如下:
|
||||||
|
|
||||||
```c++
|
```CPP
|
||||||
class Solution {
|
class Solution {
|
||||||
private:
|
private:
|
||||||
vector<vector<int>> result;
|
vector<vector<int>> result;
|
||||||
|
@ -262,6 +262,43 @@ for (pair<string, int>target : targets[result[result.size() - 1]])
|
|||||||
|
|
||||||
### java
|
### java
|
||||||
|
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
private LinkedList<String> res;
|
||||||
|
private LinkedList<String> path = new LinkedList<>();
|
||||||
|
|
||||||
|
public List<String> findItinerary(List<List<String>> tickets) {
|
||||||
|
Collections.sort(tickets, (a, b) -> a.get(1).compareTo(b.get(1)));
|
||||||
|
path.add("JFK");
|
||||||
|
boolean[] used = new boolean[tickets.size()];
|
||||||
|
backTracking((ArrayList) tickets, used);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean backTracking(ArrayList<List<String>> tickets, boolean[] used) {
|
||||||
|
if (path.size() == tickets.size() + 1) {
|
||||||
|
res = new LinkedList(path);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < tickets.size(); i++) {
|
||||||
|
if (!used[i] && tickets.get(i).get(0).equals(path.getLast())) {
|
||||||
|
path.add(tickets.get(i).get(1));
|
||||||
|
used[i] = true;
|
||||||
|
|
||||||
|
if (backTracking(tickets, used)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
used[i] = false;
|
||||||
|
path.removeLast();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
```java
|
```java
|
||||||
class Solution {
|
class Solution {
|
||||||
private Deque<String> res;
|
private Deque<String> res;
|
||||||
|
Reference in New Issue
Block a user