From 195504c0e03c8509d4921ef1d90b15059cf48466 Mon Sep 17 00:00:00 2001
From: jojoo15 <75017412+jojoo15@users.noreply.github.com>
Date: Tue, 18 May 2021 11:33:32 +0200
Subject: [PATCH 01/31] =?UTF-8?q?Update=200617.=E5=90=88=E5=B9=B6=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=A0=91.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
python version of code added
---
problems/0617.合并二叉树.md | 20 ++++++++++++++++++--
1 file changed, 18 insertions(+), 2 deletions(-)
diff --git a/problems/0617.合并二叉树.md b/problems/0617.合并二叉树.md
index 848454de..ce37a953 100644
--- a/problems/0617.合并二叉树.md
+++ b/problems/0617.合并二叉树.md
@@ -312,7 +312,23 @@ class Solution {
```
Python:
-
+```python
+# Definition for a binary tree node.
+# class TreeNode:
+# def __init__(self, val=0, left=None, right=None):
+# self.val = val
+# self.left = left
+# self.right = right
+//递归法*前序遍历
+class Solution:
+ def mergeTrees(self, root1: TreeNode, root2: TreeNode) -> TreeNode:
+ if not root1: return root2 // 如果t1为空,合并之后就应该是t2
+ if not root2: return root1 // 如果t2为空,合并之后就应该是t1
+ root1.val = root1.val + root2.val //中
+ root1.left = self.mergeTrees(root1.left , root2.left) //左
+ root1.right = self.mergeTrees(root1.right , root2.right) //右
+ return root1 //root1修改了结构和数值
+```
Go:
@@ -323,4 +339,4 @@ Go:
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
-
\ No newline at end of file
+
From 6d91454f18e0c5bdcb13412a976346d27b2d42ed Mon Sep 17 00:00:00 2001
From: Powerstot <77142630+Powerstot@users.noreply.github.com>
Date: Tue, 18 May 2021 17:44:03 +0800
Subject: [PATCH 02/31] =?UTF-8?q?=E4=BF=AE=E6=94=B90232.=E7=94=A8=E6=A0=88?=
=?UTF-8?q?=E5=AE=9E=E7=8E=B0=E9=98=9F=E5=88=97=20Java=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
修改0232.用栈实现队列 Java版本
---
problems/0232.用栈实现队列.md | 97 ++++++++++++++++++++++++++++-
1 file changed, 96 insertions(+), 1 deletion(-)
diff --git a/problems/0232.用栈实现队列.md b/problems/0232.用栈实现队列.md
index abdc363d..3cf058d7 100644
--- a/problems/0232.用栈实现队列.md
+++ b/problems/0232.用栈实现队列.md
@@ -131,6 +131,101 @@ public:
Java:
+使用Stack(堆栈)同名方法:
+```java
+class MyQueue {
+ // java中的 Stack 有设计上的缺陷,官方推荐使用 Deque(双端队列) 代替 Stack
+ Deque stIn;
+ Deque stOut;
+ /** Initialize your data structure here. */
+ public MyQueue() {
+ stIn = new ArrayDeque<>();
+ stOut = new ArrayDeque<>();
+ }
+
+ /** Push element x to the back of queue. */
+ public void push(int x) {
+ stIn.push(x);
+ }
+
+ /** Removes the element from in front of queue and returns that element. */
+ public int pop() {
+ // 只要 stOut 为空,那么就应该将 stIn 中所有的元素倒腾到 stOut 中
+ if (stOut.isEmpty()) {
+ while (!stIn.isEmpty()) {
+ stOut.push(stIn.pop());
+ }
+ }
+ // 再返回 stOut 中的元素
+ return stOut.pop();
+ }
+
+ /** Get the front element. */
+ public int peek() {
+ // 直接使用已有的pop函数
+ int res = this.pop();
+ // 因为pop函数弹出了元素res,所以再添加回去
+ stOut.push(res);
+ return res;
+ }
+
+ /** Returns whether the queue is empty. */
+ public boolean empty() {
+ // 当 stIn 栈为空时,说明没有元素可以倒腾到 stOut 栈了
+ // 并且 stOut 栈也为空时,说明没有以前从 stIn 中倒腾到的元素了
+ return stIn.isEmpty() && stOut.isEmpty();
+ }
+}
+```
+
+个人习惯写法,使用Deque通用api:
+```java
+class MyQueue {
+ // java中的 Stack 有设计上的缺陷,官方推荐使用 Deque(双端队列) 代替 Stack
+ // Deque 中的 addFirst、removeFirst、peekFirst 等方法等效于 Stack(堆栈) 中的 push、pop、peek
+ Deque stIn;
+ Deque stOut;
+ /** Initialize your data structure here. */
+ public MyQueue() {
+ stIn = new ArrayDeque<>();
+ stOut = new ArrayDeque<>();
+ }
+
+ /** Push element x to the back of queue. */
+ public void push(int x) {
+ stIn.addLast(x);
+ }
+
+ /** Removes the element from in front of queue and returns that element. */
+ public int pop() {
+ // 只要 stOut 为空,那么就应该将 stIn 中所有的元素倒腾到 stOut 中
+ if (stOut.isEmpty()) {
+ while (!stIn.isEmpty()) {
+ stOut.addLast(stIn.pollLast());
+ }
+ }
+ // 再返回 stOut 中的元素
+ return stOut.pollLast();
+ }
+
+ /** Get the front element. */
+ public int peek() {
+ // 直接使用已有的pop函数
+ int res = this.pop();
+ // 因为pop函数弹出了元素res,所以再添加回去
+ stOut.addLast(res);
+ return res;
+ }
+
+ /** Returns whether the queue is empty. */
+ public boolean empty() {
+ // 当 stIn 栈为空时,说明没有元素可以倒腾到 stOut 栈了
+ // 并且 stOut 栈也为空时,说明没有以前从 stIn 中倒腾到的元素了
+ return stIn.isEmpty() && stOut.isEmpty();
+ }
+}
+```
+
```java
class MyQueue {
@@ -198,4 +293,4 @@ Go:
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
-
\ No newline at end of file
+
From 3169bc2688d702a1b8b367a6cf94db1ef88bf345 Mon Sep 17 00:00:00 2001
From: Powerstot <77142630+Powerstot@users.noreply.github.com>
Date: Tue, 18 May 2021 17:52:35 +0800
Subject: [PATCH 03/31] =?UTF-8?q?=E4=BF=AE=E6=94=B90225.=E7=94=A8=E9=98=9F?=
=?UTF-8?q?=E5=88=97=E5=AE=9E=E7=8E=B0=E6=A0=88=20Java=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
修改0225.用队列实现栈 Java版本
---
problems/0225.用队列实现栈.md | 90 ++++++++++++++++++++++++++++-
1 file changed, 89 insertions(+), 1 deletion(-)
diff --git a/problems/0225.用队列实现栈.md b/problems/0225.用队列实现栈.md
index 4018364a..8dff2b86 100644
--- a/problems/0225.用队列实现栈.md
+++ b/problems/0225.用队列实现栈.md
@@ -156,6 +156,7 @@ public:
Java:
+使用两个 Queue 实现
```java
class MyStack {
@@ -205,7 +206,94 @@ class MyStack {
* boolean param_4 = obj.empty();
*/
```
+使用两个 Deque 实现
+```java
+class MyStack {
+ // Deque 接口继承了 Queue 接口
+ // 所以 Queue 中的 add、poll、peek等效于 Deque 中的 addLast、pollFirst、peekFirst
+ Deque que1; // 和栈中保持一样元素的队列
+ Deque que2; // 辅助队列
+ /** Initialize your data structure here. */
+ public MyStack() {
+ que1 = new ArrayDeque<>();
+ que2 = new ArrayDeque<>();
+ }
+
+ /** Push element x onto stack. */
+ public void push(int x) {
+ que1.addLast(x);
+ }
+
+ /** Removes the element on top of the stack and returns that element. */
+ public int pop() {
+ int size = que1.size();
+ size--;
+ // 将 que1 导入 que2 ,但留下最后一个值
+ while (size-- > 0) {
+ que2.addLast(que1.peekFirst());
+ que1.pollFirst();
+ }
+ int res = que1.pollFirst();
+ // 将 que2 对象的引用赋给了 que1 ,此时 que1,que2 指向同一个队列
+ que1 = que2;
+ // 如果直接操作 que2,que1 也会受到影响,所以为 que2 分配一个新的空间
+ que2 = new ArrayDeque<>();
+ return res;
+ }
+
+ /** Get the top element. */
+ public int top() {
+ return que1.peekLast();
+ }
+
+ /** Returns whether the stack is empty. */
+ public boolean empty() {
+ return que1.isEmpty();
+ }
+}
+```
+优化,使用一个 Deque 实现
+```java
+class MyStack {
+ // Deque 接口继承了 Queue 接口
+ // 所以 Queue 中的 add、poll、peek等效于 Deque 中的 addLast、pollFirst、peekFirst
+ Deque que1;
+ /** Initialize your data structure here. */
+ public MyStack() {
+ que1 = new ArrayDeque<>();
+ }
+
+ /** Push element x onto stack. */
+ public void push(int x) {
+ que1.addLast(x);
+ }
+
+ /** Removes the element on top of the stack and returns that element. */
+ public int pop() {
+ int size = que1.size();
+ size--;
+ // 将 que1 导入 que2 ,但留下最后一个值
+ while (size-- > 0) {
+ que1.addLast(que1.peekFirst());
+ que1.pollFirst();
+ }
+
+ int res = que1.pollFirst();
+ return res;
+ }
+
+ /** Get the top element. */
+ public int top() {
+ return que1.peekLast();
+ }
+
+ /** Returns whether the stack is empty. */
+ public boolean empty() {
+ return que1.isEmpty();
+ }
+}
+```
Python:
@@ -276,4 +364,4 @@ Go:
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
-
\ No newline at end of file
+
From 0b3945cfd1a9b972839678b717f0f08461daae34 Mon Sep 17 00:00:00 2001
From: jojoo15 <75017412+jojoo15@users.noreply.github.com>
Date: Tue, 18 May 2021 12:18:00 +0200
Subject: [PATCH 04/31] =?UTF-8?q?Update=200700.=E4=BA=8C=E5=8F=89=E6=90=9C?=
=?UTF-8?q?=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E6=90=9C=E7=B4=A2.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
recursive python version of code modified
---
problems/0700.二叉搜索树中的搜索.md | 17 +++++++++++------
1 file changed, 11 insertions(+), 6 deletions(-)
diff --git a/problems/0700.二叉搜索树中的搜索.md b/problems/0700.二叉搜索树中的搜索.md
index 277ef681..3e35fbfb 100644
--- a/problems/0700.二叉搜索树中的搜索.md
+++ b/problems/0700.二叉搜索树中的搜索.md
@@ -212,13 +212,18 @@ Python:
递归法:
```python
+# Definition for a binary tree node.
+# class TreeNode:
+# def __init__(self, val=0, left=None, right=None):
+# self.val = val
+# self.left = left
+# self.right = right
class Solution:
def searchBST(self, root: TreeNode, val: int) -> TreeNode:
- if root is None:
- return None
- if val < root.val: return self.searchBST(root.left, val)
- elif val > root.val: return self.searchBST(root.right, val)
- else: return root
+ if not root or root.val == val: return root //为空或者已经找到都是直接返回root,所以合并了
+ if root.val > val: return self.searchBST(root.left,val) //注意一定要加return
+ else: return self.searchBST(root.right,val)
+
```
迭代法:
@@ -243,4 +248,4 @@ Go:
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
-
\ No newline at end of file
+
From b857e4bcbd6b2cf2ae9df3224ec218000735eedc Mon Sep 17 00:00:00 2001
From: QuinnDK <39618652+QuinnDK@users.noreply.github.com>
Date: Tue, 18 May 2021 21:19:49 +0800
Subject: [PATCH 05/31] =?UTF-8?q?Update=200232.=E7=94=A8=E6=A0=88=E5=AE=9E?=
=?UTF-8?q?=E7=8E=B0=E9=98=9F=E5=88=97.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0232.用栈实现队列.md | 69 ++++++++++++++++++++++++++++-
1 file changed, 68 insertions(+), 1 deletion(-)
diff --git a/problems/0232.用栈实现队列.md b/problems/0232.用栈实现队列.md
index abdc363d..8bf646a4 100644
--- a/problems/0232.用栈实现队列.md
+++ b/problems/0232.用栈实现队列.md
@@ -190,6 +190,73 @@ Python:
Go:
+```Go
+type MyQueue struct {
+ stack []int
+ back []int
+}
+
+/** Initialize your data structure here. */
+func Constructor() MyQueue {
+ return MyQueue{
+ stack: make([]int, 0),
+ back: make([]int, 0),
+ }
+}
+
+/** Push element x to the back of queue. */
+func (this *MyQueue) Push(x int) {
+ for len(this.back) != 0 {
+ val := this.back[len(this.back)-1]
+ this.back = this.back[:len(this.back)-1]
+ this.stack = append(this.stack, val)
+ }
+ this.stack = append(this.stack, x)
+}
+
+/** Removes the element from in front of queue and returns that element. */
+func (this *MyQueue) Pop() int {
+ for len(this.stack) != 0 {
+ val := this.stack[len(this.stack)-1]
+ this.stack = this.stack[:len(this.stack)-1]
+ this.back = append(this.back, val)
+ }
+ if len(this.back) == 0 {
+ return 0
+ }
+ val := this.back[len(this.back)-1]
+ this.back = this.back[:len(this.back)-1]
+ return val
+}
+
+/** Get the front element. */
+func (this *MyQueue) Peek() int {
+ for len(this.stack) != 0 {
+ val := this.stack[len(this.stack)-1]
+ this.stack = this.stack[:len(this.stack)-1]
+ this.back = append(this.back, val)
+ }
+ if len(this.back) == 0 {
+ return 0
+ }
+ val := this.back[len(this.back)-1]
+ return val
+}
+
+/** Returns whether the queue is empty. */
+func (this *MyQueue) Empty() bool {
+ return len(this.stack) == 0 && len(this.back) == 0
+}
+
+/**
+ * Your MyQueue object will be instantiated and called as such:
+ * obj := Constructor();
+ * obj.Push(x);
+ * param_2 := obj.Pop();
+ * param_3 := obj.Peek();
+ * param_4 := obj.Empty();
+ */
+ ```
@@ -198,4 +265,4 @@ Go:
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
-
\ No newline at end of file
+
From dcd7962774eba5e0092c514a26e10d9a7d857719 Mon Sep 17 00:00:00 2001
From: QuinnDK <39618652+QuinnDK@users.noreply.github.com>
Date: Tue, 18 May 2021 21:22:33 +0800
Subject: [PATCH 06/31] =?UTF-8?q?Update=200516.=E6=9C=80=E9=95=BF=E5=9B=9E?=
=?UTF-8?q?=E6=96=87=E5=AD=90=E5=BA=8F=E5=88=97.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0516.最长回文子序列.md | 30 +++++++++++++++++++++++++-
1 file changed, 29 insertions(+), 1 deletion(-)
diff --git a/problems/0516.最长回文子序列.md b/problems/0516.最长回文子序列.md
index d7acef99..d2253a4e 100644
--- a/problems/0516.最长回文子序列.md
+++ b/problems/0516.最长回文子序列.md
@@ -173,7 +173,35 @@ Python:
Go:
-
+```Go
+func longestPalindromeSubseq(s string) int {
+ str:=[]byte(s)
+ dp:=make([][]int,len(s))
+ for i:=0;i=0;j--{
+ if str[i]==str[j]{
+ if j==i-1{
+ dp[j][i]=2
+ }else{
+ dp[j][i]=dp[j+1][i-1]+2
+ }
+ }else{
+ dp[j][i]=Max(dp[j+1][i],dp[j][i-1])
+ }
+ }
+ }
+ return dp[0][len(s)-1]
+}
+func Max(a,b int)int{
+ if a>b{
+ return a
+ }
+ return b
+}
+```
From 53168265e88a903257da119c1522a081675b2fbd Mon Sep 17 00:00:00 2001
From: jojoo15 <75017412+jojoo15@users.noreply.github.com>
Date: Tue, 18 May 2021 15:35:24 +0200
Subject: [PATCH 07/31] =?UTF-8?q?Update=200098.=E9=AA=8C=E8=AF=81=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
python version of code added
---
problems/0098.验证二叉搜索树.md | 24 +++++++++++++++++++++---
1 file changed, 21 insertions(+), 3 deletions(-)
diff --git a/problems/0098.验证二叉搜索树.md b/problems/0098.验证二叉搜索树.md
index d8945eff..a3f78c39 100644
--- a/problems/0098.验证二叉搜索树.md
+++ b/problems/0098.验证二叉搜索树.md
@@ -336,8 +336,26 @@ class Solution {
```
Python:
-
-
+```python
+# Definition for a binary tree node.
+# class TreeNode:
+# def __init__(self, val=0, left=None, right=None):
+# self.val = val
+# self.left = left
+# self.right = right
+//递归法
+class Solution:
+ def isValidBST(self, root: TreeNode) -> bool:
+ res = [] //把二叉搜索树按中序遍历写成list
+ def buildalist(root):
+ if not root: return
+ buildalist(root.left) //左
+ res.append(root.val) //中
+ buildalist(root.right) //右
+ return res
+ buildalist(root)
+ return res == sorted(res) and len(set(res)) == len(res) //检查list里的数有没有重复元素,以及是否按从小到大排列
+```
Go:
```Go
import "math"
@@ -365,4 +383,4 @@ func isBST(root *TreeNode, min, max int) bool {
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
-
\ No newline at end of file
+
From 4f96f64e3d6f54eac978bdea355c22060f80d1ee Mon Sep 17 00:00:00 2001
From: jojoo15 <75017412+jojoo15@users.noreply.github.com>
Date: Tue, 18 May 2021 15:59:48 +0200
Subject: [PATCH 08/31] =?UTF-8?q?Update=200530.=E4=BA=8C=E5=8F=89=E6=90=9C?=
=?UTF-8?q?=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F=E7=BB=9D=E5=AF=B9?=
=?UTF-8?q?=E5=B7=AE.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
added python version of code
---
.../0530.二叉搜索树的最小绝对差.md | 27 ++++++++++++++++---
1 file changed, 24 insertions(+), 3 deletions(-)
diff --git a/problems/0530.二叉搜索树的最小绝对差.md b/problems/0530.二叉搜索树的最小绝对差.md
index 903ebf78..347d66d1 100644
--- a/problems/0530.二叉搜索树的最小绝对差.md
+++ b/problems/0530.二叉搜索树的最小绝对差.md
@@ -177,8 +177,29 @@ class Solution {
```
Python:
-
-
+```python
+# Definition for a binary tree node.
+# class TreeNode:
+# def __init__(self, val=0, left=None, right=None):
+# self.val = val
+# self.left = left
+# self.right = right
+class Solution:
+ def getMinimumDifference(self, root: TreeNode) -> int:
+ res = []
+ r = float("inf")
+ def buildaList(root): //把二叉搜索树转换成有序数组
+ if not root: return None
+ if root.left: buildaList(root.left) //左
+ res.append(root.val) //中
+ if root.right: buildaList(root.right) //右
+ return res
+
+ buildaList(root)
+ for i in range(len(res)-1): // 统计有序数组的最小差值
+ r = min(abs(res[i]-res[i+1]),r)
+ return r
+```
Go:
@@ -188,4 +209,4 @@ Go:
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
-
\ No newline at end of file
+
From 41272638670e6da93171dfce8af63d5da42d4e36 Mon Sep 17 00:00:00 2001
From: "qingyi.liu"
Date: Tue, 18 May 2021 22:51:51 +0800
Subject: [PATCH 09/31] =?UTF-8?q?=E6=B7=BB=E5=8A=A0111.=E4=BA=8C=E5=8F=89?=
=?UTF-8?q?=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F=E6=B7=B1=E5=BA=A6=20JavaSc?=
=?UTF-8?q?ript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0111.二叉树的最小深度.md | 47 +++++++++++++++++++++++
1 file changed, 47 insertions(+)
diff --git a/problems/0111.二叉树的最小深度.md b/problems/0111.二叉树的最小深度.md
index 01b6c89c..5605e13d 100644
--- a/problems/0111.二叉树的最小深度.md
+++ b/problems/0111.二叉树的最小深度.md
@@ -302,6 +302,53 @@ class Solution:
Go:
+JavaScript:
+
+递归法:
+
+```javascript
+/**
+ * @param {TreeNode} root
+ * @return {number}
+ */
+var minDepth1 = function(root) {
+ if(!root) return 0;
+ // 到叶子节点 返回 1
+ if(!root.left && !root.right) return 1;
+ // 只有右节点时 递归右节点
+ if(!root.left) return 1 + minDepth(root.right);、
+ // 只有左节点时 递归左节点
+ if(!root.right) return 1 + minDepth(root.left);
+ return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
+};
+```
+
+迭代法:
+
+```javascript
+/**
+* @param {TreeNode} root
+* @return {number}
+*/
+var minDepth = function(root) {
+ if(!root) return 0;
+ const queue = [root];
+ let dep = 0;
+ while(true) {
+ let size = queue.length;
+ dep++;
+ while(size--){
+ const node = queue.shift();
+ // 到第一个叶子节点 返回 当前深度
+ if(!node.left && !node.right) return dep;
+ node.left && queue.push(node.left);
+ node.right && queue.push(node.right);
+ }
+ }
+};
+```
+
+
-----------------------
From cf6ec4f65a8ee43a15c29f0a380cfac5f809a8dd Mon Sep 17 00:00:00 2001
From: X-shuffle <53906918+X-shuffle@users.noreply.github.com>
Date: Tue, 18 May 2021 22:58:00 +0800
Subject: [PATCH 10/31] =?UTF-8?q?Update=20=E4=BA=8C=E5=8F=89=E6=A0=91?=
=?UTF-8?q?=E7=9A=84=E8=BF=AD=E4=BB=A3=E9=81=8D=E5=8E=86.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
添加 二叉树的迭代遍历 GO版本
---
problems/二叉树的迭代遍历.md | 102 +++++++++++++++++++++++++++
1 file changed, 102 insertions(+)
diff --git a/problems/二叉树的迭代遍历.md b/problems/二叉树的迭代遍历.md
index 2647616b..d228077b 100644
--- a/problems/二叉树的迭代遍历.md
+++ b/problems/二叉树的迭代遍历.md
@@ -163,7 +163,109 @@ Python:
Go:
+> 迭代法前序遍历
+```go
+//迭代法前序遍历
+/**
+ type Element struct {
+ // 元素保管的值
+ Value interface{}
+ // 内含隐藏或非导出字段
+}
+
+func (l *List) Back() *Element
+前序遍历:中左右
+压栈顺序:右左中
+ **/
+func preorderTraversal(root *TreeNode) []int {
+ if root == nil {
+ return nil
+ }
+ var stack = list.New()
+ stack.PushBack(root.Right)
+ stack.PushBack(root.Left)
+ res:=[]int{}
+ res=append(res,root.Val)
+ for stack.Len()>0 {
+ e:=stack.Back()
+ stack.Remove(e)
+ node := e.Value.(*TreeNode)//e是Element类型,其值为e.Value.由于Value为接口,所以要断言
+ if node==nil{
+ continue
+ }
+ res=append(res,node.Val)
+ stack.PushBack(node.Right)
+ stack.PushBack(node.Left)
+ }
+ return res
+}
+```
+
+> 迭代法后序遍历
+
+```go
+//后续遍历:左右中
+//压栈顺序:中右左(按照前序遍历思路),再反转结果数组
+func postorderTraversal(root *TreeNode) []int {
+ if root == nil {
+ return nil
+ }
+ var stack = list.New()
+ stack.PushBack(root.Left)
+ stack.PushBack(root.Right)
+ res:=[]int{}
+ res=append(res,root.Val)
+ for stack.Len()>0 {
+ e:=stack.Back()
+ stack.Remove(e)
+ node := e.Value.(*TreeNode)//e是Element类型,其值为e.Value.由于Value为接口,所以要断言
+ if node==nil{
+ continue
+ }
+ res=append(res,node.Val)
+ stack.PushBack(node.Left)
+ stack.PushBack(node.Right)
+ }
+ for i:=0;i 迭代法中序遍历
+
+```go
+//迭代法中序遍历
+func inorderTraversal(root *TreeNode) []int {
+ rootRes:=[]int{}
+ if root==nil{
+ return nil
+ }
+ stack:=list.New()
+ node:=root
+ //先将所有左节点找到,加入栈中
+ for node!=nil{
+ stack.PushBack(node)
+ node=node.Left
+ }
+ //其次对栈中的每个节点先弹出加入到结果集中,再找到该节点的右节点的所有左节点加入栈中
+ for stack.Len()>0{
+ e:=stack.Back()
+ node:=e.Value.(*TreeNode)
+ stack.Remove(e)
+ //找到该节点的右节点,再搜索他的所有左节点加入栈中
+ rootRes=append(rootRes,node.Val)
+ node=node.Right
+ for node!=nil{
+ stack.PushBack(node)
+ node=node.Left
+ }
+ }
+ return rootRes
+}
+```
From 1fcb8402232e4db9fb1af3312232f7c2e3f26ee5 Mon Sep 17 00:00:00 2001
From: tw2665 <55668073+tw2665@users.noreply.github.com>
Date: Tue, 18 May 2021 15:45:10 -0400
Subject: [PATCH 11/31] =?UTF-8?q?Update=200454.=E5=9B=9B=E6=95=B0=E7=9B=B8?=
=?UTF-8?q?=E5=8A=A0II.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0454.四数相加II.md | 31 +++++++++++++++++++++++++++++++
1 file changed, 31 insertions(+)
diff --git a/problems/0454.四数相加II.md b/problems/0454.四数相加II.md
index a2ef928b..ad928a3f 100644
--- a/problems/0454.四数相加II.md
+++ b/problems/0454.四数相加II.md
@@ -121,6 +121,37 @@ class Solution {
Python:
+```
+class Solution(object):
+ def fourSumCount(self, nums1, nums2, nums3, nums4):
+ """
+ :type nums1: List[int]
+ :type nums2: List[int]
+ :type nums3: List[int]
+ :type nums4: List[int]
+ :rtype: int
+ """
+ # use a dict to store the elements in nums1 and nums2 and their sum
+ hashmap = dict()
+ for n1 in nums1:
+ for n2 in nums2:
+ if n1 + n2 in hashmap:
+ hashmap[n1+n2] += 1
+ else:
+ hashmap[n1+n2] = 1
+
+ # if the -(a+b) exists in nums3 and nums4, we shall add the count
+ count = 0
+ for n3 in nums3:
+ for n4 in nums4:
+ key = - n3 - n4
+ if key in hashmap:
+ count += hashmap[key]
+ return count
+
+
+```
+
Go:
From 830381313abb85357f180f6315e0e1c28c63c01d Mon Sep 17 00:00:00 2001
From: tw2665 <55668073+tw2665@users.noreply.github.com>
Date: Tue, 18 May 2021 15:53:09 -0400
Subject: [PATCH 12/31] =?UTF-8?q?Update=200383.=E8=B5=8E=E9=87=91=E4=BF=A1?=
=?UTF-8?q?.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0383.赎金信.md | 29 ++++++++++++++++++++++++++++-
1 file changed, 28 insertions(+), 1 deletion(-)
diff --git a/problems/0383.赎金信.md b/problems/0383.赎金信.md
index 2f7ae4d8..527945fa 100644
--- a/problems/0383.赎金信.md
+++ b/problems/0383.赎金信.md
@@ -136,7 +136,34 @@ class Solution {
```
Python:
-
+```
+class Solution(object):
+ def canConstruct(self, ransomNote, magazine):
+ """
+ :type ransomNote: str
+ :type magazine: str
+ :rtype: bool
+ """
+
+ # use a dict to store the number of letter occurance in ransomNote
+ hashmap = dict()
+ for s in ransomNote:
+ if s in hashmap:
+ hashmap[s] += 1
+ else:
+ hashmap[s] = 1
+
+ # check if the letter we need can be found in magazine
+ for l in magazine:
+ if l in hashmap:
+ hashmap[l] -= 1
+
+ for key in hashmap:
+ if hashmap[key] > 0:
+ return False
+
+ return True
+```
Go:
From 9edc7aa01df2f652a170435870747a0896b0d83e Mon Sep 17 00:00:00 2001
From: charon2121 <66763177+charon2121@users.noreply.github.com>
Date: Wed, 19 May 2021 08:28:50 +0800
Subject: [PATCH 13/31] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20144.=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E5=89=8D=E5=BA=8F=E9=81=8D=E5=8E=86?=
=?UTF-8?q?=20python3=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
更新 145.二叉树的后序遍历 python3版本
更新 94.二叉树的中序遍历 python3版本
---
problems/二叉树的迭代遍历.md | 62 ++++++++++++++++++++++++++++
1 file changed, 62 insertions(+)
diff --git a/problems/二叉树的迭代遍历.md b/problems/二叉树的迭代遍历.md
index 2647616b..be2456de 100644
--- a/problems/二叉树的迭代遍历.md
+++ b/problems/二叉树的迭代遍历.md
@@ -160,6 +160,68 @@ Java:
Python:
+```python3
+# 前序遍历-迭代-LC144_二叉树的前序遍历
+class Solution:
+ def preorderTraversal(self, root: TreeNode) -> List[int]:
+ # 根结点为空则返回空列表
+ if not root:
+ return []
+ stack = [root]
+ result = []
+ while stack:
+ node = stack.pop()
+ # 中结点先处理
+ result.append(node.val)
+ # 右孩子先入栈
+ if node.right:
+ stack.append(node.right)
+ # 左孩子后入栈
+ if node.left:
+ stack.append(node.left)
+ return result
+
+# 中序遍历-迭代-LC94_二叉树的中序遍历
+class Solution:
+ def inorderTraversal(self, root: TreeNode) -> List[int]:
+ if not root:
+ return []
+ stack = [] # 不能提前将root结点加入stack中
+ result = []
+ cur = root
+ while cur or stack:
+ # 先迭代访问最底层的左子树结点
+ if cur:
+ stack.append(cur)
+ cur = cur.left
+ # 到达最左结点后处理栈顶结点
+ else:
+ cur = stack.pop()
+ result.append(cur.val)
+ # 取栈顶元素右结点
+ cur = cur.right
+ return result
+
+# 后序遍历-迭代-LC145_二叉树的后序遍历
+class Solution:
+ def postorderTraversal(self, root: TreeNode) -> List[int]:
+ if not root:
+ return []
+ stack = [root]
+ result = []
+ while stack:
+ node = stack.pop()
+ # 中结点先处理
+ result.append(node.val)
+ # 左孩子先入栈
+ if node.left:
+ stack.append(node.left)
+ # 右孩子后入栈
+ if node.right:
+ stack.append(node.right)
+ # 将最终的数组翻转
+ return result[::-1]
+```
Go:
From 44c8ce6a4f54869008961c62d2728548b6f24e04 Mon Sep 17 00:00:00 2001
From: X-shuffle <53906918+X-shuffle@users.noreply.github.com>
Date: Wed, 19 May 2021 10:03:09 +0800
Subject: [PATCH 14/31] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E4=BA=8C=E5=8F=89?=
=?UTF-8?q?=E6=A0=91=E7=9A=84=E8=BF=AD=E4=BB=A3=E9=81=8D=E5=8E=86=20GO?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
添加 二叉树的迭代遍历 GO版本
---
problems/二叉树的迭代遍历.md | 1 -
1 file changed, 1 deletion(-)
diff --git a/problems/二叉树的迭代遍历.md b/problems/二叉树的迭代遍历.md
index d228077b..10e7e7b4 100644
--- a/problems/二叉树的迭代遍历.md
+++ b/problems/二叉树的迭代遍历.md
@@ -268,7 +268,6 @@ func inorderTraversal(root *TreeNode) []int {
```
-
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
From a57b6385c4ceccb7900cb9dc41a7eb3d6a59d2e5 Mon Sep 17 00:00:00 2001
From: X-shuffle <53906918+X-shuffle@users.noreply.github.com>
Date: Wed, 19 May 2021 10:07:22 +0800
Subject: [PATCH 15/31] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E4=BA=8C=E5=8F=89?=
=?UTF-8?q?=E6=A0=91=E7=9A=84=E8=BF=AD=E4=BB=A3=E9=81=8D=E5=8E=86=20GO?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
添加 二叉树的迭代遍历 GO版本
---
problems/二叉树的迭代遍历.md | 1 +
1 file changed, 1 insertion(+)
diff --git a/problems/二叉树的迭代遍历.md b/problems/二叉树的迭代遍历.md
index 10e7e7b4..a1906469 100644
--- a/problems/二叉树的迭代遍历.md
+++ b/problems/二叉树的迭代遍历.md
@@ -205,6 +205,7 @@ func preorderTraversal(root *TreeNode) []int {
> 迭代法后序遍历
```go
+//迭代法后序遍历
//后续遍历:左右中
//压栈顺序:中右左(按照前序遍历思路),再反转结果数组
func postorderTraversal(root *TreeNode) []int {
From c628620ceddbdbe236b1cbb6cc597431642ae260 Mon Sep 17 00:00:00 2001
From: QuinnDK <39618652+QuinnDK@users.noreply.github.com>
Date: Wed, 19 May 2021 10:07:40 +0800
Subject: [PATCH 16/31] =?UTF-8?q?Update=200516.=E6=9C=80=E9=95=BF=E5=9B=9E?=
=?UTF-8?q?=E6=96=87=E5=AD=90=E5=BA=8F=E5=88=97.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0516.最长回文子序列.md | 27 ++++++++++++++++++++++++++
1 file changed, 27 insertions(+)
diff --git a/problems/0516.最长回文子序列.md b/problems/0516.最长回文子序列.md
index d7acef99..7736b69f 100644
--- a/problems/0516.最长回文子序列.md
+++ b/problems/0516.最长回文子序列.md
@@ -173,6 +173,33 @@ Python:
Go:
+```Go
+func longestPalindromeSubseq(s string) int {
+ lenth:=len(s)
+ dp:=make([][]int,lenth)
+ for i:=0;i=0;i--{
+ for j:=i+1;j
Date: Wed, 19 May 2021 10:25:58 +0800
Subject: [PATCH 17/31] =?UTF-8?q?Update=200055.=E8=B7=B3=E8=B7=83=E6=B8=B8?=
=?UTF-8?q?=E6=88=8F.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0055.跳跃游戏.md | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/problems/0055.跳跃游戏.md b/problems/0055.跳跃游戏.md
index a25c831a..ae2cbd1e 100644
--- a/problems/0055.跳跃游戏.md
+++ b/problems/0055.跳跃游戏.md
@@ -122,6 +122,24 @@ class Solution:
```
Go:
+```Go
+func canJUmp(nums []int) bool {
+ if len(nums)<=1{
+ return true
+ }
+ dp:=make([]bool,len(nums))
+ dp[0]=true
+ for i:=1;i=0;j--{
+ if dp[j]&&nums[j]+j>=i{
+ dp[i]=true
+ break
+ }
+ }
+ }
+ return dp[len(nums)-1]
+}
+```
From 7a5008ef78993ce5b538148082f70dab3520947a Mon Sep 17 00:00:00 2001
From: QuinnDK <39618652+QuinnDK@users.noreply.github.com>
Date: Wed, 19 May 2021 10:50:27 +0800
Subject: [PATCH 18/31] =?UTF-8?q?Update=200045.=E8=B7=B3=E8=B7=83=E6=B8=B8?=
=?UTF-8?q?=E6=88=8FII.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0045.跳跃游戏II.md | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)
diff --git a/problems/0045.跳跃游戏II.md b/problems/0045.跳跃游戏II.md
index f65ceb9e..9b303a7c 100644
--- a/problems/0045.跳跃游戏II.md
+++ b/problems/0045.跳跃游戏II.md
@@ -193,6 +193,26 @@ class Solution:
```
Go:
+```Go
+func jump(nums []int) int {
+ dp:=make([]int ,len(nums))
+ dp[0]=0
+
+ for i:=1;ii{
+ dp[i]=min(dp[j]+1,dp[i])
+ }
+ }
+ }
+ return dp[len(nums)-1]
+}
+/*
+dp[i]表示从起点到当前位置的最小跳跃次数
+dp[i]=min(dp[j]+1,dp[i]) 表示从j位置用一步跳跃到当前位置,这个j位置可能有很多个,却最小一个就可以
+*/
+```
From 22bd2751f622aa3019045dbca0624384c411d164 Mon Sep 17 00:00:00 2001
From: HuJian <502361472@qq.com>
Date: Wed, 19 May 2021 10:56:39 +0800
Subject: [PATCH 19/31] =?UTF-8?q?=E6=9B=B4=E6=96=B00704.=E4=BA=8C=E5=88=86?=
=?UTF-8?q?=E6=9F=A5=E6=89=BE,java=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0704.二分查找.md | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/problems/0704.二分查找.md b/problems/0704.二分查找.md
index c7207364..8b000e41 100644
--- a/problems/0704.二分查找.md
+++ b/problems/0704.二分查找.md
@@ -153,6 +153,10 @@ Java:
```java
class Solution {
public int search(int[] nums, int target) {
+ // 避免当 target 小于nums[0] nums[nums.length - 1]时多次循环运算
+ if (target < nums[0] || target > nums[nums.length - 1]) {
+ return -1;
+ }
int left = 0, right = nums.length - 1;
while (left <= right) {
int mid = left + ((right - left) >> 1);
From 7253dc9bc7aaec1fb8235d41ea75aa5eae1f606f Mon Sep 17 00:00:00 2001
From: "qingyi.liu"
Date: Wed, 19 May 2021 11:08:53 +0800
Subject: [PATCH 20/31] =?UTF-8?q?=E6=B7=BB=E5=8A=A019.=E5=88=A0=E9=99=A4?=
=?UTF-8?q?=E9=93=BE=E8=A1=A8=E7=9A=84=E5=80=92=E6=95=B0=E7=AC=ACN?=
=?UTF-8?q?=E4=B8=AA=E8=8A=82=E7=82=B9javascript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...0019.删除链表的倒数第N个节点.md | 22 +++++++++++++++++++
1 file changed, 22 insertions(+)
diff --git a/problems/0019.删除链表的倒数第N个节点.md b/problems/0019.删除链表的倒数第N个节点.md
index 3b89dabd..920c8110 100644
--- a/problems/0019.删除链表的倒数第N个节点.md
+++ b/problems/0019.删除链表的倒数第N个节点.md
@@ -135,6 +135,28 @@ func removeNthFromEnd(head *ListNode, n int) *ListNode {
}
```
+JavaScript:
+
+```js
+/**
+ * @param {ListNode} head
+ * @param {number} n
+ * @return {ListNode}
+ */
+var removeNthFromEnd = function(head, n) {
+ let ret = new ListNode(0, head),
+ slow = fast = ret;
+ while(n--) fast = fast.next;
+ if(!fast) return ret.next;
+ while (fast.next) {
+ fast = fast.next;
+ slow = slow.next
+ };
+ slow.next = slow.next.next;
+ return ret.next;
+};
+```
+
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
From 0a7682998d60cf02caab772ba74955baf1ad806e Mon Sep 17 00:00:00 2001
From: "qingyi.liu"
Date: Wed, 19 May 2021 11:22:07 +0800
Subject: [PATCH 21/31] =?UTF-8?q?=E6=B7=BB=E5=8A=A0142.=E7=8E=AF=E5=BD=A2?=
=?UTF-8?q?=E9=93=BE=E8=A1=A8IIjavascript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0142.环形链表II.md | 44 +++++++++++++++++++++++++++++++++
1 file changed, 44 insertions(+)
diff --git a/problems/0142.环形链表II.md b/problems/0142.环形链表II.md
index b3a53c07..71f5cc4c 100644
--- a/problems/0142.环形链表II.md
+++ b/problems/0142.环形链表II.md
@@ -258,6 +258,50 @@ Go:
}
```
+javaScript
+
+```js
+// 两种循环实现方式
+/**
+ * @param {ListNode} head
+ * @return {ListNode}
+ */
+// 先判断是否是环形链表
+var detectCycle = function(head) {
+ if(!head || !head.next) return null;
+ let slow =head.next, fast = head.next.next;
+ while(fast && fast.next && fast!== slow) {
+ slow = slow.next;
+ fast = fast.next.next;
+ }
+ if(!fast || !fast.next ) return null;
+ slow = head;
+ while (fast !== slow) {
+ slow = slow.next;
+ fast = fast.next;
+ }
+ return slow;
+};
+
+var detectCycle = function(head) {
+ if(!head || !head.next) return null;
+ let slow =head.next, fast = head.next.next;
+ while(fast && fast.next) {
+ slow = slow.next;
+ fast = fast.next.next;
+ if(fast == slow) {
+ slow = head;
+ while (fast !== slow) {
+ slow = slow.next;
+ fast = fast.next;
+ }
+ return slow;
+ }
+ }
+ return null;
+};
+```
+
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
From c175eaf471fd370b5d6a46767a9e0ffe726a9a4a Mon Sep 17 00:00:00 2001
From: NevS <1173325467@qq.com>
Date: Wed, 19 May 2021 11:33:21 +0800
Subject: [PATCH 22/31] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20704.=20=E4=BA=8C?=
=?UTF-8?q?=E5=88=86=E6=9F=A5=E6=89=BE=20go=20=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0704.二分查找.md | 43 ++++++++++++++++++++++++++++++++++-
1 file changed, 42 insertions(+), 1 deletion(-)
diff --git a/problems/0704.二分查找.md b/problems/0704.二分查找.md
index c7207364..96bb1fed 100644
--- a/problems/0704.二分查找.md
+++ b/problems/0704.二分查找.md
@@ -210,6 +210,47 @@ class Solution:
Go:
+(版本一)左闭右闭区间
+
+```go
+func search(nums []int, target int) int {
+ high := len(nums)-1
+ low := 0
+ for low <= high {
+ mid := low + (high-low)/2
+ if nums[mid] == target {
+ return mid
+ } else if nums[mid] > target {
+ high = mid-1
+ } else {
+ low = mid+1
+ }
+ }
+ return -1
+}
+```
+
+(版本二)左闭右开区间
+
+```go
+func search(nums []int, target int) int {
+ high := len(nums)
+ low := 0
+ for low < high {
+ mid := low + (high-low)/2
+ if nums[mid] == target {
+ return mid
+ } else if nums[mid] > target {
+ high = mid
+ } else {
+ low = mid+1
+ }
+ }
+ return -1
+}
+```
+
+
@@ -217,4 +258,4 @@ Go:
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
-
\ No newline at end of file
+
From 3f16de6462d9d80bf44ec8d0adc95988fccbd028 Mon Sep 17 00:00:00 2001
From: "qingyi.liu"
Date: Wed, 19 May 2021 11:42:18 +0800
Subject: [PATCH 23/31] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=AC=AC202=E9=A2=98.?=
=?UTF-8?q?=20=E5=BF=AB=E4=B9=90=E6=95=B0javascript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0202.快乐数.md | 34 ++++++++++++++++++++++++++++++++++
1 file changed, 34 insertions(+)
diff --git a/problems/0202.快乐数.md b/problems/0202.快乐数.md
index 8c0dd1e7..90228b8c 100644
--- a/problems/0202.快乐数.md
+++ b/problems/0202.快乐数.md
@@ -112,6 +112,40 @@ Python:
Go:
+javaScript:
+
+```js
+function getN(n) {
+ if (n == 1 || n == 0) return n;
+ let res = 0;
+ while (n) {
+ res += (n % 10) * (n % 10);
+ n = parseInt(n / 10);
+ }
+ return res;
+}
+
+var isHappy = function(n) {
+ const sumSet = new Set();
+ while (n != 1 && !sumSet.has(n)) {
+ sumSet.add(n);
+ n = getN(n);
+ }
+ return n == 1;
+};
+
+// 使用环形链表的思想 说明出现闭环 退出循环
+var isHappy = function(n) {
+ if (getN(n) == 1) return true;
+ let a = getN(n), b = getN(getN(n));
+ // 如果 a === b
+ while (b !== 1 && getN(b) !== 1 && a !== b) {
+ a = getN(a);
+ b = getN(getN(b));
+ }
+ return b === 1 || getN(b) === 1 ;
+};
+```
From 142c4bb83dbb97eaba1616afdca72292add9d0e0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B8=A5=E7=95=85?=
Date: Wed, 19 May 2021 14:20:31 +0800
Subject: [PATCH 24/31] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E0077=E7=BB=84?=
=?UTF-8?q?=E5=90=88python=E3=80=81JavaScript=E8=A7=A3=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0077.组合.md | 39 ++++++++++++++++++++++++++++++++++++++-
1 file changed, 38 insertions(+), 1 deletion(-)
diff --git a/problems/0077.组合.md b/problems/0077.组合.md
index 1f0d6eb1..d9d4b17f 100644
--- a/problems/0077.组合.md
+++ b/problems/0077.组合.md
@@ -370,8 +370,45 @@ class Solution {
Python:
+```python
+class Solution:
+ result: List[List[int]] = []
+ path: List[int] = []
+ def combine(self, n: int, k: int) -> List[List[int]]:
+ self.result = []
+ self.combineHelper(n, k, 1)
+ return self.result
-
+ def combineHelper(self, n: int, k: int, startIndex: int):
+ if (l := len(self.path)) == k:
+ self.result.append(self.path.copy())
+ return
+ for i in range(startIndex, n - (k - l) + 2):
+ self.path.append(i)
+ self.combineHelper(n, k, i + 1)
+ self.path.pop()
+```
+javascript
+```javascript
+let result = []
+let path = []
+var combine = function(n, k) {
+ result = []
+ combineHelper(n, k, 1)
+ return result
+};
+const combineHelper = (n, k, startIndex) => {
+ if (path.length === k) {
+ result.push([...path])
+ return
+ }
+ for (let i = startIndex; i <= n - (k - path.length) + 1; ++i) {
+ path.push(i)
+ combineHelper(n, k, i + 1)
+ path.pop()
+ }
+}
+```
Go:
```Go
var res [][]int
From 1b635d82100bda2d956bed9d6ebbc22163d12b88 Mon Sep 17 00:00:00 2001
From: QuinnDK <39618652+QuinnDK@users.noreply.github.com>
Date: Wed, 19 May 2021 14:28:01 +0800
Subject: [PATCH 25/31] =?UTF-8?q?Update=200139.=E5=8D=95=E8=AF=8D=E6=8B=86?=
=?UTF-8?q?=E5=88=86.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0139.单词拆分.md | 22 ++++++++++++++++++++--
1 file changed, 20 insertions(+), 2 deletions(-)
diff --git a/problems/0139.单词拆分.md b/problems/0139.单词拆分.md
index c6d8e43b..46ed641e 100644
--- a/problems/0139.单词拆分.md
+++ b/problems/0139.单词拆分.md
@@ -254,7 +254,25 @@ Python:
Go:
-
+```Go
+func wordBreak(s string,wordDict []string) bool {
+ wordDictSet:=make(map[string]bool)
+ for _,w:=range wordDict{
+ wordDictSet[w]=true
+ }
+ dp:=make([]bool,len(s)+1)
+ dp[0]=true
+ for i:=1;i<=len(s);i++{
+ for j:=0;j
\ No newline at end of file
+
From b8bb7786a41a78bca103f686398fd2801e43bdbb Mon Sep 17 00:00:00 2001
From: hk27xing <244798299@qq.com>
Date: Wed, 19 May 2021 16:23:04 +0800
Subject: [PATCH 26/31] =?UTF-8?q?=E6=B7=BB=E5=8A=A00459.=E9=87=8D=E5=A4=8D?=
=?UTF-8?q?=E7=9A=84=E5=AD=90=E5=AD=97=E7=AC=A6=E4=B8=B2Java=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0459.重复的子字符串.md | 31 +++++++++++++++++++++++++-
1 file changed, 30 insertions(+), 1 deletion(-)
diff --git a/problems/0459.重复的子字符串.md b/problems/0459.重复的子字符串.md
index 5abc999a..3a5f3b17 100644
--- a/problems/0459.重复的子字符串.md
+++ b/problems/0459.重复的子字符串.md
@@ -149,9 +149,38 @@ public:
## 其他语言版本
-
Java:
+```java
+class Solution {
+ public boolean repeatedSubstringPattern(String s) {
+ if (s.equals("")) return false;
+
+ int len = s.length();
+ // 原串加个空格(哨兵),使下标从1开始,这样j从0开始,也不用初始化了
+ s = " " + s;
+ char[] chars = s.toCharArray();
+ int[] next = new int[len + 1];
+
+ // 构造 next 数组过程,j从0开始(空格),i从2开始
+ for (int i = 2, j = 0; i <= len; i++) {
+ // 匹配不成功,j回到前一位置 next 数组所对应的值
+ while (j > 0 && chars[i] != chars[j + 1]) j = next[j];
+ // 匹配成功,j往后移
+ if (chars[i] == chars[j + 1]) j++;
+ // 更新 next 数组的值
+ next[i] = j;
+ }
+
+ // 最后判断是否是重复的子字符串,这里 next[len] 即代表next数组末尾的值
+ if (next[len] > 0 && len % (len - next[len]) == 0) {
+ return true;
+ }
+ return false;
+ }
+}
+```
+
Python:
From 8c77bb2565658fb19c3d1159988ff6c50804876b Mon Sep 17 00:00:00 2001
From: "qingyi.liu"
Date: Wed, 19 May 2021 18:45:14 +0800
Subject: [PATCH 27/31] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=8C=E5=8F=89?=
=?UTF-8?q?=E6=A0=91=E7=9A=84=E9=80=92=E5=BD=92=E9=81=8D=E5=8E=86javascrip?=
=?UTF-8?q?t=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/二叉树的递归遍历.md | 37 +++++++++++++++++++++++++++-
1 file changed, 36 insertions(+), 1 deletion(-)
diff --git a/problems/二叉树的递归遍历.md b/problems/二叉树的递归遍历.md
index 937ef603..2d571294 100644
--- a/problems/二叉树的递归遍历.md
+++ b/problems/二叉树的递归遍历.md
@@ -226,7 +226,7 @@ func PreorderTraversal(root *TreeNode) (res []int) {
var traversal func(node *TreeNode)
traversal = func(node *TreeNode) {
if node == nil {
- return
+ return
}
res = append(res,node.Val)
traversal(node.Left)
@@ -272,6 +272,41 @@ func PostorderTraversal(root *TreeNode) (res []int) {
}
```
+javaScript:
+
+```js
+
+前序遍历:
+
+var preorderTraversal = function(root, res = []) {
+ if (!root) return res;
+ res.push(root.val);
+ preorderTraversal(root.left, res)
+ preorderTraversal(root.right, res)
+ return res;
+};
+
+中序遍历:
+
+var inorderTraversal = function(root, res = []) {
+ if (!root) return res;
+ inorderTraversal(root.left, res);
+ res.push(root.val);
+ inorderTraversal(root.right, res);
+ return res;
+};
+
+后序遍历:
+
+var postorderTraversal = function(root, res = []) {
+ if (!root) return res;
+ postorderTraversal(root.left, res);
+ postorderTraversal(root.right, res);
+ res.push(root.val);
+ return res;
+};
+```
+
From a5759ce09b133815ecd5e41e6ffc4c7ec4ff04c1 Mon Sep 17 00:00:00 2001
From: nmydt <62681228+nmydt@users.noreply.github.com>
Date: Wed, 19 May 2021 19:42:37 +0800
Subject: [PATCH 28/31] =?UTF-8?q?Update=20=E4=BA=8C=E5=8F=89=E6=A0=91?=
=?UTF-8?q?=E7=9A=84=E7=BB=9F=E4=B8=80=E8=BF=AD=E4=BB=A3=E6=B3=95.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/二叉树的统一迭代法.md | 83 +++++++++++++++++++++++++
1 file changed, 83 insertions(+)
diff --git a/problems/二叉树的统一迭代法.md b/problems/二叉树的统一迭代法.md
index 3ea4eeaf..bc91eca0 100644
--- a/problems/二叉树的统一迭代法.md
+++ b/problems/二叉树的统一迭代法.md
@@ -153,8 +153,91 @@ public:
Java:
+ 迭代法前序遍历代码如下:
+ ```java
+ class Solution {
+
+ public List preorderTraversal(TreeNode root) {
+ List result = new LinkedList<>();
+ Stack st = new Stack<>();
+ if (root != null) st.push(root);
+ while (!st.empty()) {
+ TreeNode node = st.peek();
+ if (node != null) {
+ st.pop(); // 将该节点弹出,避免重复操作,下面再将右中左节点添加到栈中
+ if (node.right!=null) st.push(node.right); // 添加右节点(空节点不入栈)
+ if (node.left!=null) st.push(node.left); // 添加左节点(空节点不入栈)
+ st.push(node); // 添加中节点
+ st.push(null); // 中节点访问过,但是还没有处理,加入空节点做为标记。
+
+ } else { // 只有遇到空节点的时候,才将下一个节点放进结果集
+ st.pop(); // 将空节点弹出
+ node = st.peek(); // 重新取出栈中元素
+ st.pop();
+ result.add(node.val); // 加入到结果集
+ }
+ }
+ return result;
+ }
+}
+ ```
+ 迭代法中序遍历代码如下:
+ ```java
+ class Solution {
+ public List inorderTraversal(TreeNode root) {
+ List result = new LinkedList<>();
+ Stack st = new Stack<>();
+ if (root != null) st.push(root);
+ while (!st.empty()) {
+ TreeNode node = st.peek();
+ if (node != null) {
+ st.pop(); // 将该节点弹出,避免重复操作,下面再将右中左节点添加到栈中
+ if (node.right!=null) st.push(node.right); // 添加右节点(空节点不入栈)
+ st.push(node); // 添加中节点
+ st.push(null); // 中节点访问过,但是还没有处理,加入空节点做为标记。
+ if (node.left!=null) st.push(node.left); // 添加左节点(空节点不入栈)
+ } else { // 只有遇到空节点的时候,才将下一个节点放进结果集
+ st.pop(); // 将空节点弹出
+ node = st.peek(); // 重新取出栈中元素
+ st.pop();
+ result.add(node.val); // 加入到结果集
+ }
+ }
+ return result;
+ }
+}
+ ```
+ 迭代法后序遍历代码如下:
+ ```java
+ class Solution {
+
+ public List postorderTraversal(TreeNode root) {
+ List result = new LinkedList<>();
+ Stack st = new Stack<>();
+ if (root != null) st.push(root);
+ while (!st.empty()) {
+ TreeNode node = st.peek();
+ if (node != null) {
+ st.pop(); // 将该节点弹出,避免重复操作,下面再将右中左节点添加到栈中
+ st.push(node); // 添加中节点
+ st.push(null); // 中节点访问过,但是还没有处理,加入空节点做为标记。
+ if (node.right!=null) st.push(node.right); // 添加右节点(空节点不入栈)
+ if (node.left!=null) st.push(node.left); // 添加左节点(空节点不入栈)
+
+ } else { // 只有遇到空节点的时候,才将下一个节点放进结果集
+ st.pop(); // 将空节点弹出
+ node = st.peek(); // 重新取出栈中元素
+ st.pop();
+ result.add(node.val); // 加入到结果集
+ }
+ }
+ return result;
+ }
+}
+
+ ```
Python:
From 1c267dc06458dfb1708484b0edb20e8f61f7f190 Mon Sep 17 00:00:00 2001
From: "qingyi.liu"
Date: Wed, 19 May 2021 20:05:13 +0800
Subject: [PATCH 29/31] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=8C=E5=8F=89?=
=?UTF-8?q?=E6=A0=91=E7=9A=84=E8=BF=AD=E4=BB=A3=E9=81=8D=E5=8E=86javaScrip?=
=?UTF-8?q?t=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/二叉树的迭代遍历.md | 64 ++++++++++++++++++++++++++++
1 file changed, 64 insertions(+)
diff --git a/problems/二叉树的迭代遍历.md b/problems/二叉树的迭代遍历.md
index 594d498f..60dd73a8 100644
--- a/problems/二叉树的迭代遍历.md
+++ b/problems/二叉树的迭代遍历.md
@@ -330,6 +330,70 @@ func inorderTraversal(root *TreeNode) []int {
}
```
+javaScript
+
+```js
+
+前序遍历:
+
+// 入栈 右 -> 左
+// 出栈 中 -> 左 -> 右
+var preorderTraversal = function(root, res = []) {
+ if(!root) return res;
+ const stack = [root];
+ let cur = null;
+ while(stack.length) {
+ cur = stack.pop();
+ res.push(cur.val);
+ cur.right && stack.push(cur.right);
+ cur.left && stack.push(cur.left);
+ }
+ return res;
+};
+
+中序遍历:
+
+// 入栈 左 -> 右
+// 出栈 左 -> 中 -> 右
+
+var inorderTraversal = function(root, res = []) {
+ const stack = [];
+ let cur = root;
+ while(stack.length || cur) {
+ if(cur) {
+ stack.push(cur);
+ // 左
+ cur = cur.left;
+ } else {
+ // --> 弹出 中
+ cur = stack.pop();
+ res.push(cur.val);
+ // 右
+ cur = cur.right;
+ }
+ };
+ return res;
+};
+
+后序遍历:
+
+// 入栈 左 -> 右
+// 出栈 中 -> 右 -> 左 结果翻转
+
+var postorderTraversal = function(root, res = []) {
+ if (!root) return res;
+ const stack = [root];
+ let cur = null;
+ do {
+ cur = stack.pop();
+ res.push(cur.val);
+ cur.left && stack.push(cur.left);
+ cur.right && stack.push(cur.right);
+ } while(stack.length);
+ return res.reverse();
+};
+```
+
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
From af6340c400999bb0dc2e26d09407ea18b5ef38fb Mon Sep 17 00:00:00 2001
From: nmydt <62681228+nmydt@users.noreply.github.com>
Date: Wed, 19 May 2021 20:51:11 +0800
Subject: [PATCH 30/31] =?UTF-8?q?Update=20=E4=BA=8C=E5=8F=89=E6=A0=91?=
=?UTF-8?q?=E4=B8=AD=E9=80=92=E5=BD=92=E5=B8=A6=E7=9D=80=E5=9B=9E=E6=BA=AF?=
=?UTF-8?q?.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
添加 二叉树:以为使用了递归,其实还隐藏着回溯 Java代码
---
problems/二叉树中递归带着回溯.md | 78 ++++++++++++++++++++++
1 file changed, 78 insertions(+)
diff --git a/problems/二叉树中递归带着回溯.md b/problems/二叉树中递归带着回溯.md
index bf15e39b..3f874332 100644
--- a/problems/二叉树中递归带着回溯.md
+++ b/problems/二叉树中递归带着回溯.md
@@ -175,7 +175,85 @@ if (cur->right) {
Java:
+ 100. 相同的树:递归代码
+ ```java
+ class Solution {
+ public boolean compare(TreeNode tree1, TreeNode tree2) {
+
+ if(tree1==null && tree2==null)return true;
+ if(tree1==null || tree2==null)return false;
+ if(tree1.val!=tree2.val)return false;
+ // 此时就是:左右节点都不为空,且数值相同的情况
+ // 此时才做递归,做下一层的判断
+ boolean compareLeft = compare(tree1.left, tree2.left); // 左子树:左、 右子树:左
+ boolean compareRight = compare(tree1.right, tree2.right); // 左子树:右、 右子树:右
+ boolean isSame = compareLeft && compareRight; // 左子树:中、 右子树:中(逻辑处理)
+ return isSame;
+ }
+ boolean isSameTree(TreeNode p, TreeNode q) {
+ return compare(p, q);
+ }
+}
+ ```
+ 257. 二叉树的所有路径: 回溯代码
+ ```java
+ class Solution {
+ public void traversal(TreeNode cur, List path, List result) {
+ path.add(cur.val);
+ // 这才到了叶子节点
+ if (cur.left == null && cur.right == null) {
+ String sPath="";
+ for (int i = 0; i < path.size() - 1; i++) {
+ sPath += ""+path.get(i);
+ sPath += "->";
+ }
+ sPath += path.get(path.size() - 1);
+ result.add(sPath);
+ return;
+ }
+ if (cur.left!=null) {
+ traversal(cur.left, path, result);
+ path.remove(path.size()-1); // 回溯
+ }
+ if (cur.right!=null) {
+ traversal(cur.right, path, result);
+ path.remove(path.size()-1); // 回溯
+ }
+ }
+
+ public List binaryTreePaths(TreeNode root) {
+ List result = new LinkedList<>();
+ List path = new LinkedList<>();
+ if (root == null) return result;
+ traversal(root, path, result);
+ return result;
+ }
+}
+
+ ```
+ 如下为精简之后的递归代码:(257. 二叉树的所有路径)
+ ```java
+ class Solution {
+ public void traversal(TreeNode cur, String path, List result) {
+ path += cur.val; // 中
+ if (cur.left == null && cur.right == null) {
+ result.add(path);
+ return;
+ }
+ if (cur.left!=null) traversal(cur.left, path + "->", result); // 左 回溯就隐藏在这里
+ if (cur.right!=null) traversal(cur.right, path + "->", result); // 右 回溯就隐藏在这里
+ }
+
+ public List binaryTreePaths(TreeNode root) {
+ List result = new LinkedList<>();
+ String path = "";
+ if (root == null) return result;
+ traversal(root, path, result);
+ return result;
+ }
+}
+ ```
Python:
From 9b52a5070c4a2e6c38023302b2c541dc59a94098 Mon Sep 17 00:00:00 2001
From: nmydt <62681228+nmydt@users.noreply.github.com>
Date: Wed, 19 May 2021 21:13:30 +0800
Subject: [PATCH 31/31] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=201035.=E4=B8=8D?=
=?UTF-8?q?=E7=9B=B8=E4=BA=A4=E7=9A=84=E7=BA=BF=20Java=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/1035.不相交的线.md | 19 ++++++++++++++++++-
1 file changed, 18 insertions(+), 1 deletion(-)
diff --git a/problems/1035.不相交的线.md b/problems/1035.不相交的线.md
index 6f2b6646..660b1e27 100644
--- a/problems/1035.不相交的线.md
+++ b/problems/1035.不相交的线.md
@@ -73,7 +73,24 @@ public:
Java:
-
+ ```java
+ class Solution {
+ public int maxUncrossedLines(int[] A, int[] B) {
+ int [][] dp = new int[A.length+1][B.length+1];
+ for(int i=1;i<=A.length;i++) {
+ for(int j=1;j<=B.length;j++) {
+ if (A[i-1]==B[j-1]) {
+ dp[i][j]=dp[i-1][j-1]+1;
+ }
+ else {
+ dp[i][j]=Math.max(dp[i-1][j], dp[i][j-1]);
+ }
+ }
+ }
+ return dp[A.length][B.length];
+ }
+}
+ ```
Python: