Skip to content

Commit 845d741

Browse files
committed
Merge branch 'youngyangyang04:master' into master
2 parents 3a0b699 + e9f8cda commit 845d741

10 files changed

+325
-22
lines changed

problems/0024.两两交换链表中的节点.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,27 @@ class Solution {
131131
Python:
132132

133133
Go:
134+
```go
135+
func swapPairs(head *ListNode) *ListNode {
136+
dummy := &ListNode{
137+
Next: head,
138+
}
139+
//head=list[i]
140+
//pre=list[i-1]
141+
pre := dummy
142+
for head != nil && head.Next != nil {
143+
pre.Next = head.Next
144+
next := head.Next.Next
145+
head.Next.Next = head
146+
head.Next = next
147+
//pre=list[(i+2)-1]
148+
pre = head
149+
//head=list[(i+2)]
150+
head = next
151+
}
152+
return dummy.Next
153+
}
154+
```
134155

135156
Javascript:
136157
```javascript

problems/0037.解数独.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,61 @@ Python:
291291

292292
Go:
293293

294+
Javascript:
295+
```Javascript
296+
var solveSudoku = function(board) {
297+
function isValid(row, col, val, board) {
298+
let len = board.length
299+
// 行不能重复
300+
for(let i = 0; i < len; i++) {
301+
if(board[row][i] === val) {
302+
return false
303+
}
304+
}
305+
// 列不能重复
306+
for(let i = 0; i < len; i++) {
307+
if(board[i][col] === val) {
308+
return false
309+
}
310+
}
311+
let startRow = Math.floor(row / 3) * 3
312+
let startCol = Math.floor(col / 3) * 3
313+
314+
for(let i = startRow; i < startRow + 3; i++) {
315+
for(let j = startCol; j < startCol + 3; j++) {
316+
if(board[i][j] === val) {
317+
return false
318+
}
319+
}
320+
}
321+
322+
return true
323+
}
294324

325+
function backTracking() {
326+
for(let i = 0; i < board.length; i++) {
327+
for(let j = 0; j < board[0].length; j++) {
328+
if(board[i][j] !== '.') continue
329+
for(let val = 1; val <= 9; val++) {
330+
if(isValid(i, j, `${val}`, board)) {
331+
board[i][j] = `${val}`
332+
if (backTracking()) {
333+
return true
334+
}
335+
336+
board[i][j] = `.`
337+
}
338+
}
339+
return false
340+
}
341+
}
342+
return true
343+
}
344+
backTracking(board)
345+
return board
346+
347+
};
348+
```
295349

296350

297351
-----------------------

problems/0051.N皇后.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,65 @@ func solveNQueens(n int) [][]string {
430430
}
431431
```
432432

433+
Javascript:
434+
```Javascript
435+
var solveNQueens = function(n) {
436+
function isValid(row, col, chessBoard, n) {
437+
438+
for(let i = 0; i < row; i++) {
439+
if(chessBoard[i][col] === 'Q') {
440+
return false
441+
}
442+
}
443+
444+
for(let i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {
445+
if(chessBoard[i][j] === 'Q') {
446+
return false
447+
}
448+
}
449+
450+
for(let i = row - 1, j = col + 1; i >= 0 && j < n; i--, j++) {
451+
if(chessBoard[i][j] === 'Q') {
452+
return false
453+
}
454+
}
455+
return true
456+
}
457+
458+
function transformChessBoard(chessBoard) {
459+
let chessBoardBack = []
460+
chessBoard.forEach(row => {
461+
let rowStr = ''
462+
row.forEach(value => {
463+
rowStr += value
464+
})
465+
chessBoardBack.push(rowStr)
466+
})
467+
468+
return chessBoardBack
469+
}
470+
471+
let result = []
472+
function backtracing(row,chessBoard) {
473+
if(row === n) {
474+
result.push(transformChessBoard(chessBoard))
475+
return
476+
}
477+
for(let col = 0; col < n; col++) {
478+
if(isValid(row, col, chessBoard, n)) {
479+
chessBoard[row][col] = 'Q'
480+
backtracing(row + 1,chessBoard)
481+
chessBoard[row][col] = '.'
482+
}
483+
}
484+
}
485+
let chessBoard = new Array(n).fill([]).map(() => new Array(n).fill('.'))
486+
backtracing(0,chessBoard)
487+
return result
488+
489+
};
490+
```
491+
433492

434493

435494
-----------------------

problems/0077.组合.md

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -370,23 +370,21 @@ class Solution {
370370

371371

372372
Python:
373-
```python
373+
```python3
374374
class Solution:
375-
result: List[List[int]] = []
376-
path: List[int] = []
377375
def combine(self, n: int, k: int) -> List[List[int]]:
378-
self.result = []
379-
self.combineHelper(n, k, 1)
380-
return self.result
381-
382-
def combineHelper(self, n: int, k: int, startIndex: int):
383-
if (l := len(self.path)) == k:
384-
self.result.append(self.path.copy())
385-
return
386-
for i in range(startIndex, n - (k - l) + 2):
387-
self.path.append(i)
388-
self.combineHelper(n, k, i + 1)
389-
self.path.pop()
376+
res=[] #存放符合条件结果的集合
377+
path=[] #用来存放符合条件结果
378+
def backtrack(n,k,startIndex):
379+
if len(path) == k:
380+
res.append(path[:])
381+
return
382+
for i in range(startIndex,n+1):
383+
path.append(i) #处理节点
384+
backtrack(n,k,i+1) #递归
385+
path.pop() #回溯,撤销处理的节点
386+
backtrack(n,k,1)
387+
return res
390388
```
391389
javascript
392390
```javascript
@@ -438,8 +436,6 @@ func backtrack(n,k,start int,track []int){
438436
```
439437

440438

441-
442-
443439
-----------------------
444440
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
445441
* B站视频:[代码随想录](https://space.bilibili.com/525438321)

problems/0077.组合优化.md

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,49 @@ class Solution {
176176
```
177177

178178
Python:
179-
180-
179+
```python3
180+
class Solution:
181+
def combine(self, n: int, k: int) -> List[List[int]]:
182+
res=[] #存放符合条件结果的集合
183+
path=[] #用来存放符合条件结果
184+
def backtrack(n,k,startIndex):
185+
if len(path) == k:
186+
res.append(path[:])
187+
return
188+
for i in range(startIndex,n-(k-len(path))+2): #优化的地方
189+
path.append(i) #处理节点
190+
backtrack(n,k,i+1) #递归
191+
path.pop() #回溯,撤销处理的节点
192+
backtrack(n,k,1)
193+
return res
194+
```
181195
Go:
196+
```Go
197+
var res [][]int
198+
func combine(n int, k int) [][]int {
199+
res=[][]int{}
200+
if n <= 0 || k <= 0 || k > n {
201+
return res
202+
}
203+
backtrack(n, k, 1, []int{})
204+
return res
205+
}
206+
func backtrack(n,k,start int,track []int){
207+
if len(track)==k{
208+
temp:=make([]int,k)
209+
copy(temp,track)
210+
res=append(res,temp)
211+
}
212+
if len(track)+n-start+1 < k {
213+
return
214+
}
215+
for i:=start;i<=n;i++{
216+
track=append(track,i)
217+
backtrack(n,k,i+1,track)
218+
track=track[:len(track)-1]
219+
}
220+
}
221+
```
182222

183223

184224

problems/0106.从中序与后序遍历序列构造二叉树.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,8 +580,10 @@ tree2 的前序遍历是[1 2 3], 后序遍历是[3 2 1]。
580580
581581
## 其他语言版本
582582
583-
584583
Java:
584+
585+
106.从中序与后序遍历序列构造二叉树
586+
585587
```java
586588
class Solution {
587589
public TreeNode buildTree(int[] inorder, int[] postorder) {
@@ -617,8 +619,43 @@ class Solution {
617619
}
618620
```
619621

622+
105.从前序与中序遍历序列构造二叉树
623+
624+
```java
625+
class Solution {
626+
public TreeNode buildTree(int[] preorder, int[] inorder) {
627+
return helper(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1);
628+
}
629+
630+
public TreeNode helper(int[] preorder, int preLeft, int preRight,
631+
int[] inorder, int inLeft, int inRight) {
632+
// 递归终止条件
633+
if (inLeft > inRight || preLeft > preRight) return null;
634+
635+
// val 为前序遍历第一个的值,也即是根节点的值
636+
// idx 为根据根节点的值来找中序遍历的下标
637+
int idx = inLeft, val = preorder[preLeft];
638+
TreeNode root = new TreeNode(val);
639+
for (int i = inLeft; i <= inRight; i++) {
640+
if (inorder[i] == val) {
641+
idx = i;
642+
break;
643+
}
644+
}
645+
646+
// 根据 idx 来递归找左右子树
647+
root.left = helper(preorder, preLeft + 1, preLeft + (idx - inLeft),
648+
inorder, inLeft, idx - 1);
649+
root.right = helper(preorder, preLeft + (idx - inLeft) + 1, preRight,
650+
inorder, idx + 1, inRight);
651+
return root;
652+
}
653+
}
654+
```
655+
620656
Python:
621657
105.从前序与中序遍历序列构造二叉树
658+
622659
```python
623660
# Definition for a binary tree node.
624661
# class TreeNode:
@@ -637,6 +674,7 @@ class Solution:
637674
return root
638675
```
639676
106.从中序与后序遍历序列构造二叉树
677+
640678
```python
641679
# Definition for a binary tree node.
642680
# class TreeNode:

problems/0112.路径总和.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,42 @@ class Solution {
347347
}
348348
```
349349

350+
0113.路径总和-ii
351+
352+
```java
353+
class Solution {
354+
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
355+
List<List<Integer>> res = new ArrayList<>();
356+
if (root == null) return res; // 非空判断
357+
358+
List<Integer> path = new LinkedList<>();
359+
preorderDFS(root, targetSum, res, path);
360+
return res;
361+
}
362+
363+
public void preorderDFS(TreeNode root, int targetSum, List<List<Integer>> res, List<Integer> path) {
364+
path.add(root.val);
365+
// 遇到了叶子节点
366+
if (root.left == null && root.right == null) {
367+
// 找到了和为 targetSum 的路径
368+
if (targetSum - root.val == 0) {
369+
res.add(new ArrayList<>(path));
370+
}
371+
return; // 如果和不为 targetSum,返回
372+
}
373+
374+
if (root.left != null) {
375+
preorderDFS(root.left, targetSum - root.val, res, path);
376+
path.remove(path.size() - 1); // 回溯
377+
}
378+
if (root.right != null) {
379+
preorderDFS(root.right, targetSum - root.val, res, path);
380+
path.remove(path.size() - 1); // 回溯
381+
}
382+
}
383+
}
384+
```
385+
350386
Python:
351387

352388
0112.路径总和

problems/0344.反转字符串.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,26 @@ func reverseString(s []byte) {
183183
}
184184
```
185185

186+
javaScript:
187+
188+
```js
189+
/**
190+
* @param {character[]} s
191+
* @return {void} Do not return anything, modify s in-place instead.
192+
*/
193+
var reverseString = function(s) {
194+
return s.reverse();
195+
};
196+
197+
var reverseString = function(s) {
198+
let l = -1, r = s.length;
199+
while(++l < --r) [s[l], s[r]] = [s[r], s[l]];
200+
return s;
201+
};
202+
```
203+
204+
205+
186206

187207

188208

0 commit comments

Comments
 (0)