From 2137778d44d256795bd0ca6c9b880b2c3be180d1 Mon Sep 17 00:00:00 2001
From: damon <245211612@qq.com>
Date: Thu, 26 May 2022 00:29:02 +0800
Subject: [PATCH 01/22] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880015.=E4=B8=89?=
=?UTF-8?q?=E6=95=B0=E4=B9=8B=E5=92=8C.md=EF=BC=89=EF=BC=9A=E5=A2=9E?=
=?UTF-8?q?=E5=8A=A0javascript=E7=89=88=E6=9C=ACnsum=E7=9A=84=E9=80=9A?=
=?UTF-8?q?=E7=94=A8=E8=A7=A3=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0015.三数之和.md | 70 +++++++++++++++++++++++++++++++++++
1 file changed, 70 insertions(+)
diff --git a/problems/0015.三数之和.md b/problems/0015.三数之和.md
index cc184c87..12d83d8f 100644
--- a/problems/0015.三数之和.md
+++ b/problems/0015.三数之和.md
@@ -345,6 +345,76 @@ var threeSum = function(nums) {
return res
};
```
+
+解法二:nSum通用解法。递归
+
+```js
+/**
+ * nsum通用解法,支持2sum,3sum,4sum...等等
+ * 时间复杂度分析:
+ * 1. n = 2时,时间复杂度O(NlogN),排序所消耗的时间。、
+ * 2. n > 2时,时间复杂度为O(N^n-1),即N的n-1次方,至少是2次方,此时可省略排序所消耗的时间。举例:3sum为O(n^2),4sum为O(n^3)
+ * @param {number[]} nums
+ * @return {number[][]}
+ */
+var threeSum = function (nums) {
+ // nsum通用解法核心方法
+ function nSumTarget(nums, n, start, target) {
+ // 前提:nums要先排序好
+ let res = [];
+ if (n === 2) {
+ res = towSumTarget(nums, start, target);
+ } else {
+ for (let i = start; i < nums.length; i++) {
+ // 递归求(n - 1)sum
+ let subRes = nSumTarget(
+ nums,
+ n - 1,
+ i + 1,
+ target - nums[i]
+ );
+ for (let j = 0; j < subRes.length; j++) {
+ res.push([nums[i], ...subRes[j]]);
+ }
+ // 跳过相同元素
+ while (nums[i] === nums[i + 1]) i++;
+ }
+ }
+ return res;
+ }
+
+ function towSumTarget(nums, start, target) {
+ // 前提:nums要先排序好
+ let res = [];
+ let len = nums.length;
+ let left = start;
+ let right = len - 1;
+ while (left < right) {
+ let sum = nums[left] + nums[right];
+ if (sum < target) {
+ while (nums[left] === nums[left + 1]) left++;
+ left++;
+ } else if (sum > target) {
+ while (nums[right] === nums[right - 1]) right--;
+ right--;
+ } else {
+ // 相等
+ res.push([nums[left], nums[right]]);
+ // 跳过相同元素
+ while (nums[left] === nums[left + 1]) left++;
+ while (nums[right] === nums[right - 1]) right--;
+ left++;
+ right--;
+ }
+ }
+ return res;
+ }
+ nums.sort((a, b) => a - b);
+ // n = 3,此时求3sum之和
+ return nSumTarget(nums, 3, 0, 0);
+};
+```
+
TypeScript:
```typescript
From c70440ac4318e48cfd3500ec290678f8976bb008 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Thu, 26 May 2022 10:54:39 +0800
Subject: [PATCH 02/22] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880283.=E7=A7=BB?=
=?UTF-8?q?=E5=8A=A8=E9=9B=B6.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/0283.移动零.md | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)
diff --git a/problems/0283.移动零.md b/problems/0283.移动零.md
index ed59d2c4..60eea378 100644
--- a/problems/0283.移动零.md
+++ b/problems/0283.移动零.md
@@ -133,6 +133,27 @@ var moveZeroes = function(nums) {
};
```
+TypeScript:
+
+```typescript
+function moveZeroes(nums: number[]): void {
+ const length: number = nums.length;
+ let slowIndex: number = 0,
+ fastIndex: number = 0;
+ while (fastIndex < length) {
+ if (nums[fastIndex] !== 0) {
+ nums[slowIndex++] = nums[fastIndex];
+ };
+ fastIndex++;
+ }
+ while (slowIndex < length) {
+ nums[slowIndex++] = 0;
+ }
+};
+```
+
+
+
-----------------------
From 36a1d71201a59a113a339a1555e2734b7e0cc764 Mon Sep 17 00:00:00 2001
From: Harrytsz <18810271846@163.com>
Date: Thu, 26 May 2022 16:35:42 +0800
Subject: [PATCH 03/22] =?UTF-8?q?Update=200541.=E5=8F=8D=E8=BD=AC=E5=AD=97?=
=?UTF-8?q?=E7=AC=A6=E4=B8=B2II.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
去掉 continue,增加 else 分支,逻辑更加清晰
---
problems/0541.反转字符串II.md | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/problems/0541.反转字符串II.md b/problems/0541.反转字符串II.md
index 8c13a390..38dc6853 100644
--- a/problems/0541.反转字符串II.md
+++ b/problems/0541.反转字符串II.md
@@ -53,10 +53,10 @@ public:
// 2. 剩余字符小于 2k 但大于或等于 k 个,则反转前 k 个字符
if (i + k <= s.size()) {
reverse(s.begin() + i, s.begin() + i + k );
- continue;
+ } else {
+ // 3. 剩余字符少于 k 个,则将剩余字符全部反转。
+ reverse(s.begin() + i, s.end());
}
- // 3. 剩余字符少于 k 个,则将剩余字符全部反转。
- reverse(s.begin() + i, s.begin() + s.size());
}
return s;
}
From 8d3e5b46083fe83210b683aec67e471a4a00225e Mon Sep 17 00:00:00 2001
From: SevenMonths
Date: Thu, 26 May 2022 13:09:05 +0800
Subject: [PATCH 04/22] =?UTF-8?q?=E6=B7=BB=E5=8A=A0(0232.=E7=94=A8?=
=?UTF-8?q?=E6=A0=88=E5=AE=9E=E7=8E=B0=E9=98=9F=E5=88=97.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/0232.用栈实现队列.md | 44 +++++++++++++++++++++++++++++
1 file changed, 44 insertions(+)
diff --git a/problems/0232.用栈实现队列.md b/problems/0232.用栈实现队列.md
index 1a56d9f3..6752651e 100644
--- a/problems/0232.用栈实现队列.md
+++ b/problems/0232.用栈实现队列.md
@@ -496,5 +496,49 @@ void myQueueFree(MyQueue* obj) {
}
```
+PHP:
+```php
+// SplStack 类通过使用一个双向链表来提供栈的主要功能。[PHP 5 >= 5.3.0, PHP 7, PHP 8]
+// https://www.php.net/manual/zh/class.splstack.php
+class MyQueue {
+ // 双栈模拟队列:In栈存储数据;Out栈辅助处理
+ private $stackIn;
+ private $stackOut;
+
+ function __construct() {
+ $this->stackIn = new SplStack();
+ $this->stackOut = new SplStack();
+ }
+
+ // In: 1 2 3 <= push
+ function push($x) {
+ $this->stackIn->push($x);
+ }
+
+ function pop() {
+ $this->peek();
+ return $this->stackOut->pop();
+ }
+
+ function peek() {
+ if($this->stackOut->isEmpty()){
+ $this->shift();
+ }
+ return $this->stackOut->top();
+ }
+
+ function empty() {
+ return $this->stackOut->isEmpty() && $this->stackIn->isEmpty();
+ }
+
+ // 如果Out栈为空,把In栈数据压入Out栈
+ // In: 1 2 3 => pop push => 1 2 3 :Out
+ private function shift(){
+ while(!$this->stackIn->isEmpty()){
+ $this->stackOut->push($this->stackIn->pop());
+ }
+ }
+}
+```
-----------------------
From 5baecc0f7919912e10ea64651ddc880f8c61f3c9 Mon Sep 17 00:00:00 2001
From: SevenMonths
Date: Fri, 27 May 2022 00:59:37 +0800
Subject: [PATCH 05/22] =?UTF-8?q?=E6=B7=BB=E5=8A=A0(0225.=E7=94=A8?=
=?UTF-8?q?=E9=98=9F=E5=88=97=E5=AE=9E=E7=8E=B0=E6=A0=88.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/0225.用队列实现栈.md | 78 +++++++++++++++++++++++++++++
1 file changed, 78 insertions(+)
diff --git a/problems/0225.用队列实现栈.md b/problems/0225.用队列实现栈.md
index 3457c4b3..f946cd86 100644
--- a/problems/0225.用队列实现栈.md
+++ b/problems/0225.用队列实现栈.md
@@ -816,5 +816,83 @@ class MyStack {
}
```
+PHP
+> 双对列
+```php
+// SplQueue 类通过使用一个双向链表来提供队列的主要功能。(PHP 5 >= 5.3.0, PHP 7, PHP 8)
+// https://www.php.net/manual/zh/class.splqueue.php
+class MyStack {
+ public $queueMain; // 保存数据
+ public $queueTmp; // 辅助作用
+
+ function __construct() {
+ $this->queueMain=new SplQueue();
+ $this->queueTmp=new SplQueue();
+ }
+
+ // queueMain: 1,2,3 <= add
+ function push($x) {
+ $this->queueMain->enqueue($x);
+ }
+
+ function pop() {
+ $qmSize = $this->queueMain->Count();
+ $qmSize --;
+ // queueMain: 3,2,1 => pop =>2,1 => add => 2,1 :queueTmp
+ while($qmSize --){
+ $this->queueTmp->enqueue($this->queueMain->dequeue());
+ }
+ // queueMain: 3
+ $val = $this->queueMain->dequeue();
+ // queueMain <= queueTmp
+ $this->queueMain = $this->queueTmp;
+ // 清空queueTmp,下次使用
+ $this->queueTmp = new SplQueue();
+ return $val;
+ }
+
+ function top() {
+ // 底层是双链表实现:从双链表的末尾查看节点
+ return $this->queueMain->top();
+ }
+
+ function empty() {
+ return $this->queueMain->isEmpty();
+ }
+}
+```
+> 单对列
+```php
+class MyStack {
+ public $queue;
+
+ function __construct() {
+ $this->queue=new SplQueue();
+ }
+
+ function push($x) {
+ $this->queue->enqueue($x);
+ }
+
+ function pop() {
+ $qmSize = $this->queue->Count();
+ $qmSize --;
+ //queue: 3,2,1 => pop =>2,1 => add => 2,1,3 :queue
+ while($qmSize --){
+ $this->queue->enqueue($this->queue->dequeue());
+ }
+ $val = $this->queue->dequeue();
+ return $val;
+ }
+
+ function top() {
+ return $this->queue->top();
+ }
+
+ function empty() {
+ return $this->queue->isEmpty();
+ }
+}
+```
-----------------------
From 513ec5418910aee18a1b6722b9316ffaaeb2dec4 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Fri, 27 May 2022 12:18:24 +0800
Subject: [PATCH 06/22] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880189.=E6=97=8B?=
=?UTF-8?q?=E8=BD=AC=E6=95=B0=E7=BB=84.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/0189.旋转数组.md | 23 +++++++++++++++++++++++
1 file changed, 23 insertions(+)
diff --git a/problems/0189.旋转数组.md b/problems/0189.旋转数组.md
index 3ffed877..23092f9c 100644
--- a/problems/0189.旋转数组.md
+++ b/problems/0189.旋转数组.md
@@ -7,6 +7,8 @@
# 189. 旋转数组
+[力扣题目链接](https://leetcode.cn/problems/rotate-array/)
+
给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。
进阶:
@@ -160,6 +162,27 @@ var rotate = function (nums, k) {
};
```
+## TypeScript
+
+```typescript
+function rotate(nums: number[], k: number): void {
+ const length: number = nums.length;
+ k %= length;
+ reverseByRange(nums, 0, length - 1);
+ reverseByRange(nums, 0, k - 1);
+ reverseByRange(nums, k, length - 1);
+};
+function reverseByRange(nums: number[], left: number, right: number): void {
+ while (left < right) {
+ const temp = nums[left];
+ nums[left] = nums[right];
+ nums[right] = temp;
+ left++;
+ right--;
+ }
+}
+```
+
-----------------------
From 156d8fdd92b07c6ba74ed68ee9e8cd092c106024 Mon Sep 17 00:00:00 2001
From: SevenMonths
Date: Fri, 27 May 2022 14:21:41 +0800
Subject: [PATCH 07/22] =?UTF-8?q?=E5=A2=9E=E5=8A=A0(0020.=E6=9C=89?=
=?UTF-8?q?=E6=95=88=E7=9A=84=E6=8B=AC=E5=8F=B7.md)=EF=BC=9APHP=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/0020.有效的括号.md | 28 ++++++++++++++++++++++++++++
1 file changed, 28 insertions(+)
diff --git a/problems/0020.有效的括号.md b/problems/0020.有效的括号.md
index 7bb7f746..5e468549 100644
--- a/problems/0020.有效的括号.md
+++ b/problems/0020.有效的括号.md
@@ -401,5 +401,33 @@ bool isValid(char * s){
}
```
+PHP:
+```php
+// https://www.php.net/manual/zh/class.splstack.php
+class Solution
+{
+ function isValid($s){
+ $stack = new SplStack();
+ for ($i = 0; $i < strlen($s); $i++) {
+ if ($s[$i] == "(") {
+ $stack->push(')');
+ } else if ($s[$i] == "{") {
+ $stack->push('}');
+ } else if ($s[$i] == "[") {
+ $stack->push(']');
+ // 2、遍历匹配过程中,发现栈内没有要匹配的字符 return false
+ // 3、遍历匹配过程中,栈已为空,没有匹配的字符了,说明右括号没有找到对应的左括号 return false
+ } else if ($stack->isEmpty() || $stack->top() != $s[$i]) {
+ return false;
+ } else {//$stack->top() == $s[$i]
+ $stack->pop();
+ }
+ }
+ // 1、遍历完,但是栈不为空,说明有相应的括号没有被匹配,return false
+ return $stack->isEmpty();
+ }
+}
+```
+
-----------------------
From edbff2f4f4746b09afa2d6dd8981f000d0bc75ae Mon Sep 17 00:00:00 2001
From: SevenMonths
Date: Fri, 27 May 2022 15:04:23 +0800
Subject: [PATCH 08/22] =?UTF-8?q?=E6=B7=BB=E5=8A=A0(1047.=E5=88=A0?=
=?UTF-8?q?=E9=99=A4=E5=AD=97=E7=AC=A6=E4=B8=B2=E4=B8=AD=E7=9A=84=E6=89=80?=
=?UTF-8?q?=E6=9C=89=E7=9B=B8=E9=82=BB=E9=87=8D=E5=A4=8D=E9=A1=B9.md)?=
=?UTF-8?q?=EF=BC=9Aphp=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...除字符串中的所有相邻重复项.md | 25 +++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git a/problems/1047.删除字符串中的所有相邻重复项.md b/problems/1047.删除字符串中的所有相邻重复项.md
index 638c8f4e..65428394 100644
--- a/problems/1047.删除字符串中的所有相邻重复项.md
+++ b/problems/1047.删除字符串中的所有相邻重复项.md
@@ -375,5 +375,30 @@ func removeDuplicates(_ s: String) -> String {
}
```
+PHP:
+```php
+class Solution {
+ function removeDuplicates($s) {
+ $stack = new SplStack();
+ for($i=0;$iisEmpty() || $s[$i] != $stack->top()){
+ $stack->push($s[$i]);
+ }else{
+ $stack->pop();
+ }
+ }
+
+ $result = "";
+ while(!$stack->isEmpty()){
+ $result.= $stack->top();
+ $stack->pop();
+ }
+
+ // 此时字符串需要反转一下
+ return strrev($result);
+ }
+}
+```
+
-----------------------
From 821ca656e14384fd63e937a0cd99fbe7bde813f0 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Fri, 27 May 2022 16:10:35 +0800
Subject: [PATCH 09/22] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200654.=E6=9C=80?=
=?UTF-8?q?=E5=A4=A7=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/0654.最大二叉树.md | 26 ++++++++++++++++++++++++++
1 file changed, 26 insertions(+)
diff --git a/problems/0654.最大二叉树.md b/problems/0654.最大二叉树.md
index 1c73354b..1b0eac66 100644
--- a/problems/0654.最大二叉树.md
+++ b/problems/0654.最大二叉树.md
@@ -476,7 +476,33 @@ func traversal(_ nums: inout [Int], _ left: Int, _ right: Int) -> TreeNode? {
}
```
+## Scala
+```scala
+object Solution {
+ def constructMaximumBinaryTree(nums: Array[Int]): TreeNode = {
+ if (nums.size == 0) return null
+ // 找到数组最大值
+ var maxIndex = 0
+ var maxValue = Int.MinValue
+ for (i <- nums.indices) {
+ if (nums(i) > maxValue) {
+ maxIndex = i
+ maxValue = nums(i)
+ }
+ }
+
+ // 构建一棵树
+ var root = new TreeNode(maxValue, null, null)
+
+ // 递归寻找左右子树
+ root.left = constructMaximumBinaryTree(nums.slice(0, maxIndex))
+ root.right = constructMaximumBinaryTree(nums.slice(maxIndex + 1, nums.length))
+
+ root // 返回root
+ }
+}
+```
-----------------------
From de34170f5cc1526eb0c4b1860943a57e6f41c4e1 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Fri, 27 May 2022 16:37:53 +0800
Subject: [PATCH 10/22] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200617.=E5=90=88?=
=?UTF-8?q?=E5=B9=B6=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/0617.合并二叉树.md | 53 ++++++++++++++++++++++++++++++++
1 file changed, 53 insertions(+)
diff --git a/problems/0617.合并二叉树.md b/problems/0617.合并二叉树.md
index 55786ea9..d8bdc91c 100644
--- a/problems/0617.合并二叉树.md
+++ b/problems/0617.合并二叉树.md
@@ -631,7 +631,60 @@ function mergeTrees(root1: TreeNode | null, root2: TreeNode | null): TreeNode |
};
```
+## Scala
+递归:
+```scala
+object Solution {
+ def mergeTrees(root1: TreeNode, root2: TreeNode): TreeNode = {
+ if (root1 == null) return root2 // 如果root1为空,返回root2
+ if (root2 == null) return root1 // 如果root2为空,返回root1
+ // 新建一个节点,值为两个节点的和
+ var node = new TreeNode(root1.value + root2.value)
+ // 往下递归
+ node.left = mergeTrees(root1.left, root2.left)
+ node.right = mergeTrees(root1.right, root2.right)
+ node // 返回node,return关键字可以省略
+ }
+}
+```
+
+迭代:
+```scala
+object Solution {
+ import scala.collection.mutable
+ def mergeTrees(root1: TreeNode, root2: TreeNode): TreeNode = {
+ if (root1 == null) return root2
+ if (root2 == null) return root1
+ var stack = mutable.Stack[TreeNode]()
+ // 先放node2再放node1
+ stack.push(root2)
+ stack.push(root1)
+ while (!stack.isEmpty) {
+ var node1 = stack.pop()
+ var node2 = stack.pop()
+ node1.value += node2.value
+ if (node1.right != null && node2.right != null) {
+ stack.push(node2.right)
+ stack.push(node1.right)
+ } else {
+ if(node1.right == null){
+ node1.right = node2.right
+ }
+ }
+ if (node1.left != null && node2.left != null) {
+ stack.push(node2.left)
+ stack.push(node1.left)
+ } else {
+ if(node1.left == null){
+ node1.left = node2.left
+ }
+ }
+ }
+ root1
+ }
+}
+```
-----------------------
From 9412e2e40529db526bbb16de1f3a9c45cf2c3949 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Fri, 27 May 2022 17:01:56 +0800
Subject: [PATCH 11/22] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200700.=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E6=90=9C?=
=?UTF-8?q?=E7=B4=A2.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0700.二叉搜索树中的搜索.md | 27 ++++++++++++++++++++
1 file changed, 27 insertions(+)
diff --git a/problems/0700.二叉搜索树中的搜索.md b/problems/0700.二叉搜索树中的搜索.md
index 40cf4ea1..16ddef9b 100644
--- a/problems/0700.二叉搜索树中的搜索.md
+++ b/problems/0700.二叉搜索树中的搜索.md
@@ -363,7 +363,34 @@ function searchBST(root: TreeNode | null, val: number): TreeNode | null {
};
```
+## Scala
+递归:
+```scala
+object Solution {
+ def searchBST(root: TreeNode, value: Int): TreeNode = {
+ if (root == null || value == root.value) return root
+ // 相当于三元表达式,在Scala中if...else有返回值
+ if (value < root.value) searchBST(root.left, value) else searchBST(root.right, value)
+ }
+}
+```
+
+迭代:
+```scala
+object Solution {
+ def searchBST(root: TreeNode, value: Int): TreeNode = {
+ // 因为root是不可变量,所以需要赋值给一个可变量
+ var node = root
+ while (node != null) {
+ if (value < node.value) node = node.left
+ else if (value > node.value) node = node.right
+ else return node
+ }
+ null // 没有返回就返回空
+ }
+}
+```
-----------------------
From d4a4eda4b8396603a70871ff215b48b2d17eef93 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Fri, 27 May 2022 17:29:44 +0800
Subject: [PATCH 12/22] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200098.=E9=AA=8C?=
=?UTF-8?q?=E8=AF=81=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91.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/0098.验证二叉搜索树.md | 43 ++++++++++++++++++++++++++
1 file changed, 43 insertions(+)
diff --git a/problems/0098.验证二叉搜索树.md b/problems/0098.验证二叉搜索树.md
index a8f3c324..06f1b8d1 100644
--- a/problems/0098.验证二叉搜索树.md
+++ b/problems/0098.验证二叉搜索树.md
@@ -589,7 +589,50 @@ function isValidBST(root: TreeNode | null): boolean {
};
```
+## Scala
+辅助数组解决:
+```scala
+object Solution {
+ import scala.collection.mutable
+ def isValidBST(root: TreeNode): Boolean = {
+ var arr = new mutable.ArrayBuffer[Int]()
+ // 递归中序遍历二叉树,将节点添加到arr
+ def traversal(node: TreeNode): Unit = {
+ if (node == null) return
+ traversal(node.left)
+ arr.append(node.value)
+ traversal(node.right)
+ }
+ traversal(root)
+ // 这个数组如果是升序就代表是二叉搜索树
+ for (i <- 1 until arr.size) {
+ if (arr(i) <= arr(i - 1)) return false
+ }
+ true
+ }
+}
+```
+
+递归中解决:
+```scala
+object Solution {
+ def isValidBST(root: TreeNode): Boolean = {
+ var flag = true
+ var preValue:Long = Long.MinValue // 这里要使用Long类型
+
+ def traversal(node: TreeNode): Unit = {
+ if (node == null || flag == false) return
+ traversal(node.left)
+ if (node.value > preValue) preValue = node.value
+ else flag = false
+ traversal(node.right)
+ }
+ traversal(root)
+ flag
+ }
+}
+```
-----------------------
From 4e95d663a047d1745c57b3f46cba055836e8ec13 Mon Sep 17 00:00:00 2001
From: damon <245211612@qq.com>
Date: Fri, 27 May 2022 22:52:57 +0800
Subject: [PATCH 13/22] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880509.=E6=96=90?=
=?UTF-8?q?=E6=B3=A2=E9=82=A3=E5=A5=91=E6=95=B0.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0javascript=E7=89=88=E6=9C=AC=E4=BC=98?=
=?UTF-8?q?=E5=8C=96=E7=A9=BA=E9=97=B4=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=B0?=
=?UTF-8?q?O(1)=E8=A7=A3=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0509.斐波那契数.md | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/problems/0509.斐波那契数.md b/problems/0509.斐波那契数.md
index 1d17784d..7c899195 100644
--- a/problems/0509.斐波那契数.md
+++ b/problems/0509.斐波那契数.md
@@ -234,6 +234,7 @@ func fib(n int) int {
}
```
### Javascript
+解法一
```Javascript
var fib = function(n) {
let dp = [0, 1]
@@ -244,6 +245,23 @@ var fib = function(n) {
return dp[n]
};
```
+解法二:时间复杂度O(N),空间复杂度O(1)
+```Javascript
+var fib = function(n) {
+ // 动规状态转移中,当前结果只依赖前两个元素的结果,所以只要两个变量代替dp数组记录状态过程。将空间复杂度降到O(1)
+ let pre1 = 1
+ let pre2 = 0
+ let temp
+ if (n === 0) return 0
+ if (n === 1) return 1
+ for(let i = 2; i <= n; i++) {
+ temp = pre1
+ pre1 = pre1 + pre2
+ pre2 = temp
+ }
+ return pre1
+};
+```
TypeScript
From 992909385fd8bc0181a76bf3fa65eb10f9885190 Mon Sep 17 00:00:00 2001
From: damon <245211612@qq.com>
Date: Fri, 27 May 2022 22:52:57 +0800
Subject: [PATCH 14/22] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880509.=E6=96=90?=
=?UTF-8?q?=E6=B3=A2=E9=82=A3=E5=A5=91=E6=95=B0.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0javascript=E7=89=88=E6=9C=AC=E4=BC=98?=
=?UTF-8?q?=E5=8C=96=E7=A9=BA=E9=97=B4=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=B0?=
=?UTF-8?q?O(1)=E8=A7=A3=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0509.斐波那契数.md | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/problems/0509.斐波那契数.md b/problems/0509.斐波那契数.md
index 1d17784d..7c899195 100644
--- a/problems/0509.斐波那契数.md
+++ b/problems/0509.斐波那契数.md
@@ -234,6 +234,7 @@ func fib(n int) int {
}
```
### Javascript
+解法一
```Javascript
var fib = function(n) {
let dp = [0, 1]
@@ -244,6 +245,23 @@ var fib = function(n) {
return dp[n]
};
```
+解法二:时间复杂度O(N),空间复杂度O(1)
+```Javascript
+var fib = function(n) {
+ // 动规状态转移中,当前结果只依赖前两个元素的结果,所以只要两个变量代替dp数组记录状态过程。将空间复杂度降到O(1)
+ let pre1 = 1
+ let pre2 = 0
+ let temp
+ if (n === 0) return 0
+ if (n === 1) return 1
+ for(let i = 2; i <= n; i++) {
+ temp = pre1
+ pre1 = pre1 + pre2
+ pre2 = temp
+ }
+ return pre1
+};
+```
TypeScript
From a9f830267bdcd9a5bc6ec19dfbc1b4436fa41a1e Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sat, 28 May 2022 11:12:53 +0800
Subject: [PATCH 15/22] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880724.=E5=AF=BB?=
=?UTF-8?q?=E6=89=BE=E6=95=B0=E7=BB=84=E7=9A=84=E4=B8=AD=E5=BF=83=E7=B4=A2?=
=?UTF-8?q?=E5=BC=95.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/0724.寻找数组的中心索引.md | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/problems/0724.寻找数组的中心索引.md b/problems/0724.寻找数组的中心索引.md
index 14dcd2c0..0052e5d4 100644
--- a/problems/0724.寻找数组的中心索引.md
+++ b/problems/0724.寻找数组的中心索引.md
@@ -140,6 +140,24 @@ var pivotIndex = function(nums) {
};
```
+### TypeScript
+
+```typescript
+function pivotIndex(nums: number[]): number {
+ const length: number = nums.length;
+ const sum: number = nums.reduce((a, b) => a + b);
+ let leftSum: number = 0;
+ for (let i = 0; i < length; i++) {
+ const rightSum: number = sum - leftSum - nums[i];
+ if (leftSum === rightSum) return i;
+ leftSum += nums[i];
+ }
+ return -1;
+};
+```
+
+
+
-----------------------
From 6f46d91676693a83c0620e49bfdf9b9a252d87a0 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sat, 28 May 2022 13:55:52 +0800
Subject: [PATCH 16/22] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200530.=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F?=
=?UTF-8?q?=E7=BB=9D=E5=AF=B9=E5=B7=AE.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../0530.二叉搜索树的最小绝对差.md | 77 +++++++++++++++++++
1 file changed, 77 insertions(+)
diff --git a/problems/0530.二叉搜索树的最小绝对差.md b/problems/0530.二叉搜索树的最小绝对差.md
index 77699c9f..d7db85ae 100644
--- a/problems/0530.二叉搜索树的最小绝对差.md
+++ b/problems/0530.二叉搜索树的最小绝对差.md
@@ -431,7 +431,84 @@ function getMinimumDifference(root: TreeNode | null): number {
};
```
+## Scala
+构建二叉树的有序数组:
+
+```scala
+object Solution {
+ import scala.collection.mutable
+ def getMinimumDifference(root: TreeNode): Int = {
+ val arr = mutable.ArrayBuffer[Int]()
+ def traversal(node: TreeNode): Unit = {
+ if (node == null) return
+ traversal(node.left)
+ arr.append(node.value)
+ traversal(node.right)
+ }
+ traversal(root)
+ // 在有序数组上求最小差值
+ var result = Int.MaxValue
+ for (i <- 1 until arr.size) {
+ result = math.min(result, arr(i) - arr(i - 1))
+ }
+ result // 返回最小差值
+ }
+}
+```
+
+递归记录前一个节点:
+
+```scala
+object Solution {
+ def getMinimumDifference(root: TreeNode): Int = {
+ var result = Int.MaxValue // 初始化为最大值
+ var pre: TreeNode = null // 记录前一个节点
+
+ def traversal(cur: TreeNode): Unit = {
+ if (cur == null) return
+ traversal(cur.left)
+ if (pre != null) {
+ // 对比result与节点之间的差值
+ result = math.min(result, cur.value - pre.value)
+ }
+ pre = cur
+ traversal(cur.right)
+ }
+
+ traversal(root)
+ result // return关键字可以省略
+ }
+}
+```
+
+迭代解决:
+
+```scala
+object Solution {
+ import scala.collection.mutable
+ def getMinimumDifference(root: TreeNode): Int = {
+ var result = Int.MaxValue // 初始化为最大值
+ var pre: TreeNode = null // 记录前一个节点
+ var cur = root
+ var stack = mutable.Stack[TreeNode]()
+ while (cur != null || !stack.isEmpty) {
+ if (cur != null) {
+ stack.push(cur)
+ cur = cur.left
+ } else {
+ cur = stack.pop()
+ if (pre != null) {
+ result = math.min(result, cur.value - pre.value)
+ }
+ pre = cur
+ cur = cur.right
+ }
+ }
+ result // return关键字可以省略
+ }
+}
+```
-----------------------
From 779adf74cb1ec9b1f8b17db3cbf3d8fc3b88392f Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sat, 28 May 2022 14:53:40 +0800
Subject: [PATCH 17/22] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200501.=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E4=BC=97?=
=?UTF-8?q?=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
---
problems/0501.二叉搜索树中的众数.md | 71 +++++++++++++++++++-
1 file changed, 70 insertions(+), 1 deletion(-)
diff --git a/problems/0501.二叉搜索树中的众数.md b/problems/0501.二叉搜索树中的众数.md
index 9cb5d071..c08f68d9 100644
--- a/problems/0501.二叉搜索树中的众数.md
+++ b/problems/0501.二叉搜索树中的众数.md
@@ -9,7 +9,7 @@
# 501.二叉搜索树中的众数
-[力扣题目链接](https://leetcode-cn.com/problems/find-mode-in-binary-search-tree/solution/)
+[力扣题目链接](https://leetcode.cn/problems/find-mode-in-binary-search-tree/)
给定一个有相同值的二叉搜索树(BST),找出 BST 中的所有众数(出现频率最高的元素)。
@@ -798,7 +798,76 @@ function findMode(root: TreeNode | null): number[] {
};
```
+## Scala
+暴力:
+```scala
+object Solution {
+ // 导包
+ import scala.collection.mutable // 集合包
+ import scala.util.control.Breaks.{break, breakable} // 流程控制包
+ def findMode(root: TreeNode): Array[Int] = {
+ var map = mutable.HashMap[Int, Int]() // 存储节点的值,和该值出现的次数
+ def searchBST(curNode: TreeNode): Unit = {
+ if (curNode == null) return
+ var value = map.getOrElse(curNode.value, 0)
+ map.put(curNode.value, value + 1)
+ searchBST(curNode.left)
+ searchBST(curNode.right)
+ }
+ searchBST(root) // 前序遍历把每个节点的值加入到里面
+ // 将map转换为list,随后根据元组的第二个值进行排序
+ val list = map.toList.sortWith((map1, map2) => {
+ if (map1._2 > map2._2) true else false
+ })
+ var res = mutable.ArrayBuffer[Int]()
+ res.append(list(0)._1) // 将第一个加入结果集
+ breakable {
+ for (i <- 1 until list.size) {
+ // 如果值相同就加入结果集合,反之break
+ if (list(i)._2 == list(0)._2) res.append(list(i)._1)
+ else break
+ }
+ }
+ res.toArray // 最终返回res的Array格式,return关键字可以省略
+ }
+}
+```
+
+递归(利用二叉搜索树的性质):
+```scala
+object Solution {
+ import scala.collection.mutable
+ def findMode(root: TreeNode): Array[Int] = {
+ var maxCount = 0 // 最大频率
+ var count = 0 // 统计频率
+ var pre: TreeNode = null
+ var result = mutable.ArrayBuffer[Int]()
+
+ def searchBST(cur: TreeNode): Unit = {
+ if (cur == null) return
+ searchBST(cur.left)
+ if (pre == null) count = 1 // 等于空置为1
+ else if (pre.value == cur.value) count += 1 // 与上一个节点的值相同加1
+ else count = 1 // 与上一个节点的值不同
+ pre = cur
+
+ // 如果和最大值相同,则放入结果集
+ if (count == maxCount) result.append(cur.value)
+
+ // 如果当前计数大于最大值频率,更新最大值,清空结果集
+ if (count > maxCount) {
+ maxCount = count
+ result.clear()
+ result.append(cur.value)
+ }
+ searchBST(cur.right)
+ }
+ searchBST(root)
+ result.toArray // return关键字可以省略
+ }
+}
+```
-----------------------
From 95cc2b965807a6493fe64eb23d6bbfa66587f0fa Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sun, 29 May 2022 10:18:40 +0800
Subject: [PATCH 18/22] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200236.=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E8=BF=91=E5=85=AC=E5=85=B1?=
=?UTF-8?q?=E7=A5=96=E5=85=88.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../0236.二叉树的最近公共祖先.md | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/problems/0236.二叉树的最近公共祖先.md b/problems/0236.二叉树的最近公共祖先.md
index 69a6d0d6..a99180c3 100644
--- a/problems/0236.二叉树的最近公共祖先.md
+++ b/problems/0236.二叉树的最近公共祖先.md
@@ -343,7 +343,25 @@ function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: Tree
};
```
+## Scala
+```scala
+object Solution {
+ def lowestCommonAncestor(root: TreeNode, p: TreeNode, q: TreeNode): TreeNode = {
+ // 递归结束条件
+ if (root == null || root == p || root == q) {
+ return root
+ }
+
+ var left = lowestCommonAncestor(root.left, p, q)
+ var right = lowestCommonAncestor(root.right, p, q)
+
+ if (left != null && right != null) return root
+ if (left == null) return right
+ left
+ }
+}
+```
-----------------------
From 78930cdd0970c0850db6aa7c90ced0510fd6b1ec Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sun, 29 May 2022 10:32:47 +0800
Subject: [PATCH 19/22] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200235.=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E8=BF=91?=
=?UTF-8?q?=E5=85=AC=E5=85=B1=E7=A5=96=E5=85=88.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
---
...35.二叉搜索树的最近公共祖先.md | 29 +++++++++++++++++++
1 file changed, 29 insertions(+)
diff --git a/problems/0235.二叉搜索树的最近公共祖先.md b/problems/0235.二叉搜索树的最近公共祖先.md
index f7f1427a..a0f30999 100644
--- a/problems/0235.二叉搜索树的最近公共祖先.md
+++ b/problems/0235.二叉搜索树的最近公共祖先.md
@@ -381,7 +381,36 @@ function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: Tree
};
```
+## Scala
+递归:
+
+```scala
+object Solution {
+ def lowestCommonAncestor(root: TreeNode, p: TreeNode, q: TreeNode): TreeNode = {
+ // scala中每个关键字都有其返回值,于是可以不写return
+ if (root.value > p.value && root.value > q.value) lowestCommonAncestor(root.left, p, q)
+ else if (root.value < p.value && root.value < q.value) lowestCommonAncestor(root.right, p, q)
+ else root
+ }
+}
+```
+
+迭代:
+
+```scala
+object Solution {
+ def lowestCommonAncestor(root: TreeNode, p: TreeNode, q: TreeNode): TreeNode = {
+ var curNode = root // 因为root是不可变量,所以要赋值给curNode一个可变量
+ while(curNode != null){
+ if(curNode.value > p.value && curNode.value > q.value) curNode = curNode.left
+ else if(curNode.value < p.value && curNode.value < q.value) curNode = curNode.right
+ else return curNode
+ }
+ null
+ }
+}
+```
-----------------------
From 45a01eed32598a6cf4c9a20d995aa46efe296dd5 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sun, 29 May 2022 13:04:37 +0800
Subject: [PATCH 20/22] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200701.=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E6=8F=92?=
=?UTF-8?q?=E5=85=A5=E6=93=8D=E4=BD=9C.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../0701.二叉搜索树中的插入操作.md | 37 +++++++++++++++++++
1 file changed, 37 insertions(+)
diff --git a/problems/0701.二叉搜索树中的插入操作.md b/problems/0701.二叉搜索树中的插入操作.md
index 50e39ade..135911b9 100644
--- a/problems/0701.二叉搜索树中的插入操作.md
+++ b/problems/0701.二叉搜索树中的插入操作.md
@@ -585,6 +585,43 @@ function insertIntoBST(root: TreeNode | null, val: number): TreeNode | null {
```
+## Scala
+
+递归:
+
+```scala
+object Solution {
+ def insertIntoBST(root: TreeNode, `val`: Int): TreeNode = {
+ if (root == null) return new TreeNode(`val`)
+ if (`val` < root.value) root.left = insertIntoBST(root.left, `val`)
+ else root.right = insertIntoBST(root.right, `val`)
+ root // 返回根节点
+ }
+}
+```
+
+迭代:
+
+```scala
+object Solution {
+ def insertIntoBST(root: TreeNode, `val`: Int): TreeNode = {
+ if (root == null) {
+ return new TreeNode(`val`)
+ }
+ var parent = root // 记录当前节点的父节点
+ var curNode = root
+ while (curNode != null) {
+ parent = curNode
+ if(`val` < curNode.value) curNode = curNode.left
+ else curNode = curNode.right
+ }
+ if(`val` < parent.value) parent.left = new TreeNode(`val`)
+ else parent.right = new TreeNode(`val`)
+ root // 最终返回根节点
+ }
+}
+```
+
-----------------------
From cc72b164cf7780f3dc4e119d30033dd0708eddd5 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sun, 29 May 2022 22:48:13 +0800
Subject: [PATCH 21/22] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880034.=E5=9C=A8?=
=?UTF-8?q?=E6=8E=92=E5=BA=8F=E6=95=B0=E7=BB=84=E4=B8=AD=E6=9F=A5=E6=89=BE?=
=?UTF-8?q?=E5=85=83=E7=B4=A0=E7=9A=84=E7=AC=AC=E4=B8=80=E4=B8=AA=E5=92=8C?=
=?UTF-8?q?=E6=9C=80=E5=90=8E=E4=B8=80=E4=B8=AA=E4=BD=8D=E7=BD=AE.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
---
...元素的第一个和最后一个位置.md | 55 +++++++++++++++++++
1 file changed, 55 insertions(+)
diff --git a/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md b/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md
index dfd90b82..25db0083 100644
--- a/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md
+++ b/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md
@@ -481,6 +481,61 @@ var searchRange = function(nums, target) {
};
```
+### TypeScript
+
+```typescript
+function searchRange(nums: number[], target: number): number[] {
+ const leftBoard: number = getLeftBorder(nums, target);
+ const rightBoard: number = getRightBorder(nums, target);
+ // target 在nums区间左侧或右侧
+ if (leftBoard === (nums.length - 1) || rightBoard === 0) return [-1, -1];
+ // target 不存在与nums范围内
+ if (rightBoard - leftBoard <= 1) return [-1, -1];
+ // target 存在于nums范围内
+ return [leftBoard + 1, rightBoard - 1];
+};
+// 查找第一个大于target的元素下标
+function getRightBorder(nums: number[], target: number): number {
+ let left: number = 0,
+ right: number = nums.length - 1;
+ // 0表示target在nums区间的左边
+ let rightBoard: number = 0;
+ while (left <= right) {
+ let mid = Math.floor((left + right) / 2);
+ if (nums[mid] <= target) {
+ // 右边界一定在mid右边(不含mid)
+ left = mid + 1;
+ rightBoard = left;
+ } else {
+ // 右边界在mid左边(含mid)
+ right = mid - 1;
+ }
+ }
+ return rightBoard;
+}
+// 查找第一个小于target的元素下标
+function getLeftBorder(nums: number[], target: number): number {
+ let left: number = 0,
+ right: number = nums.length - 1;
+ // length-1表示target在nums区间的右边
+ let leftBoard: number = nums.length - 1;
+ while (left <= right) {
+ let mid = Math.floor((left + right) / 2);
+ if (nums[mid] >= target) {
+ // 左边界一定在mid左边(不含mid)
+ right = mid - 1;
+ leftBoard = right;
+ } else {
+ // 左边界在mid右边(含mid)
+ left = mid + 1;
+ }
+ }
+ return leftBoard;
+}
+```
+
+
+
-----------------------
From 8eb956b553b178d3d14555c270f8b726baea5870 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Mon, 30 May 2022 10:29:31 +0800
Subject: [PATCH 22/22] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200450.=E5=88=A0?=
=?UTF-8?q?=E9=99=A4=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD?=
=?UTF-8?q?=E7=9A=84=E8=8A=82=E7=82=B9.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../0450.删除二叉搜索树中的节点.md | 28 +++++++++++++++++++
1 file changed, 28 insertions(+)
diff --git a/problems/0450.删除二叉搜索树中的节点.md b/problems/0450.删除二叉搜索树中的节点.md
index e8f7e54c..8367a145 100644
--- a/problems/0450.删除二叉搜索树中的节点.md
+++ b/problems/0450.删除二叉搜索树中的节点.md
@@ -582,7 +582,35 @@ function deleteNode(root: TreeNode | null, key: number): TreeNode | null {
};
```
+## Scala
+```scala
+object Solution {
+ def deleteNode(root: TreeNode, key: Int): TreeNode = {
+ if (root == null) return root // 第一种情况,没找到删除的节点,遍历到空节点直接返回
+ if (root.value == key) {
+ // 第二种情况: 左右孩子都为空,直接删除节点,返回null
+ if (root.left == null && root.right == null) return null
+ // 第三种情况: 左孩子为空,右孩子不为空,右孩子补位
+ else if (root.left == null && root.right != null) return root.right
+ // 第四种情况: 左孩子不为空,右孩子为空,左孩子补位
+ else if (root.left != null && root.right == null) return root.left
+ // 第五种情况: 左右孩子都不为空,将删除节点的左子树头节点(左孩子)放到
+ // 右子树的最左边节点的左孩子上,返回删除节点的右孩子
+ else {
+ var tmp = root.right
+ while (tmp.left != null) tmp = tmp.left
+ tmp.left = root.left
+ return root.right
+ }
+ }
+ if (root.value > key) root.left = deleteNode(root.left, key)
+ if (root.value < key) root.right = deleteNode(root.right, key)
+
+ root // 返回根节点,return关键字可以省略
+ }
+}
+```
-----------------------