Skip to content

Commit 26f1508

Browse files
committed
Merge branch 'youngyangyang04:master' into master
2 parents 0e8430f + e9f8cda commit 26f1508

9 files changed

+296
-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/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/0101.对称二叉树.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,90 @@ const check = (leftPtr, rightPtr) => {
379379
return leftPtr.val === rightPtr.val && check(leftPtr.left, rightPtr.right) && check(leftPtr.right, rightPtr.left)
380380
}
381381
```
382+
JavaScript:
383+
384+
递归判断是否为对称二叉树:
385+
```javascript
386+
var isSymmetric = function(root) {
387+
//使用递归遍历左右子树 递归三部曲
388+
// 1. 确定递归的参数 root.left root.right和返回值true false
389+
const compareNode=function(left,right){
390+
//2. 确定终止条件 空的情况
391+
if(left===null&&right!==null||left!==null&&right===null){
392+
return false;
393+
}else if(left===null&&right===null){
394+
return true;
395+
}else if(left.val!==right.val){
396+
return false;
397+
}
398+
//3. 确定单层递归逻辑
399+
let outSide=compareNode(left.left,right.right);
400+
let inSide=compareNode(left.right,right.left);
401+
return outSide&&inSide;
402+
}
403+
if(root===null){
404+
return true;
405+
}
406+
return compareNode(root.left,root.right);
407+
};
408+
```
409+
队列实现迭代判断是否为对称二叉树:
410+
```javascript
411+
var isSymmetric = function(root) {
412+
//迭代方法判断是否是对称二叉树
413+
//首先判断root是否为空
414+
if(root===null){
415+
return true;
416+
}
417+
let queue=[];
418+
queue.push(root.left);
419+
queue.push(root.right);
420+
while(queue.length){
421+
let leftNode=queue.shift();//左节点
422+
let rightNode=queue.shift();//右节点
423+
if(leftNode===null&&rightNode===null){
424+
continue;
425+
}
426+
if(leftNode===null||rightNode===null||leftNode.val!==rightNode.val){
427+
return false;
428+
}
429+
queue.push(leftNode.left);//左节点左孩子入队
430+
queue.push(rightNode.right);//右节点右孩子入队
431+
queue.push(leftNode.right);//左节点右孩子入队
432+
queue.push(rightNode.left);//右节点左孩子入队
433+
}
434+
return true;
435+
};
436+
```
437+
栈实现迭代判断是否为对称二叉树:
438+
```javascript
439+
var isSymmetric = function(root) {
440+
//迭代方法判断是否是对称二叉树
441+
//首先判断root是否为空
442+
if(root===null){
443+
return true;
444+
}
445+
let stack=[];
446+
stack.push(root.left);
447+
stack.push(root.right);
448+
while(stack.length){
449+
let rightNode=stack.pop();//左节点
450+
let leftNode=stack.pop();//右节点
451+
if(leftNode===null&&rightNode===null){
452+
continue;
453+
}
454+
if(leftNode===null||rightNode===null||leftNode.val!==rightNode.val){
455+
return false;
456+
}
457+
stack.push(leftNode.left);//左节点左孩子入队
458+
stack.push(rightNode.right);//右节点右孩子入队
459+
stack.push(leftNode.right);//左节点右孩子入队
460+
stack.push(rightNode.left);//右节点左孩子入队
461+
}
462+
return true;
463+
};
464+
```
465+
382466

383467

384468
-----------------------

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

problems/0541.反转字符串II.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,26 @@ class Solution(object):
168168

169169
Go:
170170

171+
javaScript:
172+
173+
```js
174+
175+
/**
176+
* @param {string} s
177+
* @param {number} k
178+
* @return {string}
179+
*/
180+
var reverseStr = function(s, k) {
181+
const len = s.length;
182+
let resArr = s.split("");
183+
for(let i = 0; i < len; i += 2 * k) {
184+
let l = i - 1, r = i + k > len ? len : i + k;
185+
while(++l < --r) [resArr[l], resArr[r]] = [resArr[r], resArr[l]];
186+
}
187+
return resArr.join("");
188+
};
189+
190+
```
171191

172192

173193

0 commit comments

Comments
 (0)