mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
Merge branch 'master' of https://github.com/youngyangyang04/leetcode
This commit is contained in:
@ -363,7 +363,72 @@ Python:
|
|||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
```Go
|
||||||
|
import "strings"
|
||||||
|
var res [][]string
|
||||||
|
|
||||||
|
func isValid(board [][]string, row, col int) (res bool){
|
||||||
|
n := len(board)
|
||||||
|
for i:=0; i < row; i++ {
|
||||||
|
if board[i][col] == "Q" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for i := 0; i < n; i++{
|
||||||
|
if board[row][i] == "Q" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for i ,j := row, col; i >= 0 && j >=0 ; i, j = i - 1, j- 1{
|
||||||
|
if board[i][j] == "Q"{
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for i, j := row, col; i >=0 && j < n; i,j = i-1, j+1 {
|
||||||
|
if board[i][j] == "Q" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func backtrack(board [][]string, row int) {
|
||||||
|
size := len(board)
|
||||||
|
if row == size{
|
||||||
|
temp := make([]string, size)
|
||||||
|
for i := 0; i<size;i++{
|
||||||
|
temp[i] = strings.Join(board[i],"")
|
||||||
|
}
|
||||||
|
res =append(res,temp)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for col := 0; col < size; col++ {
|
||||||
|
if !isValid(board, row, col){
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
board[row][col] = "Q"
|
||||||
|
backtrack(board, row+1)
|
||||||
|
board[row][col] = "."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func solveNQueens(n int) [][]string {
|
||||||
|
res = [][]string{}
|
||||||
|
board := make([][]string, n)
|
||||||
|
for i := 0; i < n; i++{
|
||||||
|
board[i] = make([]string, n)
|
||||||
|
}
|
||||||
|
for i := 0; i < n; i++{
|
||||||
|
for j := 0; j<n;j++{
|
||||||
|
board[i][j] = "."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
backtrack(board, 0)
|
||||||
|
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -623,7 +623,21 @@ Python:
|
|||||||
Go:
|
Go:
|
||||||
|
|
||||||
|
|
||||||
|
JavaScript
|
||||||
|
```javascript
|
||||||
|
var buildTree = function(inorder, postorder) {
|
||||||
|
if (!postorder.length) return null
|
||||||
|
|
||||||
|
let root = new TreeNode(postorder[postorder.length - 1])
|
||||||
|
|
||||||
|
let index = inorder.findIndex(number => number === root.val)
|
||||||
|
|
||||||
|
root.left = buildTree(inorder.slice(0, index), postorder.slice(0, index))
|
||||||
|
root.right = buildTree(inorder.slice(index + 1, inorder.length), postorder.slice(index, postorder.length - 1))
|
||||||
|
|
||||||
|
return root
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
|
@ -201,6 +201,21 @@ public:
|
|||||||
}
|
}
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 主函数简单写法
|
||||||
|
string reverseWords(string s) {
|
||||||
|
removeExtraSpaces(s);
|
||||||
|
reverse(s, 0, s.size() - 1);
|
||||||
|
for(int i = 0; i < s.size(); i++) {
|
||||||
|
int j = i;
|
||||||
|
// 查找单词间的空格,翻转单词
|
||||||
|
while(j < s.size() && s[j] != ' ') j++;
|
||||||
|
reverse(s, i, j - 1);
|
||||||
|
i = j;
|
||||||
|
}
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
*/
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user