From 8c394c9f8588a1e94250dc93bede24b7380996f8 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Tue, 17 May 2022 17:08:11 +0800
Subject: [PATCH 01/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200150.=E9=80=86?=
=?UTF-8?q?=E6=B3=A2=E5=85=B0=E8=A1=A8=E8=BE=BE=E5=BC=8F=E6=B1=82=E5=80=BC?=
=?UTF-8?q?.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0150.逆波兰表达式求值.md | 29 ++++++++++++++++++++++-
1 file changed, 28 insertions(+), 1 deletion(-)
diff --git a/problems/0150.逆波兰表达式求值.md b/problems/0150.逆波兰表达式求值.md
index fd3d69aa..47da06f6 100644
--- a/problems/0150.逆波兰表达式求值.md
+++ b/problems/0150.逆波兰表达式求值.md
@@ -325,6 +325,33 @@ func evalRPN(_ tokens: [String]) -> Int {
return stack.last!
}
```
-
+Scala:
+```scala
+object Solution {
+ import scala.collection.mutable
+ def evalRPN(tokens: Array[String]): Int = {
+ val stack = mutable.Stack[Int]() // 定义栈
+ // 抽取运算操作,需要传递x,y,和一个函数
+ def operator(x: Int, y: Int, f: (Int, Int) => Int): Int = f(x, y)
+ for (token <- tokens) {
+ // 模式匹配,匹配不同的操作符做什么样的运算
+ token match {
+ // 最后一个参数 _+_,代表x+y,遵循Scala的函数至简原则,以下运算同理
+ case "+" => stack.push(operator(stack.pop(), stack.pop(), _ + _))
+ case "-" => stack.push(operator(stack.pop(), stack.pop(), -_ + _))
+ case "*" => stack.push(operator(stack.pop(), stack.pop(), _ * _))
+ case "/" => {
+ var pop1 = stack.pop()
+ var pop2 = stack.pop()
+ stack.push(operator(pop2, pop1, _ / _))
+ }
+ case _ => stack.push(token.toInt) // 不是运算符就入栈
+ }
+ }
+ // 最后返回栈顶,不需要加return关键字
+ stack.pop()
+ }
+}
+```
-----------------------
From 748a728420be62c835866e7027171462ef90f888 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Tue, 17 May 2022 18:41:52 +0800
Subject: [PATCH 02/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200239.=E6=BB=91?=
=?UTF-8?q?=E5=8A=A8=E7=AA=97=E5=8F=A3=E6=9C=80=E5=A4=A7=E5=80=BC.md=20Sca?=
=?UTF-8?q?la=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0239.滑动窗口最大值.md | 47 ++++++++++++++++++++++++++
1 file changed, 47 insertions(+)
diff --git a/problems/0239.滑动窗口最大值.md b/problems/0239.滑动窗口最大值.md
index f269450f..eb32fdd2 100644
--- a/problems/0239.滑动窗口最大值.md
+++ b/problems/0239.滑动窗口最大值.md
@@ -630,6 +630,53 @@ func maxSlidingWindow(_ nums: [Int], _ k: Int) -> [Int] {
return result
}
```
+Scala:
+```scala
+import scala.collection.mutable.ArrayBuffer
+object Solution {
+ def maxSlidingWindow(nums: Array[Int], k: Int): Array[Int] = {
+ var len = nums.length - k + 1 // 滑动窗口长度
+ var res: Array[Int] = new Array[Int](len) // 声明存储结果的数组
+ var index = 0 // 结果数组指针
+ val queue: MyQueue = new MyQueue // 自定义队列
+ // 将前k个添加到queue
+ for (i <- 0 until k) {
+ queue.add(nums(i))
+ }
+ res(index) = queue.peek // 第一个滑动窗口的最大值
+ index += 1
+ for (i <- k until nums.length) {
+ queue.poll(nums(i - k)) // 首先移除第i-k个元素
+ queue.add(nums(i)) // 添加当前数字到队列
+ res(index) = queue.peek() // 赋值
+ index+=1
+ }
+ // 最终返回res,return关键字可以省略
+ res
+ }
+}
+
+class MyQueue {
+ var queue = ArrayBuffer[Int]()
+
+ // 移除元素,如果传递进来的跟队头相等,那么移除
+ def poll(value: Int): Unit = {
+ if (!queue.isEmpty && queue.head == value) {
+ queue.remove(0)
+ }
+ }
+
+ // 添加元素,当队尾大于当前元素就删除
+ def add(value: Int): Unit = {
+ while (!queue.isEmpty && value > queue.last) {
+ queue.remove(queue.length - 1)
+ }
+ queue.append(value)
+ }
+
+ def peek(): Int = queue.head
+}
+```
-----------------------
From 47c4cc102123b1b43f09b7b766fdd2f1e1b979a9 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Tue, 17 May 2022 19:52:32 +0800
Subject: [PATCH 03/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200347.=E5=89=8DK?=
=?UTF-8?q?=E4=B8=AA=E9=AB=98=E9=A2=91=E5=85=83=E7=B4=A0.md=20Scala?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0347.前K个高频元素.md | 42 ++++++++++++++++++++++++++++
1 file changed, 42 insertions(+)
diff --git a/problems/0347.前K个高频元素.md b/problems/0347.前K个高频元素.md
index 1d6a358b..20932b28 100644
--- a/problems/0347.前K个高频元素.md
+++ b/problems/0347.前K个高频元素.md
@@ -374,7 +374,49 @@ function topKFrequent(nums: number[], k: number): number[] {
};
```
+Scala:
+解法一: 优先级队列
+```scala
+object Solution {
+ import scala.collection.mutable
+ def topKFrequent(nums: Array[Int], k: Int): Array[Int] = {
+ val map = mutable.HashMap[Int, Int]()
+ // 将所有元素都放入Map
+ for (num <- nums) {
+ map.put(num, map.getOrElse(num, 0) + 1)
+ }
+ // 声明一个优先级队列,在函数柯里化那块需要指明排序方式
+ var queue = mutable.PriorityQueue[(Int, Int)]()(Ordering.fromLessThan((x, y) => x._2 > y._2))
+ // 将map里面的元素送入优先级队列
+ for (elem <- map) {
+ queue.enqueue(elem)
+ if(queue.size > k){
+ queue.dequeue // 如果队列元素大于k个,出队
+ }
+ }
+ // 最终只需要key的Array形式就可以了,return关键字可以省略
+ queue.map(_._1).toArray
+ }
+}
+```
+解法二: 相当于一个wordCount程序
+```scala
+object Solution {
+ def topKFrequent(nums: Array[Int], k: Int): Array[Int] = {
+ // 首先将数据变为(x,1),然后按照x分组,再使用map进行转换(x,sum),变换为Array
+ // 再使用sort针对sum进行排序,最后take前k个,再把数据变为x,y,z这种格式
+ nums.map((_, 1)).groupBy(_._1)
+ .map {
+ case (x, arr) => (x, arr.map(_._2).sum)
+ }
+ .toArray
+ .sortWith(_._2 > _._2)
+ .take(k)
+ .map(_._1)
+ }
+}
+```
-----------------------
From 6457d2d99775e9eddc1ab8386cdd5ca2d916b4b7 Mon Sep 17 00:00:00 2001
From: whusky <31883473+GitHubQAQ@users.noreply.github.com>
Date: Tue, 17 May 2022 21:22:17 +0800
Subject: [PATCH 04/69] =?UTF-8?q?Update=200063.=E4=B8=8D=E5=90=8C=E8=B7=AF?=
=?UTF-8?q?=E5=BE=84II.md=20=20=E6=B7=BB=E5=8A=A0=E9=A2=84=E5=88=A4?=
=?UTF-8?q?=E6=96=AD=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
1. 优化代码高亮格式
2. 对于C++的第一种解法添加预判断代码
---
problems/0063.不同路径II.md | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/problems/0063.不同路径II.md b/problems/0063.不同路径II.md
index a40cceda..0e51e0fe 100644
--- a/problems/0063.不同路径II.md
+++ b/problems/0063.不同路径II.md
@@ -66,7 +66,7 @@ dp[i][j] :表示从(0 ,0)出发,到(i, j) 有dp[i][j]条不同的路
所以代码为:
-```
+```cpp
if (obstacleGrid[i][j] == 0) { // 当(i, j)没有障碍的时候,再推导dp[i][j]
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
@@ -76,7 +76,7 @@ if (obstacleGrid[i][j] == 0) { // 当(i, j)没有障碍的时候,再推导dp[i
在[62.不同路径](https://programmercarl.com/0062.不同路径.html)不同路径中我们给出如下的初始化:
-```
+```cpp
vector> dp(m, vector(n, 0)); // 初始值为0
for (int i = 0; i < m; i++) dp[i][0] = 1;
for (int j = 0; j < n; j++) dp[0][j] = 1;
@@ -138,6 +138,8 @@ public:
int uniquePathsWithObstacles(vector>& obstacleGrid) {
int m = obstacleGrid.size();
int n = obstacleGrid[0].size();
+ if (obstacleGrid[m - 1][n - 1] == 1 || obstacleGrid[0][0] == 1) //如果在起点或终点出现了障碍,直接返回0
+ return 0;
vector> dp(m, vector(n, 0));
for (int i = 0; i < m && obstacleGrid[i][0] == 0; i++) dp[i][0] = 1;
for (int j = 0; j < n && obstacleGrid[0][j] == 0; j++) dp[0][j] = 1;
From 552e24b171bae840f32ab865ae4518c8674492e3 Mon Sep 17 00:00:00 2001
From: molonlu
Date: Wed, 18 May 2022 11:49:55 +0800
Subject: [PATCH 05/69] =?UTF-8?q?Update=200226.=E7=BF=BB=E8=BD=AC=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
通过tmp显示交换的方式改为golang风格的交换
---
problems/0226.翻转二叉树.md | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/problems/0226.翻转二叉树.md b/problems/0226.翻转二叉树.md
index a3ebe24d..af5b8043 100644
--- a/problems/0226.翻转二叉树.md
+++ b/problems/0226.翻转二叉树.md
@@ -368,9 +368,7 @@ func invertTree(root *TreeNode) *TreeNode {
if root ==nil{
return nil
}
- temp:=root.Left
- root.Left=root.Right
- root.Right=temp
+ root.Left,root.Right=root.Right,root.Left//交换
invertTree(root.Left)
invertTree(root.Right)
From f389dcd987a7f9b381f146de9846fc4abfdb92ae Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Wed, 18 May 2022 16:23:50 +0800
Subject: [PATCH 06/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E4=BA=8C=E5=8F=89?=
=?UTF-8?q?=E6=A0=91=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80.md=20Scala?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/二叉树理论基础.md | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/problems/二叉树理论基础.md b/problems/二叉树理论基础.md
index 9c151e32..9e10ac20 100644
--- a/problems/二叉树理论基础.md
+++ b/problems/二叉树理论基础.md
@@ -258,6 +258,13 @@ class TreeNode {
}
}
```
-
+Scala:
+```scala
+class TreeNode(_value: Int = 0, _left: TreeNode = null, _right: TreeNode = null) {
+ var value: Int = _value
+ var left: TreeNode = _left
+ var right: TreeNode = _right
+}
+```
-----------------------
From b2be41e4bcd2c1ef6bc4951519e5134209b55689 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Wed, 18 May 2022 16:54:15 +0800
Subject: [PATCH 07/69] =?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=E9=80=92=E5=BD=92=E9=81=8D=E5=8E=86.md=20Sca?=
=?UTF-8?q?la=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/二叉树的递归遍历.md | 51 ++++++++++++++++++++++++++++
1 file changed, 51 insertions(+)
diff --git a/problems/二叉树的递归遍历.md b/problems/二叉树的递归遍历.md
index 186c39d3..29c0cfda 100644
--- a/problems/二叉树的递归遍历.md
+++ b/problems/二叉树的递归遍历.md
@@ -470,5 +470,56 @@ func postorder(_ root: TreeNode?, res: inout [Int]) {
res.append(root!.val)
}
```
+Scala: 前序遍历:(144.二叉树的前序遍历)
+```scala
+object Solution {
+ import scala.collection.mutable.ListBuffer
+ def preorderTraversal(root: TreeNode): List[Int] = {
+ val res = ListBuffer[Int]()
+ def traversal(curNode: TreeNode): Unit = {
+ if(curNode == null) return
+ res.append(curNode.value)
+ traversal(curNode.left)
+ traversal(curNode.right)
+ }
+ traversal(root)
+ res.toList
+ }
+}
+```
+中序遍历:(94. 二叉树的中序遍历)
+```scala
+object Solution {
+ import scala.collection.mutable.ListBuffer
+ def inorderTraversal(root: TreeNode): List[Int] = {
+ val res = ListBuffer[Int]()
+ def traversal(curNode: TreeNode): Unit = {
+ if(curNode == null) return
+ traversal(curNode.left)
+ res.append(curNode.value)
+ traversal(curNode.right)
+ }
+ traversal(root)
+ res.toList
+ }
+}
+```
+后序遍历:(145. 二叉树的后序遍历)
+```scala
+object Solution {
+ import scala.collection.mutable.ListBuffer
+ def postorderTraversal(root: TreeNode): List[Int] = {
+ val res = ListBuffer[Int]()
+ def traversal(curNode: TreeNode): Unit = {
+ if (curNode == null) return
+ traversal(curNode.left)
+ traversal(curNode.right)
+ res.append(curNode.value)
+ }
+ traversal(root)
+ res.toList
+ }
+}
+```
-----------------------
From d6d227c4affd046a47429454aa241bae12c7def2 Mon Sep 17 00:00:00 2001
From: areslk <543430610@qq.com>
Date: Wed, 18 May 2022 17:07:40 +0800
Subject: [PATCH 08/69] =?UTF-8?q?Update=200110.=E5=B9=B3=E8=A1=A1=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
修改注释
---
problems/0110.平衡二叉树.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/problems/0110.平衡二叉树.md b/problems/0110.平衡二叉树.md
index d98ff8a9..b7598365 100644
--- a/problems/0110.平衡二叉树.md
+++ b/problems/0110.平衡二叉树.md
@@ -208,7 +208,7 @@ int getHeight(TreeNode* node) {
```CPP
class Solution {
public:
- // 返回以该节点为根节点的二叉树的高度,如果不是二叉搜索树了则返回-1
+ // 返回以该节点为根节点的二叉树的高度,如果不是平衡二叉树了则返回-1
int getHeight(TreeNode* node) {
if (node == NULL) {
return 0;
From 4aad0f9ca58c0188248af5a1043a21722aae0129 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Wed, 18 May 2022 17:26:22 +0800
Subject: [PATCH 09/69] =?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.md=20Sca?=
=?UTF-8?q?la=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/二叉树的迭代遍历.md | 65 ++++++++++++++++++++++++++++
1 file changed, 65 insertions(+)
diff --git a/problems/二叉树的迭代遍历.md b/problems/二叉树的迭代遍历.md
index 13ba5f1e..fac30f99 100644
--- a/problems/二叉树的迭代遍历.md
+++ b/problems/二叉树的迭代遍历.md
@@ -568,6 +568,71 @@ func inorderTraversal(_ root: TreeNode?) -> [Int] {
return result
}
```
+Scala:
+```scala
+// 前序遍历(迭代法)
+object Solution {
+ import scala.collection.mutable
+ def preorderTraversal(root: TreeNode): List[Int] = {
+ val res = mutable.ListBuffer[Int]()
+ if (root == null) return res.toList
+ // 声明一个栈,泛型为TreeNode
+ val stack = mutable.Stack[TreeNode]()
+ stack.push(root) // 先把根节点压入栈
+ while (!stack.isEmpty) {
+ var curNode = stack.pop()
+ res.append(curNode.value) // 先把这个值压入栈
+ // 如果当前节点的左右节点不为空,则入栈,先放右节点,再放左节点
+ if (curNode.right != null) stack.push(curNode.right)
+ if (curNode.left != null) stack.push(curNode.left)
+ }
+ res.toList
+ }
+}
+// 中序遍历(迭代法)
+object Solution {
+ import scala.collection.mutable
+ def inorderTraversal(root: TreeNode): List[Int] = {
+ val res = mutable.ArrayBuffer[Int]()
+ if (root == null) return res.toList
+ val stack = mutable.Stack[TreeNode]()
+ var curNode = root
+ // 将左节点都入栈,当遍历到最左(到空)的时候,再弹出栈顶元素,加入res
+ // 再把栈顶元素的右节点加进来,继续下一轮遍历
+ while (curNode != null || !stack.isEmpty) {
+ if (curNode != null) {
+ stack.push(curNode)
+ curNode = curNode.left
+ } else {
+ curNode = stack.pop()
+ res.append(curNode.value)
+ curNode = curNode.right
+ }
+ }
+ res.toList
+ }
+}
+
+// 后序遍历(迭代法)
+object Solution {
+ import scala.collection.mutable
+ def postorderTraversal(root: TreeNode): List[Int] = {
+ val res = mutable.ListBuffer[Int]()
+ if (root == null) return res.toList
+ val stack = mutable.Stack[TreeNode]()
+ stack.push(root)
+ while (!stack.isEmpty) {
+ val curNode = stack.pop()
+ res.append(curNode.value)
+ // 这次左节点先入栈,右节点再入栈
+ if(curNode.left != null) stack.push(curNode.left)
+ if(curNode.right != null) stack.push(curNode.right)
+ }
+ // 最后需要翻转List
+ res.reverse.toList
+ }
+}
+```
-----------------------
From 54d10fc3b893d50c278349c9de9d09bab5924ae7 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Thu, 19 May 2022 00:21:29 +0800
Subject: [PATCH 10/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880300.=E6=9C=80?=
=?UTF-8?q?=E9=95=BF=E4=B8=8A=E5=8D=87=E5=AD=90=E5=BA=8F=E5=88=97.md?=
=?UTF-8?q?=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=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/0300.最长上升子序列.md | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)
diff --git a/problems/0300.最长上升子序列.md b/problems/0300.最长上升子序列.md
index f68edb5a..5ccb2d76 100644
--- a/problems/0300.最长上升子序列.md
+++ b/problems/0300.最长上升子序列.md
@@ -220,6 +220,27 @@ const lengthOfLIS = (nums) => {
};
```
+TypeScript
+
+```typescript
+function lengthOfLIS(nums: number[]): number {
+ /**
+ dp[i]: 前i个元素中,以nums[i]结尾,最长子序列的长度
+ */
+ const dp: number[] = new Array(nums.length).fill(1);
+ let resMax: number = 0;
+ for (let i = 0, length = nums.length; i < length; i++) {
+ for (let j = 0; j < i; j++) {
+ if (nums[i] > nums[j]) {
+ dp[i] = Math.max(dp[i], dp[j] + 1);
+ }
+ }
+ resMax = Math.max(resMax, dp[i]);
+ }
+ return resMax;
+};
+```
+
From 52a486101d0525074a7b1f6a54a98b5940f9ab50 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Thu, 19 May 2022 00:43:00 +0800
Subject: [PATCH 11/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880674.=E6=9C=80?=
=?UTF-8?q?=E9=95=BF=E8=BF=9E=E7=BB=AD=E9=80=92=E5=A2=9E=E5=BA=8F=E5=88=97?=
=?UTF-8?q?.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=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/0674.最长连续递增序列.md | 39 +++++++++++++++++++++++
1 file changed, 39 insertions(+)
diff --git a/problems/0674.最长连续递增序列.md b/problems/0674.最长连续递增序列.md
index 56e95d97..6ec4a6c7 100644
--- a/problems/0674.最长连续递增序列.md
+++ b/problems/0674.最长连续递增序列.md
@@ -319,6 +319,45 @@ const findLengthOfLCIS = (nums) => {
};
```
+TypeScript:
+
+> 动态规划:
+
+```typescript
+function findLengthOfLCIS(nums: number[]): number {
+ /**
+ dp[i]: 前i个元素,以nums[i]结尾,最长连续子序列的长度
+ */
+ const dp: number[] = new Array(nums.length).fill(1);
+ let resMax: number = 1;
+ for (let i = 1, length = nums.length; i < length; i++) {
+ if (nums[i] > nums[i - 1]) {
+ dp[i] = dp[i - 1] + 1;
+ }
+ resMax = Math.max(resMax, dp[i]);
+ }
+ return resMax;
+};
+```
+
+> 贪心:
+
+```typescript
+function findLengthOfLCIS(nums: number[]): number {
+ let resMax: number = 1;
+ let count: number = 1;
+ for (let i = 0, length = nums.length; i < length - 1; i++) {
+ if (nums[i] < nums[i + 1]) {
+ count++;
+ } else {
+ count = 1;
+ }
+ resMax = Math.max(resMax, count);
+ }
+ return resMax;
+};
+```
+
From 648014b28a54e953d335e822d7e7a5f98a82e3ea Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 19 May 2022 10:19:22 +0800
Subject: [PATCH 12/69] =?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=E7=BB=9F=E4=B8=80=E8=BF=AD=E4=BB=A3=E6=B3=95?=
=?UTF-8?q?.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/二叉树的统一迭代法.md | 74 +++++++++++++++++++++++++
1 file changed, 74 insertions(+)
diff --git a/problems/二叉树的统一迭代法.md b/problems/二叉树的统一迭代法.md
index f6edf586..9ca6ac39 100644
--- a/problems/二叉树的统一迭代法.md
+++ b/problems/二叉树的统一迭代法.md
@@ -591,6 +591,80 @@ function postorderTraversal(root: TreeNode | null): number[] {
return res;
};
```
+Scala:
+```scala
+// 前序遍历
+object Solution {
+ import scala.collection.mutable
+ def preorderTraversal(root: TreeNode): List[Int] = {
+ val res = mutable.ListBuffer[Int]()
+ val stack = mutable.Stack[TreeNode]()
+ if (root != null) stack.push(root)
+ while (!stack.isEmpty) {
+ var curNode = stack.top
+ if (curNode != null) {
+ stack.pop()
+ if (curNode.right != null) stack.push(curNode.right)
+ if (curNode.left != null) stack.push(curNode.left)
+ stack.push(curNode)
+ stack.push(null)
+ } else {
+ stack.pop()
+ res.append(stack.pop().value)
+ }
+ }
+ res.toList
+ }
+}
+// 中序遍历
+object Solution {
+ import scala.collection.mutable
+ def inorderTraversal(root: TreeNode): List[Int] = {
+ val res = mutable.ListBuffer[Int]()
+ val stack = mutable.Stack[TreeNode]()
+ if (root != null) stack.push(root)
+ while (!stack.isEmpty) {
+ var curNode = stack.top
+ if (curNode != null) {
+ stack.pop()
+ if (curNode.right != null) stack.push(curNode.right)
+ stack.push(curNode)
+ stack.push(null)
+ if (curNode.left != null) stack.push(curNode.left)
+ } else {
+ // 等于空的时候好办,弹出这个元素
+ stack.pop()
+ res.append(stack.pop().value)
+ }
+ }
+ res.toList
+ }
+}
+
+// 后序遍历
+object Solution {
+ import scala.collection.mutable
+ def postorderTraversal(root: TreeNode): List[Int] = {
+ val res = mutable.ListBuffer[Int]()
+ val stack = mutable.Stack[TreeNode]()
+ if (root != null) stack.push(root)
+ while (!stack.isEmpty) {
+ var curNode = stack.top
+ if (curNode != null) {
+ stack.pop()
+ stack.push(curNode)
+ stack.push(null)
+ if (curNode.right != null) stack.push(curNode.right)
+ if (curNode.left != null) stack.push(curNode.left)
+ } else {
+ stack.pop()
+ res.append(stack.pop().value)
+ }
+ }
+ res.toList
+ }
+}
+```
-----------------------
From 33ed3d980eecec6824fb5b9edad83d7b916c4142 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 19 May 2022 10:35:55 +0800
Subject: [PATCH 13/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20102.=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86?=
=?UTF-8?q?=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0102.二叉树的层序遍历.md | 25 +++++++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md
index ab8f2e57..5afce73a 100644
--- a/problems/0102.二叉树的层序遍历.md
+++ b/problems/0102.二叉树的层序遍历.md
@@ -298,6 +298,31 @@ func levelOrder(_ root: TreeNode?) -> [[Int]] {
return result
}
```
+Scala:
+```scala
+// 102.二叉树的层序遍历
+object Solution {
+ import scala.collection.mutable
+ def levelOrder(root: TreeNode): List[List[Int]] = {
+ val res = mutable.ListBuffer[List[Int]]()
+ if (root == null) return res.toList
+ val queue = mutable.Queue[TreeNode]() // 声明一个队列
+ queue.enqueue(root) // 把根节点加入queue
+ while (!queue.isEmpty) {
+ val tmp = mutable.ListBuffer[Int]()
+ val len = queue.size // 求出len的长度
+ for (i <- 0 until len) { // 从0到当前队列长度的所有节点都加入到结果集
+ val curNode = queue.dequeue()
+ tmp.append(curNode.value)
+ if (curNode.left != null) queue.enqueue(curNode.left)
+ if (curNode.right != null) queue.enqueue(curNode.right)
+ }
+ res.append(tmp.toList)
+ }
+ res.toList
+ }
+}
+```
**此时我们就掌握了二叉树的层序遍历了,那么如下九道力扣上的题目,只需要修改模板的两三行代码(不能再多了),便可打倒!**
From 04339562c7ce6c46dbd081551e51f36dcaf5fe8d Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 19 May 2022 10:36:51 +0800
Subject: [PATCH 14/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20107.=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E5=B1=82=E6=AC=A1=E9=81=8D=E5=8E=86?=
=?UTF-8?q?II=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0102.二叉树的层序遍历.md | 27 +++++++++++++++++++++++
1 file changed, 27 insertions(+)
diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md
index 5afce73a..e707ce27 100644
--- a/problems/0102.二叉树的层序遍历.md
+++ b/problems/0102.二叉树的层序遍历.md
@@ -553,6 +553,33 @@ func levelOrderBottom(_ root: TreeNode?) -> [[Int]] {
}
```
+Scala:
+```scala
+// 107.二叉树的层次遍历II
+object Solution {
+ import scala.collection.mutable
+ def levelOrderBottom(root: TreeNode): List[List[Int]] = {
+ val res = mutable.ListBuffer[List[Int]]()
+ if (root == null) return res.toList
+ val queue = mutable.Queue[TreeNode]()
+ queue.enqueue(root)
+ while (!queue.isEmpty) {
+ val tmp = mutable.ListBuffer[Int]()
+ val len = queue.size
+ for (i <- 0 until len) {
+ val curNode = queue.dequeue()
+ tmp.append(curNode.value)
+ if (curNode.left != null) queue.enqueue(curNode.left)
+ if (curNode.right != null) queue.enqueue(curNode.right)
+ }
+ res.append(tmp.toList)
+ }
+ // 最后翻转一下
+ res.reverse.toList
+ }
+}
+```
+
# 199.二叉树的右视图
[力扣题目链接](https://leetcode-cn.com/problems/binary-tree-right-side-view/)
From ba51a4947adb197fcc4551e3157c509795fd0a82 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 19 May 2022 11:03:09 +0800
Subject: [PATCH 15/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20199.=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E5=8F=B3=E8=A7=86=E5=9B=BE=20Scala?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0102.二叉树的层序遍历.md | 25 +++++++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md
index ab8f2e57..b74f1a0b 100644
--- a/problems/0102.二叉树的层序遍历.md
+++ b/problems/0102.二叉树的层序遍历.md
@@ -750,6 +750,31 @@ func rightSideView(_ root: TreeNode?) -> [Int] {
}
```
+Scala:
+```scala
+// 199.二叉树的右视图
+object Solution {
+ import scala.collection.mutable
+ def rightSideView(root: TreeNode): List[Int] = {
+ val res = mutable.ListBuffer[Int]()
+ if (root == null) return res.toList
+ val queue = mutable.Queue[TreeNode]()
+ queue.enqueue(root)
+ while (!queue.isEmpty) {
+ val len = queue.size
+ var curNode: TreeNode = null
+ for (i <- 0 until len) {
+ curNode = queue.dequeue()
+ if (curNode.left != null) queue.enqueue(curNode.left)
+ if (curNode.right != null) queue.enqueue(curNode.right)
+ }
+ res.append(curNode.value) // 把最后一个节点的值加入解集
+ }
+ res.toList // 最后需要把res转换为List,return关键字可以省略
+ }
+}
+```
+
# 637.二叉树的层平均值
[力扣题目链接](https://leetcode-cn.com/problems/average-of-levels-in-binary-tree/)
From 9af80d3007aed88bd8d09538ab6710218c3e9dfd Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 19 May 2022 11:10:07 +0800
Subject: [PATCH 16/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20637.=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E5=B1=82=E5=B9=B3=E5=9D=87=E5=80=BC?=
=?UTF-8?q?=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0102.二叉树的层序遍历.md | 24 +++++++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md
index b74f1a0b..a99449fb 100644
--- a/problems/0102.二叉树的层序遍历.md
+++ b/problems/0102.二叉树的层序遍历.md
@@ -1006,6 +1006,30 @@ func averageOfLevels(_ root: TreeNode?) -> [Double] {
return result
}
```
+Scala:
+```scala
+// 637.二叉树的层平均值
+object Solution {
+ import scala.collection.mutable
+ def averageOfLevels(root: TreeNode): Array[Double] = {
+ val res = mutable.ArrayBuffer[Double]()
+ val queue = mutable.Queue[TreeNode]()
+ queue.enqueue(root)
+ while (!queue.isEmpty) {
+ var sum = 0.0
+ var len = queue.size
+ for (i <- 0 until len) {
+ var curNode = queue.dequeue()
+ sum += curNode.value // 累加该层的值
+ if (curNode.left != null) queue.enqueue(curNode.left)
+ if (curNode.right != null) queue.enqueue(curNode.right)
+ }
+ res.append(sum / len) // 平均值即为sum/len
+ }
+ res.toArray // 最后需要转换为Array,return关键字可以省略
+ }
+}
+```
# 429.N叉树的层序遍历
From c922fbc0e3ee16bbd0f3406227ce51a44cfe636d Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 19 May 2022 12:19:10 +0800
Subject: [PATCH 17/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20429.N=E5=8F=89?=
=?UTF-8?q?=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86=20Scala?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0102.二叉树的层序遍历.md | 28 +++++++++++++++++++++++
1 file changed, 28 insertions(+)
diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md
index a99449fb..6964e1f0 100644
--- a/problems/0102.二叉树的层序遍历.md
+++ b/problems/0102.二叉树的层序遍历.md
@@ -1274,6 +1274,34 @@ func levelOrder(_ root: Node?) -> [[Int]] {
}
```
+Scala:
+```scala
+// 429.N叉树的层序遍历
+object Solution {
+ import scala.collection.mutable
+ def levelOrder(root: Node): List[List[Int]] = {
+ val res = mutable.ListBuffer[List[Int]]()
+ if (root == null) return res.toList
+ val queue = mutable.Queue[Node]()
+ queue.enqueue(root) // 根节点入队
+ while (!queue.isEmpty) {
+ val tmp = mutable.ListBuffer[Int]() // 存储每层节点
+ val len = queue.size
+ for (i <- 0 until len) {
+ val curNode = queue.dequeue()
+ tmp.append(curNode.value) // 将该节点的值加入tmp
+ // 循环遍历该节点的子节点,加入队列
+ for (child <- curNode.children) {
+ queue.enqueue(child)
+ }
+ }
+ res.append(tmp.toList) // 将该层的节点放到结果集
+ }
+ res.toList
+ }
+}
+```
+
# 515.在每个树行中找最大值
[力扣题目链接](https://leetcode-cn.com/problems/find-largest-value-in-each-tree-row/)
From 80301657b305bcbd614d5294b1d483d31419755f Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 19 May 2022 12:29:12 +0800
Subject: [PATCH 18/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20515.=E5=9C=A8?=
=?UTF-8?q?=E6=AF=8F=E4=B8=AA=E6=A0=91=E8=A1=8C=E4=B8=AD=E6=89=BE=E6=9C=80?=
=?UTF-8?q?=E5=A4=A7=E5=80=BC=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0102.二叉树的层序遍历.md | 26 +++++++++++++++++++++++
1 file changed, 26 insertions(+)
diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md
index ab8f2e57..db27c73d 100644
--- a/problems/0102.二叉树的层序遍历.md
+++ b/problems/0102.二叉树的层序遍历.md
@@ -1433,6 +1433,32 @@ func largestValues(_ root: TreeNode?) -> [Int] {
}
```
+Scala:
+```scala
+// 515.在每个树行中找最大值
+object Solution {
+ import scala.collection.mutable
+ def largestValues(root: TreeNode): List[Int] = {
+ val res = mutable.ListBuffer[Int]()
+ if (root == null) return res.toList
+ val queue = mutable.Queue[TreeNode]()
+ queue.enqueue(root)
+ while (!queue.isEmpty) {
+ var max = Int.MinValue // 初始化max为系统最小值
+ val len = queue.size
+ for (i <- 0 until len) {
+ val curNode = queue.dequeue()
+ max = math.max(max, curNode.value) // 对比求解最大值
+ if (curNode.left != null) queue.enqueue(curNode.left)
+ if (curNode.right != null) queue.enqueue(curNode.right)
+ }
+ res.append(max) // 将最大值放入结果集
+ }
+ res.toList
+ }
+}
+```
+
# 116.填充每个节点的下一个右侧节点指针
[力扣题目链接](https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node/)
From a3179499d03b05594b8808a1d11a0ab22048cf32 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 19 May 2022 12:58:52 +0800
Subject: [PATCH 19/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20116.=E5=A1=AB?=
=?UTF-8?q?=E5=85=85=E6=AF=8F=E4=B8=AA=E8=8A=82=E7=82=B9=E7=9A=84=E4=B8=8B?=
=?UTF-8?q?=E4=B8=80=E4=B8=AA=E5=8F=B3=E4=BE=A7=E8=8A=82=E7=82=B9=E6=8C=87?=
=?UTF-8?q?=E9=92=88=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0102.二叉树的层序遍历.md | 29 +++++++++++++++++++++++
1 file changed, 29 insertions(+)
diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md
index db27c73d..18fdfcb3 100644
--- a/problems/0102.二叉树的层序遍历.md
+++ b/problems/0102.二叉树的层序遍历.md
@@ -1718,6 +1718,35 @@ func connect(_ root: Node?) -> Node? {
}
```
+Scala:
+```scala
+// 116.填充每个节点的下一个右侧节点指针
+object Solution {
+ import scala.collection.mutable
+
+ def connect(root: Node): Node = {
+ if (root == null) return root
+ val queue = mutable.Queue[Node]()
+ queue.enqueue(root)
+ while (!queue.isEmpty) {
+ val len = queue.size
+ val tmp = mutable.ListBuffer[Node]()
+ for (i <- 0 until len) {
+ val curNode = queue.dequeue()
+ tmp.append(curNode)
+ if (curNode.left != null) queue.enqueue(curNode.left)
+ if (curNode.right != null) queue.enqueue(curNode.right)
+ }
+ // 处理next指针
+ for (i <- 0 until tmp.size - 1) {
+ tmp(i).next = tmp(i + 1)
+ }
+ tmp(tmp.size-1).next = null
+ }
+ root
+ }
+}
+```
# 117.填充每个节点的下一个右侧节点指针II
[力扣题目链接](https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node-ii/)
From 7a62a75e399325cf439eebf507e8ce2d576e618d Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 19 May 2022 12:59:45 +0800
Subject: [PATCH 20/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20117.=E5=A1=AB?=
=?UTF-8?q?=E5=85=85=E6=AF=8F=E4=B8=AA=E8=8A=82=E7=82=B9=E7=9A=84=E4=B8=8B?=
=?UTF-8?q?=E4=B8=80=E4=B8=AA=E5=8F=B3=E4=BE=A7=E8=8A=82=E7=82=B9=E6=8C=87?=
=?UTF-8?q?=E9=92=88II=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0102.二叉树的层序遍历.md | 29 +++++++++++++++++++++++
1 file changed, 29 insertions(+)
diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md
index 18fdfcb3..a2130f7e 100644
--- a/problems/0102.二叉树的层序遍历.md
+++ b/problems/0102.二叉树的层序遍历.md
@@ -1998,6 +1998,35 @@ func connect(_ root: Node?) -> Node? {
}
```
+Scala:
+```scala
+// 117.填充每个节点的下一个右侧节点指针II
+object Solution {
+ import scala.collection.mutable
+
+ def connect(root: Node): Node = {
+ if (root == null) return root
+ val queue = mutable.Queue[Node]()
+ queue.enqueue(root)
+ while (!queue.isEmpty) {
+ val len = queue.size
+ val tmp = mutable.ListBuffer[Node]()
+ for (i <- 0 until len) {
+ val curNode = queue.dequeue()
+ tmp.append(curNode)
+ if (curNode.left != null) queue.enqueue(curNode.left)
+ if (curNode.right != null) queue.enqueue(curNode.right)
+ }
+ // 处理next指针
+ for (i <- 0 until tmp.size - 1) {
+ tmp(i).next = tmp(i + 1)
+ }
+ tmp(tmp.size-1).next = null
+ }
+ root
+ }
+}
+```
# 104.二叉树的最大深度
[力扣题目链接](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/)
From 32f0599243e7e9585a9b18a8b36fa0e50f120a24 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 19 May 2022 13:38:22 +0800
Subject: [PATCH 21/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20104.=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6?=
=?UTF-8?q?=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0102.二叉树的层序遍历.md | 24 +++++++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md
index ab8f2e57..95a6b5d6 100644
--- a/problems/0102.二叉树的层序遍历.md
+++ b/problems/0102.二叉树的层序遍历.md
@@ -2160,6 +2160,30 @@ func maxDepth(_ root: TreeNode?) -> Int {
}
```
+Scala:
+```scala
+// 104.二叉树的最大深度
+object Solution {
+ import scala.collection.mutable
+ def maxDepth(root: TreeNode): Int = {
+ if (root == null) return 0
+ val queue = mutable.Queue[TreeNode]()
+ queue.enqueue(root)
+ var depth = 0
+ while (!queue.isEmpty) {
+ val len = queue.length
+ depth += 1
+ for (i <- 0 until len) {
+ val curNode = queue.dequeue()
+ if (curNode.left != null) queue.enqueue(curNode.left)
+ if (curNode.right != null) queue.enqueue(curNode.right)
+ }
+ }
+ depth
+ }
+}
+```
+
# 111.二叉树的最小深度
[力扣题目链接](https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/)
From f5c5a58b137adad5490c04b57f82953101789999 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 19 May 2022 13:42:12 +0800
Subject: [PATCH 22/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20111.=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F=E6=B7=B1=E5=BA=A6?=
=?UTF-8?q?=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0102.二叉树的层序遍历.md | 24 +++++++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md
index 95a6b5d6..b411816a 100644
--- a/problems/0102.二叉树的层序遍历.md
+++ b/problems/0102.二叉树的层序遍历.md
@@ -2403,6 +2403,30 @@ func minDepth(_ root: TreeNode?) -> Int {
}
```
+Scala:
+```scala
+// 111.二叉树的最小深度
+object Solution {
+ import scala.collection.mutable
+ def minDepth(root: TreeNode): Int = {
+ if (root == null) return 0
+ var depth = 0
+ val queue = mutable.Queue[TreeNode]()
+ queue.enqueue(root)
+ while (!queue.isEmpty) {
+ depth += 1
+ val len = queue.size
+ for (i <- 0 until len) {
+ val curNode = queue.dequeue()
+ if (curNode.left != null) queue.enqueue(curNode.left)
+ if (curNode.right != null) queue.enqueue(curNode.right)
+ if (curNode.left == null && curNode.right == null) return depth
+ }
+ }
+ depth
+ }
+}
+```
# 总结
From afcf6cd5a1093c3bc4f525b211858da02e4c47c6 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Thu, 19 May 2022 15:03:09 +0800
Subject: [PATCH 23/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880718.=E6=9C=80?=
=?UTF-8?q?=E9=95=BF=E9=87=8D=E5=A4=8D=E5=AD=90=E6=95=B0=E7=BB=84.md?=
=?UTF-8?q?=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=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/0718.最长重复子数组.md | 50 ++++++++++++++++++++++++++
1 file changed, 50 insertions(+)
diff --git a/problems/0718.最长重复子数组.md b/problems/0718.最长重复子数组.md
index 87b1492a..0b7b5199 100644
--- a/problems/0718.最长重复子数组.md
+++ b/problems/0718.最长重复子数组.md
@@ -297,6 +297,56 @@ const findLength = (nums1, nums2) => {
}
```
+TypeScript:
+
+> 动态规划:
+
+```typescript
+function findLength(nums1: number[], nums2: number[]): number {
+ /**
+ dp[i][j]:nums[i-1]和nums[j-1]结尾,最长重复子数组的长度
+ */
+ const length1: number = nums1.length,
+ length2: number = nums2.length;
+ const dp: number[][] = new Array(length1 + 1).fill(0)
+ .map(_ => new Array(length2 + 1).fill(0));
+ let resMax: number = 0;
+ for (let i = 1; i <= length1; i++) {
+ for (let j = 1; j <= length2; j++) {
+ if (nums1[i - 1] === nums2[j - 1]) {
+ dp[i][j] = dp[i - 1][j - 1] + 1;
+ resMax = Math.max(resMax, dp[i][j]);
+ }
+ }
+ }
+ return resMax;
+};
+```
+
+> 滚动数组:
+
+```typescript
+function findLength(nums1: number[], nums2: number[]): number {
+ const length1: number = nums1.length,
+ length2: number = nums2.length;
+ const dp: number[] = new Array(length1 + 1).fill(0);
+ let resMax: number = 0;
+ for (let i = 1; i <= length1; i++) {
+ for (let j = length2; j >= 1; j--) {
+ if (nums1[i - 1] === nums2[j - 1]) {
+ dp[j] = dp[j - 1] + 1;
+ resMax = Math.max(resMax, dp[j]);
+ } else {
+ dp[j] = 0;
+ }
+ }
+ }
+ return resMax;
+};
+```
+
+
+
-----------------------
From a4b024705b276e5cf69a2a521af8b923a32daaca Mon Sep 17 00:00:00 2001
From: xuxiaomeng <961592690@qq.com>
Date: Thu, 19 May 2022 20:25:16 +0800
Subject: [PATCH 24/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A00704.=E4=BA=8C=E5=88=86?=
=?UTF-8?q?=E6=9F=A5=E6=89=BE=20Kotlin=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0704.二分查找.md | 32 ++++++++++++++++++++++++++++++++
1 file changed, 32 insertions(+)
diff --git a/problems/0704.二分查找.md b/problems/0704.二分查找.md
index 55625130..9173d1da 100644
--- a/problems/0704.二分查找.md
+++ b/problems/0704.二分查找.md
@@ -611,6 +611,38 @@ public class Solution{
}
```
+**Kotlin:**
+```Kotlin
+// (版本一)左闭右开区间
+class Solution {
+ fun search(nums: IntArray, target: Int): Int {
+ var left = 0
+ var right = nums.size // [left,right) 右侧为开区间,right 设置为 nums.size
+ while (left < right) {
+ val mid = (left + right) / 2
+ if (nums[mid] < target) left = mid + 1
+ else if (nums[mid] > target) right = mid // 代码的核心,循环中 right 是开区间,这里也应是开区间
+ else return mid
+ }
+ return -1 // 没有找到 target ,返回 -1
+ }
+}
+// (版本二)左闭右闭区间
+class Solution {
+ fun search(nums: IntArray, target: Int): Int {
+ var left = 0
+ var right = nums.size - 1 // [left,right] 右侧为闭区间,right 设置为 nums.size - 1
+ while (left <= right) {
+ val mid = (left + right) / 2
+ if (nums[mid] < target) left = mid + 1
+ else if (nums[mid] > target) right = mid - 1 // 代码的核心,循环中 right 是闭区间,这里也应是闭区间
+ else return mid
+ }
+ return -1 // 没有找到 target ,返回 -1
+ }
+}
+```
+
-----------------------
From 568a569f0572cd500c152c7d836bc3aea42a59ad Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Thu, 19 May 2022 20:39:58 +0800
Subject: [PATCH 25/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=881143.=E6=9C=80?=
=?UTF-8?q?=E9=95=BF=E5=85=AC=E5=85=B1=E5=AD=90=E5=BA=8F=E5=88=97.md?=
=?UTF-8?q?=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=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/1143.最长公共子序列.md | 26 ++++++++++++++++++++++++++
1 file changed, 26 insertions(+)
diff --git a/problems/1143.最长公共子序列.md b/problems/1143.最长公共子序列.md
index ecedf89b..d58330ec 100644
--- a/problems/1143.最长公共子序列.md
+++ b/problems/1143.最长公共子序列.md
@@ -236,6 +236,32 @@ const longestCommonSubsequence = (text1, text2) => {
};
```
+TypeScript:
+
+```typescript
+function longestCommonSubsequence(text1: string, text2: string): number {
+ /**
+ dp[i][j]: text1中前i-1个和text2中前j-1个,最长公共子序列的长度
+ */
+ const length1: number = text1.length,
+ length2: number = text2.length;
+ const dp: number[][] = new Array(length1 + 1).fill(0)
+ .map(_ => new Array(length2 + 1).fill(0));
+ for (let i = 1; i <= length1; i++) {
+ for (let j = 1; j <= length2; j++) {
+ if (text1[i - 1] === text2[j - 1]) {
+ dp[i][j] = dp[i - 1][j - 1] + 1;
+ } else {
+ dp[i][j] = Math.max(dp[i][j - 1], dp[i - 1][j]);
+ }
+ }
+ }
+ return dp[length1][length2];
+};
+```
+
+
+
-----------------------
From b3335f78a9a5c0910577835f8e61069594a23a0f Mon Sep 17 00:00:00 2001
From: zhouchaoyu1
Date: Thu, 19 May 2022 20:47:08 +0800
Subject: [PATCH 26/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20763.=E5=88=92?=
=?UTF-8?q?=E5=88=86=E5=AD=97=E6=AF=8D=E5=8C=BA=E9=97=B4=20=E8=A1=A5?=
=?UTF-8?q?=E5=85=85=E6=80=9D=E8=B7=AF=E7=9A=84Java=E4=BB=A3=E7=A0=81?=
=?UTF-8?q?=E5=AE=9E=E7=8E=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0763.划分字母区间.md | 65 +++++++++++++++++++++++++++++
1 file changed, 65 insertions(+)
diff --git a/problems/0763.划分字母区间.md b/problems/0763.划分字母区间.md
index 2210cffa..2f4d1b48 100644
--- a/problems/0763.划分字母区间.md
+++ b/problems/0763.划分字母区间.md
@@ -158,6 +158,71 @@ class Solution {
return list;
}
}
+
+class Solution{
+ /*解法二: 上述c++补充思路的Java代码实现*/
+
+ public int[][] findPartitions(String s) {
+ List temp = new ArrayList<>();
+ int[][] hash = new int[26][2];//26个字母2列 表示该字母对应的区间
+
+ for (int i = 0; i < s.length(); i++) {
+ //更新字符c对应的位置i
+ char c = s.charAt(i);
+ if (hash[c - 'a'][0] == 0) hash[c - 'a'][0] = i;
+
+ hash[c - 'a'][1] = i;
+
+ //第一个元素区别对待一下
+ hash[s.charAt(0) - 'a'][0] = 0;
+ }
+
+
+ List> h = new LinkedList<>();
+ //组装区间
+ for (int i = 0; i < 26; i++) {
+ //if (hash[i][0] != hash[i][1]) {
+ temp.clear();
+ temp.add(hash[i][0]);
+ temp.add(hash[i][1]);
+ //System.out.println(temp);
+ h.add(new ArrayList<>(temp));
+ // }
+ }
+ // System.out.println(h);
+ // System.out.println(h.size());
+ int[][] res = new int[h.size()][2];
+ for (int i = 0; i < h.size(); i++) {
+ List list = h.get(i);
+ res[i][0] = list.get(0);
+ res[i][1] = list.get(1);
+ }
+
+ return res;
+
+ }
+
+ public List partitionLabels(String s) {
+ int[][] partitions = findPartitions(s);
+ List res = new ArrayList<>();
+ Arrays.sort(partitions, (o1, o2) -> Integer.compare(o1[0], o2[0]));
+ int right = partitions[0][1];
+ int left = 0;
+ for (int i = 0; i < partitions.length; i++) {
+ if (partitions[i][0] > right) {
+ //左边界大于右边界即可纪委一次分割
+ res.add(right - left + 1);
+ left = partitions[i][0];
+ }
+ right = Math.max(right, partitions[i][1]);
+
+ }
+ //最右端
+ res.add(right - left + 1);
+ return res;
+
+ }
+}
```
### Python
From 30c1be11ff9ef77ff6cf0294f37b09ae50ea4619 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Thu, 19 May 2022 21:26:54 +0800
Subject: [PATCH 27/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=881035.=E4=B8=8D?=
=?UTF-8?q?=E7=9B=B8=E4=BA=A4=E7=9A=84=E7=BA=BF.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/1035.不相交的线.md | 24 ++++++++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git a/problems/1035.不相交的线.md b/problems/1035.不相交的线.md
index 279ed816..4463c5f7 100644
--- a/problems/1035.不相交的线.md
+++ b/problems/1035.不相交的线.md
@@ -183,6 +183,30 @@ const maxUncrossedLines = (nums1, nums2) => {
};
```
+TypeScript:
+
+```typescript
+function maxUncrossedLines(nums1: number[], nums2: number[]): number {
+ /**
+ dp[i][j]: nums1前i-1个,nums2前j-1个,最大连线数
+ */
+ const length1: number = nums1.length,
+ length2: number = nums2.length;
+ const dp: number[][] = new Array(length1 + 1).fill(0)
+ .map(_ => new Array(length2 + 1).fill(0));
+ for (let i = 1; i <= length1; i++) {
+ for (let j = 1; j <= length2; j++) {
+ if (nums1[i - 1] === nums2[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[length1][length2];
+};
+```
+
From 4d2b80ce7508553923c99a7786521c018c24ff8a Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Thu, 19 May 2022 21:59:33 +0800
Subject: [PATCH 28/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880053.=E6=9C=80?=
=?UTF-8?q?=E5=A4=A7=E5=AD=90=E5=BA=8F=E5=92=8C=E5=8A=A8=E6=80=81=E8=A7=84?=
=?UTF-8?q?=E5=88=92.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../0053.最大子序和(动态规划).md | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/problems/0053.最大子序和(动态规划).md b/problems/0053.最大子序和(动态规划).md
index 4c883cb6..99aa7acf 100644
--- a/problems/0053.最大子序和(动态规划).md
+++ b/problems/0053.最大子序和(动态规划).md
@@ -186,6 +186,24 @@ const maxSubArray = nums => {
};
```
+TypeScript:
+
+```typescript
+function maxSubArray(nums: number[]): number {
+ /**
+ dp[i]:以nums[i]结尾的最大和
+ */
+ const dp: number[] = []
+ dp[0] = nums[0];
+ let resMax: number = 0;
+ for (let i = 1; i < nums.length; i++) {
+ dp[i] = Math.max(dp[i - 1] + nums[i], nums[i]);
+ resMax = Math.max(resMax, dp[i]);
+ }
+ return resMax;
+};
+```
+
-----------------------
From 8759349af0001c6ee2f05ceece9cc0e331fbc08a Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Fri, 20 May 2022 00:35:32 +0800
Subject: [PATCH 29/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880392.=E5=88=A4?=
=?UTF-8?q?=E6=96=AD=E5=AD=90=E5=BA=8F=E5=88=97.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0392.判断子序列.md | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git a/problems/0392.判断子序列.md b/problems/0392.判断子序列.md
index 671576f7..3f7eb11d 100644
--- a/problems/0392.判断子序列.md
+++ b/problems/0392.判断子序列.md
@@ -201,7 +201,32 @@ const isSubsequence = (s, t) => {
};
```
+TypeScript:
+
+```typescript
+function isSubsequence(s: string, t: string): boolean {
+ /**
+ dp[i][j]: s的前i-1个,t的前j-1个,最长公共子序列的长度
+ */
+ const sLen: number = s.length,
+ tLen: number = t.length;
+ const dp: number[][] = new Array(sLen + 1).fill(0)
+ .map(_ => new Array(tLen + 1).fill(0));
+ for (let i = 1; i <= sLen; i++) {
+ for (let j = 1; j <= tLen; j++) {
+ if (s[i - 1] === t[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[sLen][tLen] === s.length;
+};
+```
+
Go:
+
```go
func isSubsequence(s string, t string) bool {
dp := make([][]int,len(s)+1)
From 4eefc546c62d0b5d63f08d1b51e5603ee722d7d5 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Fri, 20 May 2022 13:41:23 +0800
Subject: [PATCH 30/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880115.=E4=B8=8D?=
=?UTF-8?q?=E5=90=8C=E7=9A=84=E5=AD=90=E5=BA=8F=E5=88=97.md=EF=BC=89?=
=?UTF-8?q?=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0115.不同的子序列.md | 30 +++++++++++++++++++++++++++++
1 file changed, 30 insertions(+)
diff --git a/problems/0115.不同的子序列.md b/problems/0115.不同的子序列.md
index 0f762969..ca66e20d 100644
--- a/problems/0115.不同的子序列.md
+++ b/problems/0115.不同的子序列.md
@@ -267,6 +267,36 @@ const numDistinct = (s, t) => {
};
```
+TypeScript:
+
+```typescript
+function numDistinct(s: string, t: string): number {
+ /**
+ dp[i][j]: s前i个字符,t前j个字符,s子序列中t出现的个数
+ dp[0][0]=1, 表示s前0个字符为'',t前0个字符为''
+ */
+ const sLen: number = s.length,
+ tLen: number = t.length;
+ const dp: number[][] = new Array(sLen + 1).fill(0)
+ .map(_ => new Array(tLen + 1).fill(0));
+ for (let m = 0; m < sLen; m++) {
+ dp[m][0] = 1;
+ }
+ for (let i = 1; i <= sLen; i++) {
+ for (let j = 1; j <= tLen; j++) {
+ if (s[i - 1] === t[j - 1]) {
+ dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];
+ } else {
+ dp[i][j] = dp[i - 1][j];
+ }
+ }
+ }
+ return dp[sLen][tLen];
+};
+```
+
+
+
-----------------------
From 58c4ad4947506310ef7c05d9603972af684c9b96 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Fri, 20 May 2022 15:11:12 +0800
Subject: [PATCH 31/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880583.=E4=B8=A4?=
=?UTF-8?q?=E4=B8=AA=E5=AD=97=E7=AC=A6=E4=B8=B2=E7=9A=84=E5=88=A0=E9=99=A4?=
=?UTF-8?q?=E6=93=8D=E4=BD=9C.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typesc?=
=?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
---
.../0583.两个字符串的删除操作.md | 61 +++++++++++++++++++
1 file changed, 61 insertions(+)
diff --git a/problems/0583.两个字符串的删除操作.md b/problems/0583.两个字符串的删除操作.md
index 53c1a125..00f11700 100644
--- a/problems/0583.两个字符串的删除操作.md
+++ b/problems/0583.两个字符串的删除操作.md
@@ -229,6 +229,67 @@ const minDistance = (word1, word2) => {
};
```
+TypeScript:
+
+> dp版本一:
+
+```typescript
+function minDistance(word1: string, word2: string): number {
+ /**
+ dp[i][j]: word1前i个字符,word2前j个字符,所需最小步数
+ dp[0][0]=0: word1前0个字符为'', word2前0个字符为''
+ */
+ const length1: number = word1.length,
+ length2: number = word2.length;
+ const dp: number[][] = new Array(length1 + 1).fill(0)
+ .map(_ => new Array(length2 + 1).fill(0));
+ for (let i = 0; i <= length1; i++) {
+ dp[i][0] = i;
+ }
+ for (let i = 0; i <= length2; i++) {
+ dp[0][i] = i;
+ }
+ for (let i = 1; i <= length1; i++) {
+ for (let j = 1; j <= length2; j++) {
+ if (word1[i - 1] === word2[j - 1]) {
+ dp[i][j] = dp[i - 1][j - 1];
+ } else {
+ dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1]) + 1;
+ }
+ }
+ }
+ return dp[length1][length2];
+};
+```
+
+> dp版本二:
+
+```typescript
+function minDistance(word1: string, word2: string): number {
+ /**
+ dp[i][j]: word1前i个字符,word2前j个字符,最长公共子序列的长度
+ dp[0][0]=0: word1前0个字符为'', word2前0个字符为''
+ */
+ const length1: number = word1.length,
+ length2: number = word2.length;
+ const dp: number[][] = new Array(length1 + 1).fill(0)
+ .map(_ => new Array(length2 + 1).fill(0));
+ for (let i = 1; i <= length1; i++) {
+ for (let j = 1; j <= length2; j++) {
+ if (word1[i - 1] === word2[j - 1]) {
+ dp[i][j] = dp[i - 1][j - 1] + 1;
+ } else {
+ dp[i][j] = Math.max(dp[i][j - 1], dp[i - 1][j]);
+ }
+ }
+ }
+ const maxLen: number = dp[length1][length2];
+ return length1 + length2 - maxLen * 2;
+};
+```
+
+
+
-----------------------
From fa3a45c3dbd3cb58b9c0027cb2c8fe9e7727a439 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Fri, 20 May 2022 16:33:34 +0800
Subject: [PATCH 32/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880072.=E7=BC=96?=
=?UTF-8?q?=E8=BE=91=E8=B7=9D=E7=A6=BB.md=EF=BC=89=EF=BC=9A=E5=A2=9E?=
=?UTF-8?q?=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0072.编辑距离.md | 37 +++++++++++++++++++++++++++++++++++
1 file changed, 37 insertions(+)
diff --git a/problems/0072.编辑距离.md b/problems/0072.编辑距离.md
index 3802c228..530774ee 100644
--- a/problems/0072.编辑距离.md
+++ b/problems/0072.编辑距离.md
@@ -327,5 +327,42 @@ const minDistance = (word1, word2) => {
};
```
+TypeScript:
+
+```typescript
+function minDistance(word1: string, word2: string): number {
+ /**
+ dp[i][j]: word1前i个字符,word2前j个字符,最少操作数
+ dp[0][0]=0:表示word1前0个字符为'', word2前0个字符为''
+ */
+ const length1: number = word1.length,
+ length2: number = word2.length;
+ const dp: number[][] = new Array(length1 + 1).fill(0)
+ .map(_ => new Array(length2 + 1).fill(0));
+ for (let i = 0; i <= length1; i++) {
+ dp[i][0] = i;
+ }
+ for (let i = 0; i <= length2; i++) {
+ dp[0][i] = i;
+ }
+ for (let i = 1; i <= length1; i++) {
+ for (let j = 1; j <= length2; j++) {
+ if (word1[i - 1] === word2[j - 1]) {
+ dp[i][j] = dp[i - 1][j - 1];
+ } else {
+ dp[i][j] = Math.min(
+ dp[i - 1][j],
+ dp[i][j - 1],
+ dp[i - 1][j - 1]
+ ) + 1;
+ }
+ }
+ }
+ return dp[length1][length2];
+};
+```
+
+
+
-----------------------
From be7d6e1325009d5f404c8abeaae0cbed49ed2303 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Fri, 20 May 2022 19:36:10 +0800
Subject: [PATCH 33/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200226.=E7=BF=BB?=
=?UTF-8?q?=E8=BD=AC=E4=BA=8C=E5=8F=89=E6=A0=91.md=20Scala=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/0226.翻转二叉树.md | 48 ++++++++++++++++++++++++++++++++
1 file changed, 48 insertions(+)
diff --git a/problems/0226.翻转二叉树.md b/problems/0226.翻转二叉树.md
index a3ebe24d..e378cbc1 100644
--- a/problems/0226.翻转二叉树.md
+++ b/problems/0226.翻转二叉树.md
@@ -820,5 +820,53 @@ func invertTree(_ root: TreeNode?) -> TreeNode? {
}
```
+### Scala
+
+深度优先遍历(前序遍历):
+```scala
+object Solution {
+ def invertTree(root: TreeNode): TreeNode = {
+ if (root == null) return root
+ // 递归
+ def process(node: TreeNode): Unit = {
+ if (node == null) return
+ // 翻转节点
+ val curNode = node.left
+ node.left = node.right
+ node.right = curNode
+ process(node.left)
+ process(node.right)
+ }
+ process(root)
+ root
+ }
+}
+```
+
+广度优先遍历(层序遍历):
+```scala
+object Solution {
+ import scala.collection.mutable
+ def invertTree(root: TreeNode): TreeNode = {
+ if (root == null) return root
+ val queue = mutable.Queue[TreeNode]()
+ queue.enqueue(root)
+ while (!queue.isEmpty) {
+ val len = queue.size
+ for (i <- 0 until len) {
+ var curNode = queue.dequeue()
+ if (curNode.left != null) queue.enqueue(curNode.left)
+ if (curNode.right != null) queue.enqueue(curNode.right)
+ // 翻转
+ var tmpNode = curNode.left
+ curNode.left = curNode.right
+ curNode.right = tmpNode
+ }
+ }
+ root
+ }
+}
+```
+
-----------------------
From 81d4685e36603ec59e3e02d8243f60e660081541 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Fri, 20 May 2022 21:12:54 +0800
Subject: [PATCH 34/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200101.=E5=AF=B9?=
=?UTF-8?q?=E7=A7=B0=E4=BA=8C=E5=8F=89=E6=A0=91.md=20Scala=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/0101.对称二叉树.md | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)
diff --git a/problems/0101.对称二叉树.md b/problems/0101.对称二叉树.md
index e4e232c8..97ca0685 100644
--- a/problems/0101.对称二叉树.md
+++ b/problems/0101.对称二叉树.md
@@ -725,5 +725,25 @@ func isSymmetric3(_ root: TreeNode?) -> Bool {
}
```
+## Scala
+
+递归:
+```scala
+object Solution {
+ def isSymmetric(root: TreeNode): Boolean = {
+ if (root == null) return true // 如果等于空直接返回true
+ def compare(left: TreeNode, right: TreeNode): Boolean = {
+ if (left == null && right == null) return true // 如果左右都为空,则为true
+ if (left == null && right != null) return false // 如果左空右不空,不对称,返回false
+ if (left != null && right == null) return false // 如果左不空右空,不对称,返回false
+ // 如果左右的值相等,并且往下递归
+ left.value == right.value && compare(left.left, right.right) && compare(left.right, right.left)
+ }
+ // 分别比较左子树和右子树
+ compare(root.left, root.right)
+ }
+}
+```
+
-----------------------
From e7529aa31bf39bb1a3966ad8bd5fcbaa730b6447 Mon Sep 17 00:00:00 2001
From: Luo <82520819+Jerry-306@users.noreply.github.com>
Date: Sat, 21 May 2022 09:14:39 +0800
Subject: [PATCH 35/69] =?UTF-8?q?=E9=87=8D=E5=86=99=200216.=E7=BB=84?=
=?UTF-8?q?=E5=90=88=E6=80=BB=E5=92=8CIII=20JavaScript=20=E7=89=88?=
=?UTF-8?q?=E6=9C=AC=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
原代码冗余,建议重写
---
problems/0216.组合总和III.md | 45 +++++++++++++-------------------
1 file changed, 18 insertions(+), 27 deletions(-)
diff --git a/problems/0216.组合总和III.md b/problems/0216.组合总和III.md
index 32b1347e..66ca7ff7 100644
--- a/problems/0216.组合总和III.md
+++ b/problems/0216.组合总和III.md
@@ -360,39 +360,30 @@ func backTree(n,k,startIndex int,track *[]int,result *[][]int){
## javaScript
```js
-// 等差数列
-var maxV = k => k * (9 + 10 - k) / 2;
-var minV = k => k * (1 + k) / 2;
+/**
+ * @param {number} k
+ * @param {number} n
+ * @return {number[][]}
+ */
var combinationSum3 = function(k, n) {
- if (k > 9 || k < 1) return [];
- // if (n > maxV(k) || n < minV(k)) return [];
- // if (n === maxV(k)) return [Array.from({length: k}).map((v, i) => 9 - i)];
- // if (n === minV(k)) return [Array.from({length: k}).map((v, i) => i + 1)];
-
- const res = [], path = [];
- backtracking(k, n, 1, 0);
- return res;
- function backtracking(k, n, i, sum){
- const len = path.length;
- if (len > k || sum > n) return;
- if (maxV(k - len) < n - sum) return;
- if (minV(k - len) > n - sum) return;
-
- if(len === k && sum == n) {
- res.push(Array.from(path));
+ const backtrack = (start) => {
+ const l = path.length;
+ if (l === k) {
+ const sum = path.reduce((a, b) => a + b);
+ if (sum === n) {
+ res.push([...path]);
+ }
return;
}
-
- const min = Math.min(n - sum, 9 + len - k + 1);
-
- for(let a = i; a <= min; a++) {
- path.push(a);
- sum += a;
- backtracking(k, n, a + 1, sum);
+ for (let i = start; i <= 9 - (k - l) + 1; i++) {
+ path.push(i);
+ backtrack(i + 1);
path.pop();
- sum -= a;
}
}
+ let res = [], path = [];
+ backtrack(1);
+ return res;
};
```
From b4873836bacc8a65b84e8f9a7e8da7fd5d7dcd58 Mon Sep 17 00:00:00 2001
From: Luo <82520819+Jerry-306@users.noreply.github.com>
Date: Sat, 21 May 2022 09:45:03 +0800
Subject: [PATCH 36/69] =?UTF-8?q?=E7=BA=A0=E6=AD=A3=200131=20=E5=88=86?=
=?UTF-8?q?=E5=89=B2=E5=9B=9E=E6=96=87=E4=B8=B2=20JavaScript=20=E7=89=88?=
=?UTF-8?q?=E6=9C=AC=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
String 实例方法 substr 已弃用,请换成 slice
---
problems/0131.分割回文串.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/problems/0131.分割回文串.md b/problems/0131.分割回文串.md
index 7a702898..6a370fb4 100644
--- a/problems/0131.分割回文串.md
+++ b/problems/0131.分割回文串.md
@@ -442,7 +442,7 @@ var partition = function(s) {
}
for(let j = i; j < len; j++) {
if(!isPalindrome(s, i, j)) continue;
- path.push(s.substr(i, j - i + 1));
+ path.push(s.slice(i, j + 1));
backtracking(j + 1);
path.pop();
}
From ab6693d4b44e11dcf51771f5004e11e33c68c2cf Mon Sep 17 00:00:00 2001
From: Luo <82520819+Jerry-306@users.noreply.github.com>
Date: Sat, 21 May 2022 09:57:03 +0800
Subject: [PATCH 37/69] =?UTF-8?q?=E7=BA=A0=E6=AD=A3=200093.=E5=A4=8D?=
=?UTF-8?q?=E5=88=B6IP=E5=9C=B0=E5=9D=80=20JavaScript=20=E7=89=88=E6=9C=AC?=
=?UTF-8?q?=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
String 实例 substr 方法已弃用,请使用 slice 方法
---
problems/0093.复原IP地址.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/problems/0093.复原IP地址.md b/problems/0093.复原IP地址.md
index 6401824b..d5eaa8ab 100644
--- a/problems/0093.复原IP地址.md
+++ b/problems/0093.复原IP地址.md
@@ -444,7 +444,7 @@ var restoreIpAddresses = function(s) {
return;
}
for(let j = i; j < s.length; j++) {
- const str = s.substr(i, j - i + 1);
+ const str = s.slice(i, j + 1);
if(str.length > 3 || +str > 255) break;
if(str.length > 1 && str[0] === "0") break;
path.push(str);
From fd194bd52fc58a22cf38627c3a6bc7d4e4335a11 Mon Sep 17 00:00:00 2001
From: Luo <82520819+Jerry-306@users.noreply.github.com>
Date: Sat, 21 May 2022 10:06:05 +0800
Subject: [PATCH 38/69] =?UTF-8?q?=E7=BA=A0=E6=AD=A3=200078.=E5=AD=90?=
=?UTF-8?q?=E9=9B=86=E9=97=AE=E9=A2=98=20JS=20=E3=80=81TS=20=E7=89=88?=
=?UTF-8?q?=E6=9C=AC=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
JS 和 TS 里面 数组深拷贝一般采用 ES6 扩展运算符 ... ,或者 Array.from() 方法,而不会采用实例方法 slice. slice方法用于数组分割等操作,请注意代码书写规范!
---
problems/0078.子集.md | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/problems/0078.子集.md b/problems/0078.子集.md
index e1c52b5b..2b1c7643 100644
--- a/problems/0078.子集.md
+++ b/problems/0078.子集.md
@@ -260,7 +260,7 @@ var subsets = function(nums) {
let result = []
let path = []
function backtracking(startIndex) {
- result.push(path.slice())
+ result.push([...path])
for(let i = startIndex; i < nums.length; i++) {
path.push(nums[i])
backtracking(i + 1)
@@ -280,7 +280,7 @@ function subsets(nums: number[]): number[][] {
backTracking(nums, 0, []);
return resArr;
function backTracking(nums: number[], startIndex: number, route: number[]): void {
- resArr.push(route.slice());
+ resArr.push([...route]);
let length = nums.length;
if (startIndex === length) return;
for (let i = startIndex; i < length; i++) {
From 13ed28dbcf8eb40e94fafab3da7955e967f298bd Mon Sep 17 00:00:00 2001
From: Luo <82520819+Jerry-306@users.noreply.github.com>
Date: Sat, 21 May 2022 10:10:36 +0800
Subject: [PATCH 39/69] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=200090.=E5=AD=90?=
=?UTF-8?q?=E9=9B=86II=20JS=E3=80=81TS=E7=89=88=E6=9C=AC=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
JS 和 TS 里面 数组深拷贝一般采用 ES6 扩展运算符 ... ,或者 Array.from() 方法,而不会采用实例方法 slice. slice方法用于数组分割等操作,请注意代码书写规范!
---
problems/0090.子集II.md | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/problems/0090.子集II.md b/problems/0090.子集II.md
index 74ce000b..dd64d199 100644
--- a/problems/0090.子集II.md
+++ b/problems/0090.子集II.md
@@ -299,7 +299,7 @@ var subsetsWithDup = function(nums) {
return a - b
})
function backtracing(startIndex, sortNums) {
- result.push(path.slice(0))
+ result.push([...path])
if(startIndex > nums.length - 1) {
return
}
@@ -327,7 +327,7 @@ function subsetsWithDup(nums: number[]): number[][] {
backTraking(nums, 0, []);
return resArr;
function backTraking(nums: number[], startIndex: number, route: number[]): void {
- resArr.push(route.slice());
+ resArr.push([...route]);
let length: number = nums.length;
if (startIndex === length) return;
for (let i = startIndex; i < length; i++) {
From 2da33bfc93326e3cdf5a358e5ef18646f97982fb Mon Sep 17 00:00:00 2001
From: Luo <82520819+Jerry-306@users.noreply.github.com>
Date: Sat, 21 May 2022 10:46:39 +0800
Subject: [PATCH 40/69] =?UTF-8?q?=E7=BA=A0=E6=AD=A3=200046.=E5=85=A8?=
=?UTF-8?q?=E6=8E=92=E5=88=97=20TS=20=E7=89=88=E6=9C=AC=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
JS 和 TS 里面 数组深拷贝一般采用 ES6 扩展运算符 ... ,或者 Array.from() 方法,而不会采用实例方法 slice. slice方法用于数组分割等操作,请注意代码书写规范!
---
problems/0046.全排列.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/problems/0046.全排列.md b/problems/0046.全排列.md
index 836c3646..6c55e8ef 100644
--- a/problems/0046.全排列.md
+++ b/problems/0046.全排列.md
@@ -341,7 +341,7 @@ function permute(nums: number[]): number[][] {
return resArr;
function backTracking(nums: number[], route: number[]): void {
if (route.length === nums.length) {
- resArr.push(route.slice());
+ resArr.push([...route]);
return;
}
let tempVal: number;
From 38b10bcbfdb3f90f6af93a5b196ca0f68463d744 Mon Sep 17 00:00:00 2001
From: Luo <82520819+Jerry-306@users.noreply.github.com>
Date: Sat, 21 May 2022 10:48:51 +0800
Subject: [PATCH 41/69] =?UTF-8?q?=E7=BA=A0=E6=AD=A3=200047.=E5=85=A8?=
=?UTF-8?q?=E6=8E=92=E5=88=97II=20JS=E3=80=81TS=20=E7=89=88=E6=9C=AC?=
=?UTF-8?q?=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
JS 和 TS 里面 数组深拷贝一般采用 ES6 扩展运算符 ... ,或者 Array.from() 方法,而不会采用实例方法 slice. slice方法用于数组分割等操作,请注意代码书写规范!
---
problems/0047.全排列II.md | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/problems/0047.全排列II.md b/problems/0047.全排列II.md
index cce25cd9..f635b8de 100644
--- a/problems/0047.全排列II.md
+++ b/problems/0047.全排列II.md
@@ -268,7 +268,7 @@ var permuteUnique = function (nums) {
function backtracing( used) {
if (path.length === nums.length) {
- result.push(path.slice())
+ result.push([...path])
return
}
for (let i = 0; i < nums.length; i++) {
@@ -303,7 +303,7 @@ function permuteUnique(nums: number[]): number[][] {
return resArr;
function backTracking(nums: number[], route: number[]): void {
if (route.length === nums.length) {
- resArr.push(route.slice());
+ resArr.push([...route]);
return;
}
for (let i = 0, length = nums.length; i < length; i++) {
From f36c1885cc8ccac0f38538b59b0a55e1b9f47913 Mon Sep 17 00:00:00 2001
From: Luo <82520819+Jerry-306@users.noreply.github.com>
Date: Sat, 21 May 2022 11:01:21 +0800
Subject: [PATCH 42/69] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20=E5=9B=9E=E6=BA=AF?=
=?UTF-8?q?=E7=AE=97=E6=B3=95=E5=8E=BB=E9=87=8D=E9=97=AE=E9=A2=98=E7=9A=84?=
=?UTF-8?q?=E5=8F=A6=E4=B8=80=E7=A7=8D=E5=86=99=E6=B3=95=20JavaScript=20?=
=?UTF-8?q?=E7=89=88=E6=9C=AC=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...溯算法去重问题的另一种写法.md | 84 ++++++++++++++++++-
1 file changed, 81 insertions(+), 3 deletions(-)
diff --git a/problems/回溯算法去重问题的另一种写法.md b/problems/回溯算法去重问题的另一种写法.md
index f48097e1..cbfe046a 100644
--- a/problems/回溯算法去重问题的另一种写法.md
+++ b/problems/回溯算法去重问题的另一种写法.md
@@ -365,6 +365,84 @@ class Solution:
return res
```
+JavaScript:
+
+**90.子集II**
+
+```javascript
+function subsetsWithDup(nums) {
+ nums.sort((a, b) => a - b);
+ const resArr = [];
+ backTraking(nums, 0, []);
+ return resArr;
+ function backTraking(nums, startIndex, route) {
+ resArr.push([...route]);
+ const helperSet = new Set();
+ for (let i = startIndex, length = nums.length; i < length; i++) {
+ if (helperSet.has(nums[i])) continue;
+ helperSet.add(nums[i]);
+ route.push(nums[i]);
+ backTraking(nums, i + 1, route);
+ route.pop();
+ }
+ }
+};
+```
+
+**40. 组合总和 II**
+
+```javascript
+function combinationSum2(candidates, target) {
+ candidates.sort((a, b) => a - b);
+ const resArr = [];
+ backTracking(candidates, target, 0, 0, []);
+ return resArr;
+ function backTracking( candidates, target, curSum, startIndex, route ) {
+ if (curSum > target) return;
+ if (curSum === target) {
+ resArr.push([...route]);
+ return;
+ }
+ const helperSet = new Set();
+ for (let i = startIndex, length = candidates.length; i < length; i++) {
+ let tempVal = candidates[i];
+ if (helperSet.has(tempVal)) continue;
+ helperSet.add(tempVal);
+ route.push(tempVal);
+ backTracking(candidates, target, curSum + tempVal, i + 1, route);
+ route.pop();
+ }
+ }
+};
+```
+
+**47. 全排列 II**
+
+```javaescript
+function permuteUnique(nums) {
+ const resArr = [];
+ const usedArr = [];
+ backTracking(nums, []);
+ return resArr;
+ function backTracking(nums, route) {
+ if (nums.length === route.length) {
+ resArr.push([...route]);
+ return;
+ }
+ const usedSet = new Set();
+ for (let i = 0, length = nums.length; i < length; i++) {
+ if (usedArr[i] === true || usedSet.has(nums[i])) continue;
+ usedSet.add(nums[i]);
+ route.push(nums[i]);
+ usedArr[i] = true;
+ backTracking(nums, route);
+ usedArr[i] = false;
+ route.pop();
+ }
+ }
+};
+```
+
TypeScript:
**90.子集II**
@@ -376,7 +454,7 @@ function subsetsWithDup(nums: number[]): number[][] {
backTraking(nums, 0, []);
return resArr;
function backTraking(nums: number[], startIndex: number, route: number[]): void {
- resArr.push(route.slice());
+ resArr.push([...route]);
const helperSet: Set = new Set();
for (let i = startIndex, length = nums.length; i < length; i++) {
if (helperSet.has(nums[i])) continue;
@@ -403,7 +481,7 @@ function combinationSum2(candidates: number[], target: number): number[][] {
) {
if (curSum > target) return;
if (curSum === target) {
- resArr.push(route.slice());
+ resArr.push([...route]);
return;
}
const helperSet: Set = new Set();
@@ -430,7 +508,7 @@ function permuteUnique(nums: number[]): number[][] {
return resArr;
function backTracking(nums: number[], route: number[]): void {
if (nums.length === route.length) {
- resArr.push(route.slice());
+ resArr.push([...route]);
return;
}
const usedSet: Set = new Set();
From f6501464cfdfe0c207e5f1026b1b3251d9b5c927 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sat, 21 May 2022 11:48:18 +0800
Subject: [PATCH 43/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880647.=E5=9B=9E?=
=?UTF-8?q?=E6=96=87=E5=AD=90=E4=B8=B2.md=EF=BC=89=EF=BC=9A=E5=A2=9E?=
=?UTF-8?q?=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0647.回文子串.md | 57 +++++++++++++++++++++++++++++++++++
1 file changed, 57 insertions(+)
diff --git a/problems/0647.回文子串.md b/problems/0647.回文子串.md
index 913aec65..6045ba7b 100644
--- a/problems/0647.回文子串.md
+++ b/problems/0647.回文子串.md
@@ -406,6 +406,63 @@ const countSubstrings = (s) => {
}
```
+TypeScript:
+
+> 动态规划:
+
+```typescript
+function countSubstrings(s: string): number {
+ /**
+ dp[i][j]: [i,j]区间内的字符串是否为回文(左闭右闭)
+ */
+ const length: number = s.length;
+ const dp: boolean[][] = new Array(length).fill(0)
+ .map(_ => new Array(length).fill(false));
+ let resCount: number = 0;
+ // 自下而上,自左向右遍历
+ for (let i = length - 1; i >= 0; i--) {
+ for (let j = i; j < length; j++) {
+ if (
+ s[i] === s[j] &&
+ (j - i <= 1 || dp[i + 1][j - 1] === true)
+ ) {
+ dp[i][j] = true;
+ resCount++;
+ }
+ }
+ }
+ return resCount;
+};
+```
+
+> 双指针法:
+
+```typescript
+function countSubstrings(s: string): number {
+ const length: number = s.length;
+ let resCount: number = 0;
+ for (let i = 0; i < length; i++) {
+ resCount += expandRange(s, i, i);
+ resCount += expandRange(s, i, i + 1);
+ }
+ return resCount;
+};
+function expandRange(s: string, left: number, right: number): number {
+ let palindromeNum: number = 0;
+ while (
+ left >= 0 && right < s.length &&
+ s[left] === s[right]
+ ) {
+ palindromeNum++;
+ left--;
+ right++;
+ }
+ return palindromeNum;
+}
+```
+
+
+
-----------------------
From 8e7663c9c663db8c7d24aa1528d45c68fe6f5e29 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sat, 21 May 2022 14:26:24 +0800
Subject: [PATCH 44/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880516.=E6=9C=80?=
=?UTF-8?q?=E9=95=BF=E5=9B=9E=E6=96=87=E5=AD=90=E5=BA=8F=E5=88=97.md?=
=?UTF-8?q?=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=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/0516.最长回文子序列.md | 29 ++++++++++++++++++++++++++
1 file changed, 29 insertions(+)
diff --git a/problems/0516.最长回文子序列.md b/problems/0516.最长回文子序列.md
index 69536cef..1b0ee9a3 100644
--- a/problems/0516.最长回文子序列.md
+++ b/problems/0516.最长回文子序列.md
@@ -236,6 +236,35 @@ const longestPalindromeSubseq = (s) => {
};
```
+TypeScript:
+
+```typescript
+function longestPalindromeSubseq(s: string): number {
+ /**
+ dp[i][j]:[i,j]区间内,最长回文子序列的长度
+ */
+ const length: number = s.length;
+ const dp: number[][] = new Array(length).fill(0)
+ .map(_ => new Array(length).fill(0));
+ for (let i = 0; i < length; i++) {
+ dp[i][i] = 1;
+ }
+ // 自下而上,自左往右遍历
+ for (let i = length - 1; i >= 0; i--) {
+ for (let j = i + 1; j < length; j++) {
+ if (s[i] === s[j]) {
+ dp[i][j] = dp[i + 1][j - 1] + 2;
+ } else {
+ dp[i][j] = Math.max(dp[i][j - 1], dp[i + 1][j]);
+ }
+ }
+ }
+ return dp[0][length - 1];
+};
+```
+
+
+
-----------------------
From f39d349d308a4657c7cbe247060c1fcec7272497 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sat, 21 May 2022 16:19:25 +0800
Subject: [PATCH 45/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880739.=E6=AF=8F?=
=?UTF-8?q?=E6=97=A5=E6=B8=A9=E5=BA=A6.md=EF=BC=89=EF=BC=9A=E5=A2=9E?=
=?UTF-8?q?=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0739.每日温度.md | 26 ++++++++++++++++++++++++++
1 file changed, 26 insertions(+)
diff --git a/problems/0739.每日温度.md b/problems/0739.每日温度.md
index 58edd489..5e7a52ac 100644
--- a/problems/0739.每日温度.md
+++ b/problems/0739.每日温度.md
@@ -371,6 +371,32 @@ var dailyTemperatures = function(temperatures) {
};
```
+TypeScript:
+
+> 精简版:
+
+```typescript
+function dailyTemperatures(temperatures: number[]): number[] {
+ const length: number = temperatures.length;
+ const stack: number[] = [];
+ const resArr: number[] = new Array(length).fill(0);
+ stack.push(0);
+ for (let i = 1; i < length; i++) {
+ let top = stack[stack.length - 1];
+ while (
+ stack.length > 0 &&
+ temperatures[top] < temperatures[i]
+ ) {
+ resArr[top] = i - top;
+ stack.pop();
+ top = stack[stack.length - 1];
+ }
+ stack.push(i);
+ }
+ return resArr;
+};
+```
+
From ef9cefe08939725adadbefd5e49c79d7ce9e9b63 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sat, 21 May 2022 17:11:25 +0800
Subject: [PATCH 46/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200104.=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6?=
=?UTF-8?q?.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0104.二叉树的最大深度.md | 99 +++++++++++++++++++++--
1 file changed, 93 insertions(+), 6 deletions(-)
diff --git a/problems/0104.二叉树的最大深度.md b/problems/0104.二叉树的最大深度.md
index 2229a854..40c65af9 100644
--- a/problems/0104.二叉树的最大深度.md
+++ b/problems/0104.二叉树的最大深度.md
@@ -468,7 +468,7 @@ class solution:
## go
-
+### 104.二叉树的最大深度
```go
/**
* definition for a binary tree node.
@@ -521,6 +521,8 @@ func maxdepth(root *treenode) int {
## javascript
+### 104.二叉树的最大深度
+
```javascript
var maxdepth = function(root) {
if (root === null) return 0;
@@ -568,6 +570,8 @@ var maxDepth = function(root) {
};
```
+### 559.n叉树的最大深度
+
N叉树的最大深度 递归写法
```js
var maxDepth = function(root) {
@@ -600,9 +604,9 @@ var maxDepth = function(root) {
};
```
-## TypeScript:
+## TypeScript
-> 二叉树的最大深度:
+### 104.二叉树的最大深度
```typescript
// 后续遍历(自下而上)
@@ -645,7 +649,7 @@ function maxDepth(root: TreeNode | null): number {
};
```
-> N叉树的最大深度
+### 559.n叉树的最大深度
```typescript
// 后续遍历(自下而上)
@@ -675,6 +679,8 @@ function maxDepth(root: TreeNode | null): number {
## C
+### 104.二叉树的最大深度
+
二叉树最大深度递归
```c
int maxDepth(struct TreeNode* root){
@@ -731,7 +737,8 @@ int maxDepth(struct TreeNode* root){
## Swift
->二叉树最大深度
+### 104.二叉树的最大深度
+
```swift
// 递归 - 后序
func maxDepth1(_ root: TreeNode?) -> Int {
@@ -770,7 +777,8 @@ func maxDepth(_ root: TreeNode?) -> Int {
}
```
->N叉树最大深度
+### 559.n叉树的最大深度
+
```swift
// 递归
func maxDepth(_ root: Node?) -> Int {
@@ -806,5 +814,84 @@ func maxDepth1(_ root: Node?) -> Int {
}
```
+## Scala
+
+### 104.二叉树的最大深度
+递归法:
+```scala
+object Solution {
+ def maxDepth(root: TreeNode): Int = {
+ def process(curNode: TreeNode): Int = {
+ if (curNode == null) return 0
+ // 递归左节点和右节点,返回最大的,最后+1
+ math.max(process(curNode.left), process(curNode.right)) + 1
+ }
+ // 调用递归方法,return关键字可以省略
+ process(root)
+ }
+}
+```
+
+迭代法:
+```scala
+object Solution {
+ import scala.collection.mutable
+ def maxDepth(root: TreeNode): Int = {
+ var depth = 0
+ if (root == null) return depth
+ val queue = mutable.Queue[TreeNode]()
+ queue.enqueue(root)
+ while (!queue.isEmpty) {
+ val len = queue.size
+ for (i <- 0 until len) {
+ val curNode = queue.dequeue()
+ if (curNode.left != null) queue.enqueue(curNode.left)
+ if (curNode.right != null) queue.enqueue(curNode.right)
+ }
+ depth += 1 // 只要有层次就+=1
+ }
+ depth
+ }
+}
+```
+
+### 559.n叉树的最大深度
+
+递归法:
+```scala
+object Solution {
+ def maxDepth(root: Node): Int = {
+ if (root == null) return 0
+ var depth = 0
+ for (node <- root.children) {
+ depth = math.max(depth, maxDepth(node))
+ }
+ depth + 1
+ }
+}
+```
+
+迭代法: (层序遍历)
+```scala
+object Solution {
+ import scala.collection.mutable
+ def maxDepth(root: Node): Int = {
+ if (root == null) return 0
+ var depth = 0
+ val queue = mutable.Queue[Node]()
+ queue.enqueue(root)
+ while (!queue.isEmpty) {
+ val len = queue.size
+ depth += 1
+ for (i <- 0 until len) {
+ val curNode = queue.dequeue()
+ for (node <- curNode.children) queue.enqueue(node)
+ }
+ }
+ depth
+ }
+}
+```
+
-----------------------
From 3a7afacdeb785d271dbd57c236c2bd344a88b343 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sat, 21 May 2022 17:18:02 +0800
Subject: [PATCH 47/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200111.=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F=E6=B7=B1=E5=BA=A6?=
=?UTF-8?q?.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0111.二叉树的最小深度.md | 39 +++++++++++++++++++++++
1 file changed, 39 insertions(+)
diff --git a/problems/0111.二叉树的最小深度.md b/problems/0111.二叉树的最小深度.md
index 224caa5e..a7eb913e 100644
--- a/problems/0111.二叉树的最小深度.md
+++ b/problems/0111.二叉树的最小深度.md
@@ -488,5 +488,44 @@ func minDepth(_ root: TreeNode?) -> Int {
}
```
+## Scala
+
+递归法:
+```scala
+object Solution {
+ def minDepth(root: TreeNode): Int = {
+ if (root == null) return 0
+ if (root.left == null && root.right != null) return 1 + minDepth(root.right)
+ if (root.left != null && root.right == null) return 1 + minDepth(root.left)
+ // 如果两侧都不为空,则取最小值,return关键字可以省略
+ 1 + math.min(minDepth(root.left), minDepth(root.right))
+ }
+}
+```
+
+迭代法:
+```scala
+object Solution {
+ import scala.collection.mutable
+ def minDepth(root: TreeNode): Int = {
+ if (root == null) return 0
+ var depth = 0
+ val queue = mutable.Queue[TreeNode]()
+ queue.enqueue(root)
+ while (!queue.isEmpty) {
+ depth += 1
+ val len = queue.size
+ for (i <- 0 until len) {
+ val curNode = queue.dequeue()
+ if (curNode.left != null) queue.enqueue(curNode.left)
+ if (curNode.right != null) queue.enqueue(curNode.right)
+ if (curNode.left == null && curNode.right == null) return depth
+ }
+ }
+ depth
+ }
+}
+```
+
-----------------------
From 9cbd053e0500846fccac3936aeb497a75906133e Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sat, 21 May 2022 19:49:59 +0800
Subject: [PATCH 48/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880496.=E4=B8=8B?=
=?UTF-8?q?=E4=B8=80=E4=B8=AA=E6=9B=B4=E5=A4=A7=E5=85=83=E7=B4=A0I.md?=
=?UTF-8?q?=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=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/0496.下一个更大元素I.md | 31 +++++++++++++++++++++++++
1 file changed, 31 insertions(+)
diff --git a/problems/0496.下一个更大元素I.md b/problems/0496.下一个更大元素I.md
index 02339677..274cc32b 100644
--- a/problems/0496.下一个更大元素I.md
+++ b/problems/0496.下一个更大元素I.md
@@ -332,5 +332,36 @@ var nextGreaterElement = function (nums1, nums2) {
};
```
+TypeScript:
+
+```typescript
+function nextGreaterElement(nums1: number[], nums2: number[]): number[] {
+ const resArr: number[] = new Array(nums1.length).fill(-1);
+ const stack: number[] = [];
+ const helperMap: Map = new Map();
+ nums1.forEach((num, index) => {
+ helperMap.set(num, index);
+ })
+ stack.push(0);
+ for (let i = 1, length = nums2.length; i < length; i++) {
+ let top = stack[stack.length - 1];
+ while (stack.length > 0 && nums2[top] < nums2[i]) {
+ let index = helperMap.get(nums2[top]);
+ if (index !== undefined) {
+ resArr[index] = nums2[i];
+ }
+ stack.pop();
+ top = stack[stack.length - 1];
+ }
+ if (helperMap.get(nums2[i]) !== undefined) {
+ stack.push(i);
+ }
+ }
+ return resArr;
+};
+```
+
+
+
-----------------------
From 10ad5411d98765272a4119ecf97b6d72c8e85ada Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sat, 21 May 2022 20:37:58 +0800
Subject: [PATCH 49/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880503.=E4=B8=8B?=
=?UTF-8?q?=E4=B8=80=E4=B8=AA=E6=9B=B4=E5=A4=A7=E5=85=83=E7=B4=A0II.md?=
=?UTF-8?q?=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=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/0503.下一个更大元素II.md | 26 ++++++++++++++++++++++++
1 file changed, 26 insertions(+)
diff --git a/problems/0503.下一个更大元素II.md b/problems/0503.下一个更大元素II.md
index ace4d40b..33807d26 100644
--- a/problems/0503.下一个更大元素II.md
+++ b/problems/0503.下一个更大元素II.md
@@ -182,5 +182,31 @@ var nextGreaterElements = function (nums) {
return res;
};
```
+TypeScript:
+
+```typescript
+function nextGreaterElements(nums: number[]): number[] {
+ const length: number = nums.length;
+ const stack: number[] = [];
+ stack.push(0);
+ const resArr: number[] = new Array(length).fill(-1);
+ for (let i = 1; i < length * 2; i++) {
+ const index = i % length;
+ let top = stack[stack.length - 1];
+ while (stack.length > 0 && nums[top] < nums[index]) {
+ resArr[top] = nums[index];
+ stack.pop();
+ top = stack[stack.length - 1];
+ }
+ if (i < length) {
+ stack.push(i);
+ }
+ }
+ return resArr;
+};
+```
+
+
+
-----------------------
From 3de2992235e43b6ce40a35624a42a08b5a5a866b Mon Sep 17 00:00:00 2001
From: xuxiaomeng <961592690@qq.com>
Date: Sat, 21 May 2022 21:02:59 +0800
Subject: [PATCH 50/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A00027.=E7=A7=BB=E9=99=A4?=
=?UTF-8?q?=E5=85=83=E7=B4=A0=20Kotlin=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0027.移除元素.md | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md
index 590cf0b9..f0cb5375 100644
--- a/problems/0027.移除元素.md
+++ b/problems/0027.移除元素.md
@@ -329,5 +329,16 @@ int removeElement(int* nums, int numsSize, int val){
}
```
+Kotlin:
+```kotlin
+fun removeElement(nums: IntArray, `val`: Int): Int {
+ var slowIndex = 0 // 初始化慢指针
+ for (fastIndex in nums.indices) {
+ if (nums[fastIndex] != `val`) nums[slowIndex++] = nums[fastIndex] // 在慢指针所在位置存储未被删除的元素
+ }
+ return slowIndex
+ }
+```
+
-----------------------
From 97211892492753b1903a882c68b4387051b652cb Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sun, 22 May 2022 00:12:15 +0800
Subject: [PATCH 51/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880042.=E6=8E=A5?=
=?UTF-8?q?=E9=9B=A8=E6=B0=B4.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typesc?=
=?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/0042.接雨水.md | 85 ++++++++++++++++++++++++++++++++++++++
1 file changed, 85 insertions(+)
diff --git a/problems/0042.接雨水.md b/problems/0042.接雨水.md
index b232ce22..060c0b45 100644
--- a/problems/0042.接雨水.md
+++ b/problems/0042.接雨水.md
@@ -744,6 +744,91 @@ var trap = function(height) {
};
```
+### TypeScript
+
+双指针法:
+
+```typescript
+function trap(height: number[]): number {
+ const length: number = height.length;
+ let resVal: number = 0;
+ for (let i = 0; i < length; i++) {
+ let leftMaxHeight: number = height[i],
+ rightMaxHeight: number = height[i];
+ let leftIndex: number = i - 1,
+ rightIndex: number = i + 1;
+ while (leftIndex >= 0) {
+ if (height[leftIndex] > leftMaxHeight)
+ leftMaxHeight = height[leftIndex];
+ leftIndex--;
+ }
+ while (rightIndex < length) {
+ if (height[rightIndex] > rightMaxHeight)
+ rightMaxHeight = height[rightIndex];
+ rightIndex++;
+ }
+ resVal += Math.min(leftMaxHeight, rightMaxHeight) - height[i];
+ }
+ return resVal;
+};
+```
+
+动态规划:
+
+```typescript
+function trap(height: number[]): number {
+ const length: number = height.length;
+ const leftMaxHeightDp: number[] = [],
+ rightMaxHeightDp: number[] = [];
+ leftMaxHeightDp[0] = height[0];
+ rightMaxHeightDp[length - 1] = height[length - 1];
+ for (let i = 1; i < length; i++) {
+ leftMaxHeightDp[i] = Math.max(height[i], leftMaxHeightDp[i - 1]);
+ }
+ for (let i = length - 2; i >= 0; i--) {
+ rightMaxHeightDp[i] = Math.max(height[i], rightMaxHeightDp[i + 1]);
+ }
+ let resVal: number = 0;
+ for (let i = 0; i < length; i++) {
+ resVal += Math.min(leftMaxHeightDp[i], rightMaxHeightDp[i]) - height[i];
+ }
+ return resVal;
+};
+```
+
+单调栈:
+
+```typescript
+function trap(height: number[]): number {
+ const length: number = height.length;
+ const stack: number[] = [];
+ stack.push(0);
+ let resVal: number = 0;
+ for (let i = 1; i < length; i++) {
+ let top = stack[stack.length - 1];
+ if (height[top] > height[i]) {
+ stack.push(i);
+ } else if (height[top] === height[i]) {
+ stack.pop();
+ stack.push(i);
+ } else {
+ while (stack.length > 0 && height[top] < height[i]) {
+ let mid = stack.pop();
+ if (stack.length > 0) {
+ let left = stack[stack.length - 1];
+ let h = Math.min(height[left], height[i]) - height[mid];
+ let w = i - left - 1;
+ resVal += h * w;
+ top = stack[stack.length - 1];
+ }
+ }
+ stack.push(i);
+ }
+ }
+ return resVal;
+};
+```
+
### C:
一种更简便的双指针方法:
From b06b83867b466fa2d72268c0a6ef50e4da52aac3 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sun, 22 May 2022 17:09:55 +0800
Subject: [PATCH 52/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880084.=E6=9F=B1?=
=?UTF-8?q?=E7=8A=B6=E5=9B=BE=E4=B8=AD=E6=9C=80=E5=A4=A7=E7=9A=84=E7=9F=A9?=
=?UTF-8?q?=E5=BD=A2.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0084.柱状图中最大的矩形.md | 90 ++++++++++++++++++++
1 file changed, 90 insertions(+)
diff --git a/problems/0084.柱状图中最大的矩形.md b/problems/0084.柱状图中最大的矩形.md
index 439a3bc5..8f10a582 100644
--- a/problems/0084.柱状图中最大的矩形.md
+++ b/problems/0084.柱状图中最大的矩形.md
@@ -486,5 +486,95 @@ var largestRectangleArea = function(heights) {
return maxArea;
};
```
+TypeScript:
+
+> 双指针法(会超时):
+
+```typescript
+function largestRectangleArea(heights: number[]): number {
+ let resMax: number = 0;
+ for (let i = 0, length = heights.length; i < length; i++) {
+ // 左开右开
+ let left: number = i - 1,
+ right: number = i + 1;
+ while (left >= 0 && heights[left] >= heights[i]) {
+ left--;
+ }
+ while (right < length && heights[right] >= heights[i]) {
+ right++;
+ }
+ resMax = Math.max(resMax, heights[i] * (right - left - 1));
+ }
+ return resMax;
+};
+```
+
+> 动态规划预处理:
+
+```typescript
+function largestRectangleArea(heights: number[]): number {
+ const length: number = heights.length;
+ const leftHeightDp: number[] = [],
+ rightHeightDp: number[] = [];
+ leftHeightDp[0] = -1;
+ rightHeightDp[length - 1] = length;
+ for (let i = 1; i < length; i++) {
+ let j = i - 1;
+ while (j >= 0 && heights[i] <= heights[j]) {
+ j = leftHeightDp[j];
+ }
+ leftHeightDp[i] = j;
+ }
+ for (let i = length - 2; i >= 0; i--) {
+ let j = i + 1;
+ while (j < length && heights[i] <= heights[j]) {
+ j = rightHeightDp[j];
+ }
+ rightHeightDp[i] = j;
+ }
+ let resMax: number = 0;
+ for (let i = 0; i < length; i++) {
+ let area = heights[i] * (rightHeightDp[i] - leftHeightDp[i] - 1);
+ resMax = Math.max(resMax, area);
+ }
+ return resMax;
+};
+```
+
+> 单调栈:
+
+```typescript
+function largestRectangleArea(heights: number[]): number {
+ heights.push(0);
+ const length: number = heights.length;
+ // 栈底->栈顶:严格单调递增
+ const stack: number[] = [];
+ stack.push(0);
+ let resMax: number = 0;
+ for (let i = 1; i < length; i++) {
+ let top = stack[stack.length - 1];
+ if (heights[top] < heights[i]) {
+ stack.push(i);
+ } else if (heights[top] === heights[i]) {
+ stack.pop();
+ stack.push(i);
+ } else {
+ while (stack.length > 0 && heights[top] > heights[i]) {
+ let mid = stack.pop();
+ let left = stack.length > 0 ? stack[stack.length - 1] : -1;
+ let w = i - left - 1;
+ let h = heights[mid];
+ resMax = Math.max(resMax, w * h);
+ top = stack[stack.length - 1];
+ }
+ stack.push(i);
+ }
+ }
+ return resMax;
+};
+```
+
+
+
-----------------------
From 10b440ec27b814715dad23110b919c33d59403b6 Mon Sep 17 00:00:00 2001
From: xuxiaomeng <961592690@qq.com>
Date: Mon, 23 May 2022 09:38:24 +0800
Subject: [PATCH 53/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A00977.=E6=9C=89=E5=BA=8F?=
=?UTF-8?q?=E6=95=B0=E7=BB=84=E7=9A=84=E5=B9=B3=E6=96=B9=20Kotlin=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/0977.有序数组的平方.md | 23 ++++++++++++++++++++++-
1 file changed, 22 insertions(+), 1 deletion(-)
diff --git a/problems/0977.有序数组的平方.md b/problems/0977.有序数组的平方.md
index 24276bcf..9c06d8a3 100644
--- a/problems/0977.有序数组的平方.md
+++ b/problems/0977.有序数组的平方.md
@@ -359,7 +359,28 @@ class Solution {
}
```
-
+Kotlin:
+```kotlin
+class Solution {
+ // 双指针法
+ fun sortedSquares(nums: IntArray): IntArray {
+ var res = IntArray(nums.size)
+ var left = 0 // 指向数组的最左端
+ var right = nums.size - 1 // 指向数组端最右端
+ // 选择平方数更大的那一个往 res 数组中倒序填充
+ for (index in nums.size - 1 downTo 0) {
+ if (nums[left] * nums[left] > nums[right] * nums[right]) {
+ res[index] = nums[left] * nums[left]
+ left++
+ } else {
+ res[index] = nums[right] * nums[right]
+ right--
+ }
+ }
+ return res
+ }
+}
+```
-----------------------
From 530422fa3ecf004d27f45552e909506947992902 Mon Sep 17 00:00:00 2001
From: Luo <82520819+Jerry-306@users.noreply.github.com>
Date: Mon, 23 May 2022 10:04:15 +0800
Subject: [PATCH 54/69] =?UTF-8?q?0416.=E5=88=86=E5=89=B2=E7=AD=89=E5=92=8C?=
=?UTF-8?q?=E5=AD=90=E9=9B=86=20=E6=96=B0=E5=A2=9E=20typescript=20?=
=?UTF-8?q?=E8=A7=A3=E6=B3=95=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0416.分割等和子集.md | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
diff --git a/problems/0416.分割等和子集.md b/problems/0416.分割等和子集.md
index 6e93ae8e..01ea825e 100644
--- a/problems/0416.分割等和子集.md
+++ b/problems/0416.分割等和子集.md
@@ -416,7 +416,24 @@ var canPartition = function(nums) {
};
```
+TypeScript:
+```ts
+function canPartition(nums: number[]): boolean {
+ const sum: number = nums.reduce((a: number, b: number): number => a + b);
+ if (sum % 2 === 1) return false;
+ const target: number = sum / 2;
+ // dp[j]表示容量(总数和)为j的背包所能装下的数(下标[0, i]之间任意取)的总和(<= 容量)的最大值
+ const dp: number[] = new Array(target + 1).fill(0);
+ const n: number = nums.length;
+ for (let i: number = 0; i < n; i++) {
+ for (let j: number = target; j >= nums[i]; j--) {
+ dp[j] = Math.max(dp[j], dp[j - nums[i]] + nums[i]);
+ }
+ }
+ return dp[target] === target;
+};
+```
-----------------------
From 8f9c8e5fb1f9d89830b92a06f1fc59d97fd84772 Mon Sep 17 00:00:00 2001
From: Luo <82520819+Jerry-306@users.noreply.github.com>
Date: Mon, 23 May 2022 10:10:48 +0800
Subject: [PATCH 55/69] =?UTF-8?q?1049.=E6=9C=80=E5=90=8E=E4=B8=80=E5=9D=97?=
=?UTF-8?q?=E7=9F=B3=E5=A4=B4=E9=87=8D=E9=87=8F=20=E6=96=B0=E5=A2=9Etypesc?=
=?UTF-8?q?ript=E7=89=88=E6=9C=AC=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/1049.最后一块石头的重量II.md | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/problems/1049.最后一块石头的重量II.md b/problems/1049.最后一块石头的重量II.md
index ee0ddef2..8d3eeb3b 100644
--- a/problems/1049.最后一块石头的重量II.md
+++ b/problems/1049.最后一块石头的重量II.md
@@ -277,5 +277,23 @@ var lastStoneWeightII = function (stones) {
};
```
+TypeScript版本
+
+```ts
+function lastStoneWeightII(stones: number[]): number {
+ const sum: number = stones.reduce((a: number, b:number): number => a + b);
+ const target: number = Math.floor(sum / 2);
+ const n: number = stones.length;
+ // dp[j]表示容量(总数和)为j的背包所能装下的数(下标[0, i]之间任意取)的总和(<= 容量)的最大值
+ const dp: number[] = new Array(target + 1).fill(0);
+ for (let i: number = 0; i < n; i++ ) {
+ for (let j: number = target; j >= stones[i]; j--) {
+ dp[j] = Math.max(dp[j], dp[j - stones[i]] + stones[i]);
+ }
+ }
+ return sum - dp[target] - dp[target];
+};
+```
+
-----------------------
From ffc91f1f1ca6099af747c77c5cdbfc21125fc140 Mon Sep 17 00:00:00 2001
From: Luo <82520819+Jerry-306@users.noreply.github.com>
Date: Mon, 23 May 2022 10:20:08 +0800
Subject: [PATCH 56/69] =?UTF-8?q?0494.=E7=9B=AE=E6=A0=87=E5=92=8C=20?=
=?UTF-8?q?=E6=96=B0=E5=A2=9Etypescript=E7=89=88=E6=9C=AC=E4=BB=A3?=
=?UTF-8?q?=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0494.目标和.md | 22 ++++++++++++++++++++++
1 file changed, 22 insertions(+)
diff --git a/problems/0494.目标和.md b/problems/0494.目标和.md
index 99b76834..929b97d4 100644
--- a/problems/0494.目标和.md
+++ b/problems/0494.目标和.md
@@ -351,6 +351,28 @@ const findTargetSumWays = (nums, target) => {
};
```
+TypeScript:
+
+```ts
+function findTargetSumWays(nums: number[], target: number): number {
+ // 把数组分成两个组合left, right.left + right = sum, left - right = target.
+ const sum: number = nums.reduce((a: number, b: number): number => a + b);
+ if ((sum + target) % 2 || Math.abs(target) > sum) return 0;
+ const left: number = (sum + target) / 2;
+
+ // 将问题转化为装满容量为left的背包有多少种方法
+ // dp[i]表示装满容量为i的背包有多少种方法
+ const dp: number[] = new Array(left + 1).fill(0);
+ dp[0] = 1; // 装满容量为0的背包有1种方法(什么也不装)
+ for (let i: number = 0; i < nums.length; i++) {
+ for (let j: number = left; j >= nums[i]; j--) {
+ dp[j] += dp[j - nums[i]];
+ }
+ }
+ return dp[left];
+};
+```
+
-----------------------
From 282cdc2b44b755098d223d0f9c8bd36bf0e85959 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Mon, 23 May 2022 11:18:04 +0800
Subject: [PATCH 57/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=881365.=E6=9C=89?=
=?UTF-8?q?=E5=A4=9A=E5=B0=91=E5=B0=8F=E4=BA=8E=E5=BD=93=E5=89=8D=E6=95=B0?=
=?UTF-8?q?=E5=AD=97=E7=9A=84=E6=95=B0=E5=AD=97.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...65.有多少小于当前数字的数字.md | 40 +++++++++++++++++++
1 file changed, 40 insertions(+)
diff --git a/problems/1365.有多少小于当前数字的数字.md b/problems/1365.有多少小于当前数字的数字.md
index 78fa84c0..ce1e77df 100644
--- a/problems/1365.有多少小于当前数字的数字.md
+++ b/problems/1365.有多少小于当前数字的数字.md
@@ -217,6 +217,46 @@ var smallerNumbersThanCurrent = function(nums) {
};
```
+TypeScript:
+
+> 暴力法:
+
+```typescript
+function smallerNumbersThanCurrent(nums: number[]): number[] {
+ const length: number = nums.length;
+ const resArr: number[] = [];
+ for (let i = 0; i < length; i++) {
+ let count: number = 0;
+ for (let j = 0; j < length; j++) {
+ if (nums[j] < nums[i]) {
+ count++;
+ }
+ }
+ resArr[i] = count;
+ }
+ return resArr;
+};
+```
+
+> 排序+hash
+
+```typescript
+function smallerNumbersThanCurrent(nums: number[]): number[] {
+ const length: number = nums.length;
+ const sortedArr: number[] = [...nums];
+ sortedArr.sort((a, b) => a - b);
+ const hashMap: Map = new Map();
+ for (let i = length - 1; i >= 0; i--) {
+ hashMap.set(sortedArr[i], i);
+ }
+ const resArr: number[] = [];
+ for (let i = 0; i < length; i++) {
+ resArr[i] = hashMap.get(nums[i]);
+ }
+ return resArr;
+};
+```
+
-----------------------
From f83d5edb6ebf5cd6de1eabba51a246a921f94e1b Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Mon, 23 May 2022 19:14:45 +0800
Subject: [PATCH 58/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200222.=E5=AE=8C?=
=?UTF-8?q?=E5=85=A8=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84=E8=8A=82=E7=82=B9?=
=?UTF-8?q?=E4=B8=AA=E6=95=B0.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../0222.完全二叉树的节点个数.md | 63 +++++++++++++++++++
1 file changed, 63 insertions(+)
diff --git a/problems/0222.完全二叉树的节点个数.md b/problems/0222.完全二叉树的节点个数.md
index ba7acc5a..746d45cc 100644
--- a/problems/0222.完全二叉树的节点个数.md
+++ b/problems/0222.完全二叉树的节点个数.md
@@ -646,5 +646,68 @@ func countNodes(_ root: TreeNode?) -> Int {
}
```
+## Scala
+
+递归:
+```scala
+object Solution {
+ def countNodes(root: TreeNode): Int = {
+ if(root == null) return 0
+ 1 + countNodes(root.left) + countNodes(root.right)
+ }
+}
+```
+
+层序遍历:
+```scala
+object Solution {
+ import scala.collection.mutable
+ def countNodes(root: TreeNode): Int = {
+ if (root == null) return 0
+ val queue = mutable.Queue[TreeNode]()
+ var node = 0
+ queue.enqueue(root)
+ while (!queue.isEmpty) {
+ val len = queue.size
+ for (i <- 0 until len) {
+ node += 1
+ val curNode = queue.dequeue()
+ if (curNode.left != null) queue.enqueue(curNode.left)
+ if (curNode.right != null) queue.enqueue(curNode.right)
+ }
+ }
+ node
+ }
+}
+```
+
+利用完全二叉树性质:
+```scala
+object Solution {
+ def countNodes(root: TreeNode): Int = {
+ if (root == null) return 0
+ var leftNode = root.left
+ var rightNode = root.right
+ // 向左向右往下探
+ var leftDepth = 0
+ while (leftNode != null) {
+ leftDepth += 1
+ leftNode = leftNode.left
+ }
+ var rightDepth = 0
+ while (rightNode != null) {
+ rightDepth += 1
+ rightNode = rightNode.right
+ }
+ // 如果相等就是一个满二叉树
+ if (leftDepth == rightDepth) {
+ return (2 << leftDepth) - 1
+ }
+ // 如果不相等就不是一个完全二叉树,继续向下递归
+ countNodes(root.left) + countNodes(root.right) + 1
+ }
+}
+```
+
-----------------------
From 45a3c91a7a954947d6b7373bee876d2f498e26bd Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Mon, 23 May 2022 19:49:18 +0800
Subject: [PATCH 59/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200257.=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=89=80=E6=9C=89=E8=B7=AF=E5=BE=84?=
=?UTF-8?q?.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0257.二叉树的所有路径.md | 30 +++++++++++++++++++++++
1 file changed, 30 insertions(+)
diff --git a/problems/0257.二叉树的所有路径.md b/problems/0257.二叉树的所有路径.md
index 1362897c..70a3c66f 100644
--- a/problems/0257.二叉树的所有路径.md
+++ b/problems/0257.二叉树的所有路径.md
@@ -702,5 +702,35 @@ func binaryTreePaths(_ root: TreeNode?) -> [String] {
}
```
+Scala:
+
+递归:
+```scala
+object Solution {
+ import scala.collection.mutable.ListBuffer
+ def binaryTreePaths(root: TreeNode): List[String] = {
+ val res = ListBuffer[String]()
+ def traversal(curNode: TreeNode, path: ListBuffer[Int]): Unit = {
+ path.append(curNode.value)
+ if (curNode.left == null && curNode.right == null) {
+ res.append(path.mkString("->")) // mkString函数: 将数组的所有值按照指定字符串拼接
+ return // 处理完可以直接return
+ }
+
+ if (curNode.left != null) {
+ traversal(curNode.left, path)
+ path.remove(path.size - 1)
+ }
+ if (curNode.right != null) {
+ traversal(curNode.right, path)
+ path.remove(path.size - 1)
+ }
+ }
+ traversal(root, ListBuffer[Int]())
+ res.toList
+ }
+}
+```
+
-----------------------
From 6d826271dc78b310dc17f107915707115a5bd544 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Tue, 24 May 2022 11:11:51 +0800
Subject: [PATCH 60/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880941.=E6=9C=89?=
=?UTF-8?q?=E6=95=88=E7=9A=84=E5=B1=B1=E8=84=89=E6=95=B0=E7=BB=84.md?=
=?UTF-8?q?=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=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/0941.有效的山脉数组.md | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)
diff --git a/problems/0941.有效的山脉数组.md b/problems/0941.有效的山脉数组.md
index 4b7a978c..7004e84c 100644
--- a/problems/0941.有效的山脉数组.md
+++ b/problems/0941.有效的山脉数组.md
@@ -157,6 +157,26 @@ var validMountainArray = function(arr) {
};
```
+## TypeScript
+
+```typescript
+function validMountainArray(arr: number[]): boolean {
+ const length: number = arr.length;
+ if (length < 3) return false;
+ let left: number = 0,
+ right: number = length - 1;
+ while (left < (length - 1) && arr[left] < arr[left + 1]) {
+ left++;
+ }
+ while (right > 0 && arr[right] < arr[right - 1]) {
+ right--;
+ }
+ if (left === right && left !== 0 && right !== length - 1)
+ return true;
+ return false;
+};
+```
+
From 211f9f28dd6ed9f9540cd5feac9c8e9bd9f80568 Mon Sep 17 00:00:00 2001
From: SevenMonths
Date: Tue, 24 May 2022 14:39:04 +0800
Subject: [PATCH 61/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880344.=E5=8F=8D?=
=?UTF-8?q?=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2.md=EF=BC=89=EF=BC=9APHP?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0344.反转字符串.md | 29 +++++++++++++++++++++++++++++
1 file changed, 29 insertions(+)
diff --git a/problems/0344.反转字符串.md b/problems/0344.反转字符串.md
index 58bada05..583d4f78 100644
--- a/problems/0344.反转字符串.md
+++ b/problems/0344.反转字符串.md
@@ -267,5 +267,34 @@ public class Solution
}
```
+PHP:
+```php
+// 双指针
+// 一:
+function reverseString(&$s) {
+ $left = 0;
+ $right = count($s)-1;
+ while($left<$right){
+ $temp = $s[$left];
+ $s[$left] = $s[$right];
+ $s[$right] = $temp;
+ $left++;
+ $right--;
+ }
+}
+
+// 二:
+function reverseString(&$s) {
+ $this->reverse($s,0,count($s)-1);
+}
+// 按指定位置交换元素
+function reverse(&$s, $start, $end) {
+ for ($i = $start, $j = $end; $i < $j; $i++, $j--) {
+ $tmp = $s[$i];
+ $s[$i] = $s[$j];
+ $s[$j] = $tmp;
+ }
+}
+```
-----------------------
From ded66cf2f18df921edccfbfbf0325635c4fb04ad Mon Sep 17 00:00:00 2001
From: wang2jun <91008685+wang2jun@users.noreply.github.com>
Date: Tue, 24 May 2022 14:53:33 +0800
Subject: [PATCH 62/69] =?UTF-8?q?Update=200052.N=E7=9A=87=E5=90=8EII.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
修改错别字:想-》详
---
problems/0052.N皇后II.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/problems/0052.N皇后II.md b/problems/0052.N皇后II.md
index 67e439ca..1c45a7f1 100644
--- a/problems/0052.N皇后II.md
+++ b/problems/0052.N皇后II.md
@@ -44,7 +44,7 @@ n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并
# 思路
-想看:[51.N皇后](https://mp.weixin.qq.com/s/lU_QwCMj6g60nh8m98GAWg) ,基本没有区别
+详看:[51.N皇后](https://mp.weixin.qq.com/s/lU_QwCMj6g60nh8m98GAWg) ,基本没有区别
# C++代码
From 3665c051d794a3abc3b350c8cc5cdb897ef54f76 Mon Sep 17 00:00:00 2001
From: SevenMonths
Date: Tue, 24 May 2022 16:53:39 +0800
Subject: [PATCH 63/69] =?UTF-8?q?=E5=A2=9E=E5=8A=A0(=E5=89=91=E6=8C=87Offe?=
=?UTF-8?q?r05.=E6=9B=BF=E6=8D=A2=E7=A9=BA=E6=A0=BC.md)=EF=BC=9Aphp?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/剑指Offer05.替换空格.md | 34 ++++++++++++++++++++++++++
1 file changed, 34 insertions(+)
diff --git a/problems/剑指Offer05.替换空格.md b/problems/剑指Offer05.替换空格.md
index 037bd427..63fb021a 100644
--- a/problems/剑指Offer05.替换空格.md
+++ b/problems/剑指Offer05.替换空格.md
@@ -415,6 +415,40 @@ func replaceSpace(_ s: String) -> String {
+PHP:
+```php
+function replaceSpace($s){
+ $sLen = strlen($s);
+ $moreLen = $this->spaceLen($s) * 2;
+
+ $head = $sLen - 1;
+ $tail = $sLen + $moreLen - 1;
+
+ $s = $s . str_repeat(' ', $moreLen);
+ while ($head != $tail) {
+ if ($s[$head] == ' ') {
+ $s[$tail--] = '0';
+ $s[$tail--] = '2';
+ $s[$tail] = '%';
+ } else {
+ $s[$tail] = $s[$head];
+ }
+ $head--;
+ $tail--;
+ }
+ return $s;
+}
+// 统计空格个数
+function spaceLen($s){
+ $count = 0;
+ for ($i = 0; $i < strlen($s); $i++) {
+ if ($s[$i] == ' ') {
+ $count++;
+ }
+ }
+ return $count;
+}
+```
-----------------------
From a69ee03b3eea09f7e2bb113b631462eadd313482 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Tue, 24 May 2022 18:59:17 +0800
Subject: [PATCH 64/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200404.=E5=B7=A6?=
=?UTF-8?q?=E5=8F=B6=E5=AD=90=E4=B9=8B=E5=92=8C.md=20Scala=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/0404.左叶子之和.md | 38 ++++++++++++++++++++++++++++++++
1 file changed, 38 insertions(+)
diff --git a/problems/0404.左叶子之和.md b/problems/0404.左叶子之和.md
index d7fd629e..78fc58f3 100644
--- a/problems/0404.左叶子之和.md
+++ b/problems/0404.左叶子之和.md
@@ -516,6 +516,44 @@ int sumOfLeftLeaves(struct TreeNode* root){
}
```
+## Scala
+
+**递归:**
+```scala
+object Solution {
+ def sumOfLeftLeaves(root: TreeNode): Int = {
+ if(root == null) return 0
+ var midValue = 0
+ if(root.left != null && root.left.left == null && root.left.right == null){
+ midValue = root.left.value
+ }
+ // return关键字可以省略
+ midValue + sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right)
+ }
+}
+```
+
+**迭代:**
+```scala
+object Solution {
+ import scala.collection.mutable
+ def sumOfLeftLeaves(root: TreeNode): Int = {
+ val stack = mutable.Stack[TreeNode]()
+ if (root == null) return 0
+ stack.push(root)
+ var sum = 0
+ while (!stack.isEmpty) {
+ val curNode = stack.pop()
+ if (curNode.left != null && curNode.left.left == null && curNode.left.right == null) {
+ sum += curNode.left.value // 如果满足条件就累加
+ }
+ if (curNode.right != null) stack.push(curNode.right)
+ if (curNode.left != null) stack.push(curNode.left)
+ }
+ sum
+ }
+}
+```
-----------------------
From 591cf193b0effd7606a4a50860804a25f06dea7c Mon Sep 17 00:00:00 2001
From: SevenMonths
Date: Tue, 24 May 2022 19:06:30 +0800
Subject: [PATCH 65/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0(0151.=E7=BF=BB?=
=?UTF-8?q?=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2=E9=87=8C=E7=9A=84=E5=8D=95?=
=?UTF-8?q?=E8=AF=8D.md)=EF=BC=9APHP=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0151.翻转字符串里的单词.md | 47 ++++++++++++++++++++
1 file changed, 47 insertions(+)
diff --git a/problems/0151.翻转字符串里的单词.md b/problems/0151.翻转字符串里的单词.md
index d03de421..e76e05dd 100644
--- a/problems/0151.翻转字符串里的单词.md
+++ b/problems/0151.翻转字符串里的单词.md
@@ -761,6 +761,53 @@ func reverseWord(_ s: inout [Character]) {
+PHP:
+```php
+function reverseWords($s) {
+ $this->removeExtraSpaces($s);
+ $this->reverseString($s, 0, strlen($s)-1);
+ // 将每个单词反转
+ $start = 0;
+ for ($i = 0; $i <= strlen($s); $i++) {
+ // 到达空格或者串尾,说明一个单词结束。进行翻转。
+ if ($i == strlen($s) || $s[$i] == ' ') {
+ // 翻转,注意是左闭右闭 []的翻转。
+ $this->reverseString($s, $start, $i-1);
+ // +1: 单词与单词直接有个空格
+ $start = $i + 1;
+ }
+ }
+ return $s;
+}
+
+// 移除多余空格
+function removeExtraSpaces(&$s){
+ $slow = 0;
+ for ($i = 0; $i < strlen($s); $i++) {
+ if ($s[$i] != ' ') {
+ if ($slow != 0){
+ $s[$slow++] = ' ';
+ }
+ while ($i < strlen($s) && $s[$i] != ' ') {
+ $s[$slow++] = $s[$i++];
+ }
+ }
+ }
+ // 移动覆盖处理,丢弃多余的脏数据。
+ $s = substr($s,0,$slow);
+ return ;
+}
+
+// 翻转字符串
+function reverseString(&$s, $start, $end) {
+ for ($i = $start, $j = $end; $i < $j; $i++, $j--) {
+ $tmp = $s[$i];
+ $s[$i] = $s[$j];
+ $s[$j] = $tmp;
+ }
+ return ;
+}
+```
-----------------------
From 17a2ea3c435677f9b7b21a413bac1939208fae22 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Tue, 24 May 2022 19:21:53 +0800
Subject: [PATCH 66/69] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200513.=E6=89=BE?=
=?UTF-8?q?=E6=A0=91=E5=B7=A6=E4=B8=8B=E8=A7=92=E7=9A=84=E5=80=BC.md=20Sca?=
=?UTF-8?q?la=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0513.找树左下角的值.md | 43 ++++++++++++++++++++++++++
1 file changed, 43 insertions(+)
diff --git a/problems/0513.找树左下角的值.md b/problems/0513.找树左下角的值.md
index 12c62c70..296fe478 100644
--- a/problems/0513.找树左下角的值.md
+++ b/problems/0513.找树左下角的值.md
@@ -546,7 +546,50 @@ func findBottomLeftValue(_ root: TreeNode?) -> Int {
}
```
+## Scala
+递归版本:
+```scala
+object Solution {
+ def findBottomLeftValue(root: TreeNode): Int = {
+ var maxLeftValue = 0
+ var maxLen = Int.MinValue
+ // 递归方法
+ def traversal(node: TreeNode, leftLen: Int): Unit = {
+ // 如果左右都为空并且当前深度大于最大深度,记录最左节点的值
+ if (node.left == null && node.right == null && leftLen > maxLen) {
+ maxLen = leftLen
+ maxLeftValue = node.value
+ }
+ if (node.left != null) traversal(node.left, leftLen + 1)
+ if (node.right != null) traversal(node.right, leftLen + 1)
+ }
+ traversal(root, 0) // 调用方法
+ maxLeftValue // return关键字可以省略
+ }
+}
+```
+
+层序遍历:
+```scala
+ import scala.collection.mutable
+
+ def findBottomLeftValue(root: TreeNode): Int = {
+ val queue = mutable.Queue[TreeNode]()
+ queue.enqueue(root)
+ var res = 0 // 记录每层最左侧结果
+ while (!queue.isEmpty) {
+ val len = queue.size
+ for (i <- 0 until len) {
+ val curNode = queue.dequeue()
+ if (i == 0) res = curNode.value // 记录最最左侧的节点
+ if (curNode.left != null) queue.enqueue(curNode.left)
+ if (curNode.right != null) queue.enqueue(curNode.right)
+ }
+ }
+ res // 最终返回结果,return关键字可以省略
+ }
+```
-----------------------
From 4405be238d0041b3cd2719df2945e00fe114c030 Mon Sep 17 00:00:00 2001
From: SevenMonths
Date: Tue, 24 May 2022 19:56:37 +0800
Subject: [PATCH 67/69] =?UTF-8?q?=E5=A2=9E=E5=8A=A0(0206.=E7=BF=BB?=
=?UTF-8?q?=E8=BD=AC=E9=93=BE=E8=A1=A8.md)=EF=BC=9Aphp=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0206.翻转链表.md | 15 +++++++++++++++
1 file changed, 15 insertions(+)
diff --git a/problems/0206.翻转链表.md b/problems/0206.翻转链表.md
index 941928ba..2e80972c 100644
--- a/problems/0206.翻转链表.md
+++ b/problems/0206.翻转链表.md
@@ -497,5 +497,20 @@ struct ListNode* reverseList(struct ListNode* head){
}
```
+PHP:
+```php
+// 双指针法:
+function reverseList($head) {
+ $cur = $head;
+ $pre = NULL;
+ while($cur){
+ $temp = $cur->next;
+ $cur->next = $pre;
+ $pre = $cur;
+ $cur = $temp;
+ }
+ return $pre;
+}
+```
-----------------------
From f5f5f5a2a530845483708938064c05aae698487c Mon Sep 17 00:00:00 2001
From: programmercarl <826123027@qq.com>
Date: Fri, 10 Jun 2022 11:02:44 +0800
Subject: [PATCH 68/69] Update
---
problems/0027.移除元素.md | 14 +-
problems/0035.搜索插入位置.md | 12 +-
problems/0704.二分查找.md | 2 +
problems/0977.有序数组的平方.md | 2 +
problems/qita/gitserver.md | 312 +++++++++++++++++++
problems/qita/server.md | 126 ++++++++
problems/{其他 => qita}/参与本项目.md | 0
7 files changed, 460 insertions(+), 8 deletions(-)
create mode 100644 problems/qita/gitserver.md
create mode 100644 problems/qita/server.md
rename problems/{其他 => qita}/参与本项目.md (100%)
diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md
index 590cf0b9..72860ffd 100644
--- a/problems/0027.移除元素.md
+++ b/problems/0027.移除元素.md
@@ -28,6 +28,8 @@
## 思路
+[本题B站视频讲解](https://www.bilibili.com/video/BV12A4y1Z7LP)
+
有的同学可能说了,多余的元素,删掉不就得了。
**要知道数组的元素在内存地址中是连续的,不能单独删除数组中的某个元素,只能覆盖。**
@@ -75,10 +77,20 @@ public:
双指针法(快慢指针法): **通过一个快指针和慢指针在一个for循环下完成两个for循环的工作。**
+定义快慢指针
+
+* 快指针:寻找新数组的元素 ,新数组就是不含有目标元素的数组
+* 慢指针:指向更新 新数组下标的位置
+
+很多同学这道题目做的很懵,就是不理解 快慢指针究竟都是什么含义,所以一定要明确含义,后面的思路就更容易理解了。
+
删除过程如下:

+很多同学不了解
+
+
**双指针法(快慢指针法)在数组和链表的操作中是非常常见的,很多考察数组、链表、字符串等操作的面试题,都使用双指针法。**
后序都会一一介绍到,本题代码如下:
@@ -104,8 +116,6 @@ public:
* 时间复杂度:O(n)
* 空间复杂度:O(1)
-旧文链接:[数组:就移除个元素很难么?](https://programmercarl.com/0027.移除元素.html)
-
```CPP
/**
* 相向双指针方法,基于元素顺序可以改变的题目描述改变了元素相对位置,确保了移动最少元素
diff --git a/problems/0035.搜索插入位置.md b/problems/0035.搜索插入位置.md
index 8a8f9706..ff1b0292 100644
--- a/problems/0035.搜索插入位置.md
+++ b/problems/0035.搜索插入位置.md
@@ -73,8 +73,8 @@ public:
};
```
-* 时间复杂度:$O(n)$
-* 空间复杂度:$O(1)$
+* 时间复杂度:O(n)
+* 空间复杂度:O(1)
效率如下:
@@ -135,14 +135,14 @@ public:
// 目标值在数组所有元素之前 [0, -1]
// 目标值等于数组中某一个元素 return middle;
// 目标值插入数组中的位置 [left, right],return right + 1
- // 目标值在数组所有元素之后的情况 [left, right], return right + 1
+ // 目标值在数组所有元素之后的情况 [left, right], 因为是右闭区间,所以 return right + 1
return right + 1;
}
};
```
-* 时间复杂度:$O(\log n)$
-* 时间复杂度:$O(1)$
+* 时间复杂度:O(log n)
+* 时间复杂度:O(1)
效率如下:

@@ -178,7 +178,7 @@ public:
// 目标值在数组所有元素之前 [0,0)
// 目标值等于数组中某一个元素 return middle
// 目标值插入数组中的位置 [left, right) ,return right 即可
- // 目标值在数组所有元素之后的情况 [left, right),return right 即可
+ // 目标值在数组所有元素之后的情况 [left, right),因为是右开区间,所以 return right
return right;
}
};
diff --git a/problems/0704.二分查找.md b/problems/0704.二分查找.md
index 55625130..ce8253af 100644
--- a/problems/0704.二分查找.md
+++ b/problems/0704.二分查找.md
@@ -36,6 +36,8 @@
## 思路
+为了易于大家理解,我还录制了视频,可以看这里:[手把手带你撕出正确的二分法](https://www.bilibili.com/video/BV1fA4y1o715)
+
**这道题目的前提是数组为有序数组**,同时题目还强调**数组中无重复元素**,因为一旦有重复元素,使用二分查找法返回的元素下标可能不是唯一的,这些都是使用二分法的前提条件,当大家看到题目描述满足如上条件的时候,可要想一想是不是可以用二分法了。
二分查找涉及的很多的边界条件,逻辑比较简单,但就是写不好。例如到底是 `while(left < right)` 还是 `while(left <= right)`,到底是`right = middle`呢,还是要`right = middle - 1`呢?
diff --git a/problems/0977.有序数组的平方.md b/problems/0977.有序数组的平方.md
index 24276bcf..8811f3d7 100644
--- a/problems/0977.有序数组的平方.md
+++ b/problems/0977.有序数组的平方.md
@@ -23,6 +23,8 @@
# 思路
+为了易于大家理解,我还特意录制了视频,[本题视频讲解](https://www.bilibili.com/video/BV1QB4y1D7ep)
+
## 暴力排序
最直观的想法,莫过于:每个数平方之后,排个序,美滋滋,代码如下:
diff --git a/problems/qita/gitserver.md b/problems/qita/gitserver.md
new file mode 100644
index 00000000..9ee06ae4
--- /dev/null
+++ b/problems/qita/gitserver.md
@@ -0,0 +1,312 @@
+
+# 一文手把手教你搭建Git私服
+
+## 为什么要搭建Git私服
+
+很多同学都问文章,文档,资料怎么备份啊,自己电脑和公司电脑怎么随时同步资料啊等等,这里呢我写一个搭建自己的git私服的详细教程
+
+为什么要搭建一个Git私服呢,而不是用Github免费的私有仓库,有以下几点:
+* Github 私有仓库真的慢,文件一旦多了,或者有图片文件,git pull 的时候半天拉不下来
+* 自己的文档难免有自己个人信息,放在github心里也是担心的
+* 想建几个库就建几个,想几个人合作开发都可以,不香么?
+
+**网上可以搜到很多git搭建,但是说的模棱两可**,而且有的直接是在本地搭建git服务,既然是备份,搭建在本地哪有备份的意义,一定要有一个远端服务器, 而且自己的电脑和公司的电脑还是同步自己的文章,文档和资料等等。
+
+
+适合人群: 想通过git私服来备份自己的文章,Markdown,并做版本管理的同学
+最后,写好每篇 Chat 是对我的责任,也是对你的尊重。谢谢大家~
+
+正文如下:
+
+-----------------------------
+
+## 如何找到可以外网访问服务器
+
+有的同学问了,自己的电脑就不能作为服务器么?
+
+这里要说一下,安装家庭带宽,运营商默认是不会给我们独立分配公网IP的
+
+一般情况下是一片区域公用一个公网IP池,所以外网是不能访问到在家里我们使用的电脑的
+
+除非我们自己去做映射,这其实非常麻烦而且公网IP池 是不断变化的
+
+辛辛苦苦做了映射,运营商给IP一换,我们的努力就白扯了
+
+那我们如何才能找到一个外网可以访问的服务器呢,此时云计算拯救了我们。
+
+推荐大家选一家云厂商(阿里云,腾讯云,百度云都可以)在上面上买一台云服务器
+
+* [阿里云活动期间服务器购买](https://www.aliyun.com/minisite/goods?taskCode=shareNew2205&recordId=3641992&userCode=roof0wob)
+* [腾讯云活动期间服务器购买](https://curl.qcloud.com/EiaMXllu)
+
+云厂商经常做活动,如果从来没有买过云服务器的账号更便宜,低配一年一百块左右的样子,强烈推荐一起买个三年。
+
+买云服务器的时候推荐直接安装centos系统。
+
+这里要说一下,有了自己的云服务器之后 不仅仅可以用来做git私服
+
+**同时还可以做网站,做程序后台,跑程序,做测试**(这样我们自己的电脑就不会因为自己各种搭建环境下载各种包而搞的的烂糟糟),等等等。
+
+有自己云服务器和一个公网IP真的是一件非常非常幸福的事情,能体验到自己的服务随时可以部署上去提供给所有人使用的喜悦。
+
+外网可以访问的服务器解决了,接下来就要部署git服务了
+
+本文将采用centos系统来部署git私服
+
+## 服务器端安装Git
+
+切换至root账户
+
+```
+su root
+```
+
+看一下服务器有没有安装git,如果出现下面信息就说明是有git的
+```
+[root@instance-5fcyjde7 ~]# git
+usage: git [--version] [--help] [-c name=value]
+ [--exec-path[=]] [--html-path] [--man-path] [--info-path]
+ [-p|--paginate|--no-pager] [--no-replace-objects] [--bare]
+ [--git-dir=] [--work-tree=] [--namespace=]
+ []
+
+The most commonly used git commands are:
+ add Add file contents to the index
+ bisect Find by binary search the change that introduced a bug
+ branch List, create, or delete branches
+ checkout Checkout a branch or paths to the working tree
+ clone Clone a repository into a new directory
+ commit Record changes to the repository
+ diff Show changes between commits, commit and working tree, etc
+ fetch Download objects and refs from another repository
+ grep Print lines matching a pattern
+ init Create an empty Git repository or reinitialize an existing one
+ log Show commit logs
+ merge Join two or more development histories together
+ mv Move or rename a file, a directory, or a symlink
+ pull Fetch from and merge with another repository or a local branch
+ push Update remote refs along with associated objects
+ rebase Forward-port local commits to the updated upstream head
+ reset Reset current HEAD to the specified state
+ rm Remove files from the working tree and from the index
+ show Show various types of objects
+ status Show the working tree status
+ tag Create, list, delete or verify a tag object signed with GPG
+
+'git help -a' and 'git help -g' lists available subcommands and some
+concept guides. See 'git help ' or 'git help '
+to read about a specific subcommand or concept.
+```
+
+如果没有git,就安装一下,yum安装的版本默认是 `1.8.3.1`
+
+```
+yum install git
+```
+
+安装成功之后,看一下自己安装的版本
+
+```
+git --version
+```
+
+## 服务器端设置Git账户
+
+创建一个git的linux账户,这个账户只做git私服的操作,也是为了安全起见
+
+如果不新创建一个linux账户,在自己的常用的linux账户下创建的话,哪天手抖 来一个`rm -rf *` 操作 数据可全没了
+
+**这里linux git账户的密码设置的尽量复杂一些,我这里为了演示,就设置成为'gitpassword'**
+```
+adduser git
+passwd gitpassword
+```
+
+然后就要切换成git账户,进行后面的操作了
+```
+[root@instance-5fcyjde7 ~]# su - git
+```
+
+看一下自己所在的目录,是不是在git目录下面
+
+```
+[git@instance-5fcyjde7 ~]$ pwd
+/home/git
+```
+
+## 服务器端密钥管理
+
+创建`.ssh` 目录,如果`.ssh` 已经存在了,可以忽略这一项
+
+为啥用配置ssh公钥呢,同学们记不记得我急使用github上传上传代码的时候也要把自己的公钥配置上github上
+
+这也是方面每次操作git仓库的时候不用再去输入密码
+
+```
+cd ~/
+mkdir .ssh
+```
+
+进入.ssh 文件下,创建一个 `authorized_keys` 文件,这个文件就是后面就是要放我们客户端的公钥
+
+```
+cd ~/.ssh
+touch authorized_keys
+```
+
+别忘了`authorized_keys`给设置权限,很多同学发现自己不能免密登陆,都是因为忘记了给`authorized_keys` 设置权限
+
+```
+chmod 700 /home/git/.ssh
+chmod 600 /home/git/.ssh/authorized_keys
+```
+
+接下来我们要把客户端的公钥放在git服务器上,我们在回到客户端,创建一个公钥
+
+在我们自己的电脑上,有公钥和私钥 两个文件分别是:`id_rsa` 和 `id_rsa.pub`
+
+如果是`windows`系统公钥私钥的目录在`C:\Users\用户名\.ssh` 下
+
+如果是mac 或者 linux, 公钥和私钥的目录这里 `cd ~/.ssh/`, 如果发现自己的电脑上没有公钥私钥,那就自己创建一个
+
+创建密钥的命令
+
+```
+ssh-keygen -t rsa
+```
+
+创建密钥的过程中,一路点击回车就可以了。不需要填任何东西
+
+把公钥拷贝到git服务器上,将我们刚刚生成的`id_rsa.pub`,拷贝到git服务器的`/home/git/.ssh/`目录
+
+在git服务器上,将公钥添加到`authorized_keys` 文件中
+
+```
+cd /home/git/.ssh/
+cat id_rsa.pub >> authorized_keys
+```
+
+如何看我们配置的密钥是否成功呢, 在客户点直接登录git服务器,看看是否是免密登陆
+```
+ssh git@git服务器ip
+```
+
+例如:
+
+```
+ssh git@127.0.0.1
+```
+
+如果可以免密登录,那就说明服务器端密钥配置成功了
+
+## 服务器端部署Git 仓库
+
+我们在登陆到git 服务器端,切换为成 git账户
+
+如果是root账户切换成git账户
+```
+su - git
+```
+
+如果是其他账户切换为git账户
+```
+sudo su - git
+```
+
+进入git目录下
+```
+cd ~/git
+```
+
+创建我们的第一个Git私服的仓库,我们叫它为world仓库
+
+那么首先创建一个文件夹名为: world.git ,然后进入这个目录
+
+有同学问,为什么文件夹名字后面要放`.git`, 其实不这样命名也是可以的
+
+但是细心的同学可能注意到,我们平时在github上 `git clone` 其他人的仓库的时候,仓库名字后面,都是加上`.git`的
+
+例如下面这个例子,其实就是github对仓库名称的一个命名规则,所以我们也遵守github的命名规则。
+
+```
+git clone https://github.com/youngyangyang04/NoSQLAttack.git
+```
+
+所以我们的操作是
+```
+[git@localhost git]# mkdir world.git
+[git@localhost git]# cd world.git
+```
+
+初始化我们的`world`仓库
+
+```
+git init --bare
+
+```
+
+**如果我们想创建多个仓库,就在这里创建多个文件夹并初始化就可以了,和world仓库的操作过程是一样一样的**
+
+现在我们服务端的git仓库就部署完了,接下来就看看客户端,如何使用这个仓库呢
+
+## 客户端连接远程仓库
+
+我们在自己的电脑上创建一个文件夹 也叫做`world`吧
+
+其实这里命名是随意的,但是我们为了和git服务端的仓库名称保持同步。 这样更直观我们操作的是哪一个仓库。
+
+```
+mkdir world
+cd world
+```
+
+进入world文件,并初始化操作
+
+```
+cd world
+git init
+```
+
+在world目录上创建一个测试文件,并且将其添加到git版本管理中
+
+```
+touch test
+git add test
+git commit -m "add test file"
+```
+
+将次仓库和远端仓库同步
+
+```
+git remote add origin git@git服务器端的ip:world.git
+git push -u origin master
+```
+
+此时这个test测试文件就已经提交到我们的git远端私服上了
+
+## Git私服安全问题
+
+这里有两点安全问题
+
+### linux git的密码不要泄露出去
+
+否则,别人可以通过 ssh git@git服务器IP 来登陆到你的git私服服务器上
+
+当然了,这里同学们如果买的是云厂商的云服务器的话
+
+如果有人恶意想通过 尝试不同密码链接的方式来链接你的服务器,重试三次以上
+
+这个客户端的IP就会被封掉,同时邮件通知我们可以IP来自哪里
+
+所以大可放心 密码只要我们不泄露出去,基本上不会有人同时不断尝试密码的方式来登上我们的git私服服务器
+
+### 私钥文件`id_rsa` 不要给别人
+
+如果有人得到了这个私钥,就可以免密码登陆我们的git私服上了,我相信大家也不至于把自己的私钥主动给别人吧
+
+## 总结
+
+这里就是整个git私服搭建的全过程,安全问题我也给大家列举了出来,接下来好好享受自己的Git私服吧
+
+**enjoy!**
+
diff --git a/problems/qita/server.md b/problems/qita/server.md
new file mode 100644
index 00000000..16995d70
--- /dev/null
+++ b/problems/qita/server.md
@@ -0,0 +1,126 @@
+
+# 一台服务器有什么用!
+
+但在组织这场活动的时候,了解到大家都有一个共同的问题: **这个服务器究竟有啥用??**
+
+这真是一个好问题,而且我一句两句还说不清楚,所以就专门发文来讲一讲。
+
+同时我还录制的一期视频,哈哈我的视频号,大家可以关注一波。
+
+
+一说到服务器,可能很多人都说搞分布式,做计算,搞爬虫,做程序后台服务,多人合作等等。
+
+其实这些普通人都用不上,我来说一说大家能用上的吧。
+
+## 搭建git私服
+
+大家平时工作的时候一定有一个自己的工作文件夹,学生的话就是自己的课件,考试,准备面试的资料等等。
+
+已经工作的录友,会有一个文件夹放着自己重要的文档,Markdown,图片,简历等等。
+
+这么重要的文件夹,而且我们每天都要更新,也担心哪天电脑丢了,或者坏了,突然这些都不见了。
+
+所以我们想备份嘛。
+
+还有就是我们经常个人电脑和工作电脑要同步一些私人资料,而不是用微信传来传去。
+
+这些都是git私服的使用场景,而且很好用。
+
+大家也知道 github,gitee也可以搞私人仓库 用来备份,同步文件,但自己的文档可能放着很多重要的信息,包括自己的各种密码,密钥之类的,放到上面未必安全。你就不怕哪些重大bug把你的信息都泄漏了么[机智]
+
+更关键的是,github 和 gitee都限速的。毕竟人家的功能定位并不是网盘。
+
+项目里有大文件(几百M以上),例如pdf,ppt等等 其上传和下载速度会让你窒息。
+
+**后面我会发文专门来讲一讲,如何大家git私服!**
+
+## 搞一个文件存储
+
+这个可以用来生成文件的下载链接,也可以把本地文件传到服务器上。
+
+相当于自己做一个对象存储,其实云厂商也有对象存储的产品。
+
+不过我们自己也可以做一个,不够很多很同学应该都不知道对象存储怎么用吧,其实我们用服务器可以自己做一个类似的公司。
+
+我现在就用自己用go写的一个工具,部署在服务器上。 用来和服务器传文件,或者生成一些文件的临时下载链接。
+
+这些都是直接命令行操作的,
+
+操作方式这样,我把命令包 包装成一个shell命令,想传那个文件,直接 uploadtomyserver,然后就返回可以下载的链接,这个文件也同时传到了我的服务器上。
+
+
+
+我也把我的项目代码放在了github上:
+
+https://github.com/youngyangyang04/fileHttpServer
+
+感兴趣的录友可以去学习一波,顺便给个star 哈哈
+
+
+## 网站
+
+做网站,例如 大家知道用html 写几行代码,就可以生成一个网页,但怎么给别人展示呢?
+
+大家如果用自己的电脑做服务器,只能同一个路由器下的设备可以访问你的网站,可能这个设备出了这个屋子 都访问不了你的网站了。
+
+因为你的IP不是公网IP。
+
+如果有了一台云服务器,都是配公网IP,你的网站就可以让任何人访问了。
+
+或者说 你提供的一个服务就可以让任何人使用。
+
+例如第二个例子中,我们可以自己开发一个文件存储,这个服务,我只把把命令行给其他人,其他人都可以使用我的服务来生成链接,当然他们的文件也都传到了我的服务器上。
+
+再说一个使用场景。
+
+我之前在组织免费里服务器的活动的时候,阿里云给我一个excel,让面就是从我这里买服务器录友的名单,我直接把这个名单甩到群里,让大家自己检查,出现在名单里就可以找我返现,这样做是不是也可以。
+
+这么做有几个很大的问题:
+* 大家都要去下载excel,做对比,会有人改excel的内容然后就说是从你这里买的,我不可能挨个去比较excel有没有改动
+* excel有其他人的个人信息,这是不能暴漏的。
+* 如果每个人自己用excel查询,私信我返现,一个将近两千人找我返现,我微信根本处理不过来,这就变成体力活了。
+
+那应该怎么做呢,
+
+我就简单写一个查询的页面,后端逻辑就是读一个execel表格,大家在查询页面输入自己的阿里云ID,如果在excel里,页面就会返回返现群的二维码,大家就可以自主扫码加群了。
+
+这样,我最后就直接在返现群里 发等额红包就好了,是不是极大降低人力成本了
+
+当然我是把 17个返现群的二维码都生成好了,按照一定的规则,展现给查询通过的录友。
+
+就是这样一个非常普通的查询页面。
+
+
+
+查询通过之后,就会展现返现群二维码。
+
+
+
+但要部署在服务器上,因为没有公网IP,别人用不了你的服务。
+
+
+## 学习linux
+
+学习linux其实在自己的电脑上搞一台虚拟机,或者安装双系统也可以学习,不过这很考验你的电脑性能如何了。
+
+如果你有一个服务器,那就是独立的一台电脑,你怎么霍霍就怎么霍霍,而且一年都不用关机的,可以一直跑你的任务,和你本地电脑也完全隔离。
+
+更方便的是,你目前系统假如是centos,想做一个实验需要在unbantu上,如果是云服务器,更换系统就是在 后台点一下,一键重装,云厂商基本都是支持所有系统一件安装的。
+
+我们平时自己玩linux经常是配各种环境,然后这个linux就被自己玩坏了(一般都是毫无节制使用root权限导致的),总之就是环境配不起来了,基本就要重装了。
+
+那云服务器重装系统可太方便了。
+
+还有就是加入你好不容易配好的环境,如果以后把这个环境玩坏了,你先回退这之前配好的环境而不是重装系统在重新配一遍吧。
+
+那么可以用云服务器的镜像保存功能,就是你配好环境的那一刻就可以打一个镜像包,以后如果环境坏了,直接回退到上次镜像包的状态,这是不是就很香了。
+
+
+## 总结
+
+其实云服务器还有很多其他用处,不过我就说一说大家普遍能用的上的。
+
+
+* [阿里云活动期间服务器购买](https://www.aliyun.com/minisite/goods?taskCode=shareNew2205&recordId=3641992&userCode=roof0wob)
+* [腾讯云活动期间服务器购买](https://curl.qcloud.com/EiaMXllu)
+
diff --git a/problems/其他/参与本项目.md b/problems/qita/参与本项目.md
similarity index 100%
rename from problems/其他/参与本项目.md
rename to problems/qita/参与本项目.md
From 2c1aa6ebf83cca21a8c6536922f2d652fa45c5cc Mon Sep 17 00:00:00 2001
From: programmercarl <826123027@qq.com>
Date: Mon, 13 Jun 2022 17:23:30 +0800
Subject: [PATCH 69/69] Update
---
problems/0202.快乐数.md | 1 +
problems/0209.长度最小的子数组.md | 29 ++++++++++++++++++-----
problems/0349.两个数组的交集.md | 2 ++
problems/0383.赎金信.md | 1 +
problems/0454.四数相加II.md | 1 +
problems/qita/server.md | 3 +++
6 files changed, 31 insertions(+), 6 deletions(-)
diff --git a/problems/0202.快乐数.md b/problems/0202.快乐数.md
index be8686f7..7738b2f6 100644
--- a/problems/0202.快乐数.md
+++ b/problems/0202.快乐数.md
@@ -417,6 +417,7 @@ object Solution {
}
sum
}
+```
C#:
diff --git a/problems/0209.长度最小的子数组.md b/problems/0209.长度最小的子数组.md
index fbef7692..69e0da4f 100644
--- a/problems/0209.长度最小的子数组.md
+++ b/problems/0209.长度最小的子数组.md
@@ -5,7 +5,7 @@
参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
-## 209.长度最小的子数组
+# 209.长度最小的子数组
[力扣题目链接](https://leetcode-cn.com/problems/minimum-size-subarray-sum/)
@@ -17,6 +17,9 @@
输出:2
解释:子数组 [4,3] 是该条件下的长度最小的子数组。
+# 思路
+
+为了易于大家理解,我特意录制了[拿下滑动窗口! | LeetCode 209 长度最小的子数组](https://www.bilibili.com/video/BV1tZ4y1q7XE)
## 暴力解法
@@ -47,8 +50,8 @@ public:
}
};
```
-时间复杂度:O(n^2)
-空间复杂度:O(1)
+* 时间复杂度:O(n^2)
+* 空间复杂度:O(1)
## 滑动窗口
@@ -56,6 +59,20 @@ public:
所谓滑动窗口,**就是不断的调节子序列的起始位置和终止位置,从而得出我们要想的结果**。
+在暴力解法中,是一个for循环滑动窗口的起始位置,一个for循环为滑动窗口的终止位置,用两个for循环 完成了一个不断搜索区间的过程。
+
+那么滑动窗口如何用一个for循环来完成这个操作呢。
+
+首先要思考 如果用一个for循环,那么应该表示 滑动窗口的起始位置,还是终止位置。
+
+如果只用一个for循环来表示 滑动窗口的起始位置,那么如何遍历剩下的终止位置?
+
+此时难免再次陷入 暴力解法的怪圈。
+
+所以 只用一个for循环,那么这个循环的索引,一定是表示 滑动窗口的终止位置。
+
+那么问题来了, 滑动窗口的起始位置如何移动呢?
+
这里还是以题目中的示例来举例,s=7, 数组是 2,3,1,2,4,3,来看一下查找的过程:

@@ -74,7 +91,7 @@ public:
窗口的起始位置如何移动:如果当前窗口的值大于s了,窗口就要向前移动了(也就是该缩小了)。
-窗口的结束位置如何移动:窗口的结束位置就是遍历数组的指针,窗口的起始位置设置为数组的起始位置就可以了。
+窗口的结束位置如何移动:窗口的结束位置就是遍历数组的指针,也就是for循环里的索引。
解题的关键在于 窗口的起始位置如何移动,如图所示:
@@ -107,8 +124,8 @@ public:
};
```
-时间复杂度:O(n)
-空间复杂度:O(1)
+* 时间复杂度:O(n)
+* 空间复杂度:O(1)
**一些录友会疑惑为什么时间复杂度是O(n)**。
diff --git a/problems/0349.两个数组的交集.md b/problems/0349.两个数组的交集.md
index f7dab3d7..4fbdd414 100644
--- a/problems/0349.两个数组的交集.md
+++ b/problems/0349.两个数组的交集.md
@@ -356,6 +356,8 @@ object Solution {
}
}
+```
+
C#:
```csharp
diff --git a/problems/0383.赎金信.md b/problems/0383.赎金信.md
index 75dafb72..9c3dda8c 100644
--- a/problems/0383.赎金信.md
+++ b/problems/0383.赎金信.md
@@ -425,6 +425,7 @@ object Solution {
true
}
}
+```
C#:
diff --git a/problems/0454.四数相加II.md b/problems/0454.四数相加II.md
index bfdee26e..726fdb15 100644
--- a/problems/0454.四数相加II.md
+++ b/problems/0454.四数相加II.md
@@ -354,6 +354,7 @@ object Solution {
res
}
}
+```
C#:
```csharp
diff --git a/problems/qita/server.md b/problems/qita/server.md
index 16995d70..1d7a1d6b 100644
--- a/problems/qita/server.md
+++ b/problems/qita/server.md
@@ -1,6 +1,9 @@
# 一台服务器有什么用!
+* [阿里云活动期间服务器购买](https://www.aliyun.com/minisite/goods?taskCode=shareNew2205&recordId=3641992&userCode=roof0wob)
+* [腾讯云活动期间服务器购买](https://curl.qcloud.com/EiaMXllu)
+
但在组织这场活动的时候,了解到大家都有一个共同的问题: **这个服务器究竟有啥用??**
这真是一个好问题,而且我一句两句还说不清楚,所以就专门发文来讲一讲。