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 01/17] =?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 02/17] =?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 03/17] =?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 04/17] =?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 05/17] =?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关键字可以省略
+ }
+}
+```
-----------------------
From df2ff8cc2e77261a0360ac848949c233f3c6258a Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Mon, 30 May 2022 10:50:51 +0800
Subject: [PATCH 06/17] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200669.=E4=BF=AE?=
=?UTF-8?q?=E5=89=AA=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/0669.修剪二叉搜索树.md | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/problems/0669.修剪二叉搜索树.md b/problems/0669.修剪二叉搜索树.md
index 385b2268..988bff30 100644
--- a/problems/0669.修剪二叉搜索树.md
+++ b/problems/0669.修剪二叉搜索树.md
@@ -453,7 +453,21 @@ function trimBST(root: TreeNode | null, low: number, high: number): TreeNode | n
};
```
+## Scala
+递归法:
+```scala
+object Solution {
+ def trimBST(root: TreeNode, low: Int, high: Int): TreeNode = {
+ if (root == null) return null
+ if (root.value < low) return trimBST(root.right, low, high)
+ if (root.value > high) return trimBST(root.left, low, high)
+ root.left = trimBST(root.left, low, high)
+ root.right = trimBST(root.right, low, high)
+ root
+ }
+}
+```
-----------------------
From 43d450d8271b6af68d209bda8f22ed71838418f1 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Mon, 30 May 2022 11:20:33 +0800
Subject: [PATCH 07/17] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880922.=E6=8C=89?=
=?UTF-8?q?=E5=A5=87=E5=81=B6=E6=8E=92=E5=BA=8F=E6=95=B0=E7=BB=84II.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/0922.按奇偶排序数组II.md | 69 ++++++++++++++++++++++++
1 file changed, 69 insertions(+)
diff --git a/problems/0922.按奇偶排序数组II.md b/problems/0922.按奇偶排序数组II.md
index cb564fb6..cb8cec73 100644
--- a/problems/0922.按奇偶排序数组II.md
+++ b/problems/0922.按奇偶排序数组II.md
@@ -260,6 +260,75 @@ var sortArrayByParityII = function(nums) {
};
```
+### TypeScript
+
+> 方法一:
+
+```typescript
+function sortArrayByParityII(nums: number[]): number[] {
+ const evenArr: number[] = [],
+ oddArr: number[] = [];
+ for (let num of nums) {
+ if (num % 2 === 0) {
+ evenArr.push(num);
+ } else {
+ oddArr.push(num);
+ }
+ }
+ const resArr: number[] = [];
+ for (let i = 0, length = nums.length / 2; i < length; i++) {
+ resArr.push(evenArr[i]);
+ resArr.push(oddArr[i]);
+ }
+ return resArr;
+};
+```
+
+> 方法二:
+
+```typescript
+function sortArrayByParityII(nums: number[]): number[] {
+ const length: number = nums.length;
+ const resArr: number[] = [];
+ let evenIndex: number = 0,
+ oddIndex: number = 1;
+ for (let i = 0; i < length; i++) {
+ if (nums[i] % 2 === 0) {
+ resArr[evenIndex] = nums[i];
+ evenIndex += 2;
+ } else {
+ resArr[oddIndex] = nums[i];
+ oddIndex += 2;
+ }
+ }
+ return resArr;
+};
+```
+
+> 方法三:
+
+```typescript
+function sortArrayByParityII(nums: number[]): number[] {
+ const length: number = nums.length;
+ let oddIndex: number = 1;
+ for (let evenIndex = 0; evenIndex < length; evenIndex += 2) {
+ if (nums[evenIndex] % 2 === 1) {
+ // 在偶数位遇到了奇数
+ while (oddIndex < length && nums[oddIndex] % 2 === 1) {
+ oddIndex += 2;
+ }
+ // 在奇数位遇到了偶数,交换
+ let temp = nums[evenIndex];
+ nums[evenIndex] = nums[oddIndex];
+ nums[oddIndex] = temp;
+ }
+ }
+ return nums;
+};
+```
+
+
+
-----------------------
From 3e885d3aff0b2c826fe522d7eb5d2cd039b62f13 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Mon, 30 May 2022 11:25:52 +0800
Subject: [PATCH 08/17] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200108.=E5=B0=86?=
=?UTF-8?q?=E6=9C=89=E5=BA=8F=E6=95=B0=E7=BB=84=E8=BD=AC=E6=8D=A2=E4=B8=BA?=
=?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91.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
---
...将有序数组转换为二叉搜索树.md | 22 +++++++++++++++++++
1 file changed, 22 insertions(+)
diff --git a/problems/0108.将有序数组转换为二叉搜索树.md b/problems/0108.将有序数组转换为二叉搜索树.md
index 6ee3947b..493118a6 100644
--- a/problems/0108.将有序数组转换为二叉搜索树.md
+++ b/problems/0108.将有序数组转换为二叉搜索树.md
@@ -448,5 +448,27 @@ struct TreeNode* sortedArrayToBST(int* nums, int numsSize) {
}
```
+## Scala
+
+递归:
+
+```scala
+object Solution {
+ def sortedArrayToBST(nums: Array[Int]): TreeNode = {
+ def buildTree(left: Int, right: Int): TreeNode = {
+ if (left > right) return null // 当left大于right的时候,返回空
+ // 最中间的节点是当前节点
+ var mid = left + (right - left) / 2
+ var curNode = new TreeNode(nums(mid))
+ curNode.left = buildTree(left, mid - 1)
+ curNode.right = buildTree(mid + 1, right)
+ curNode
+ }
+ buildTree(0, nums.size - 1)
+ }
+}
+```
+
+
-----------------------
From d4b5abfe9bc2f32f99c92a1d3794b58d78c69141 Mon Sep 17 00:00:00 2001
From: Ezralin <10881430+ezralin@user.noreply.gitee.com>
Date: Tue, 31 May 2022 08:41:53 +0800
Subject: [PATCH 09/17] =?UTF-8?q?=E6=B7=BB=E5=8A=A00704.=E4=BA=8C=E5=88=86?=
=?UTF-8?q?=E6=9F=A5=E6=89=BE.md=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 | 30 ++++++++++++++++++++++++++++++
1 file changed, 30 insertions(+)
diff --git a/problems/0704.二分查找.md b/problems/0704.二分查找.md
index 55625130..3dcbd175 100644
--- a/problems/0704.二分查找.md
+++ b/problems/0704.二分查找.md
@@ -611,6 +611,36 @@ public class Solution{
}
```
+**Kotlin:**
+```kotlin
+class Solution {
+ fun search(nums: IntArray, target: Int): Int {
+ // leftBorder
+ var left:Int = 0
+ // rightBorder
+ var right:Int = nums.size - 1
+ // 使用左闭右闭区间
+ while (left <= right) {
+ var middle:Int = left + (right - left)/2
+ // taget 在左边
+ if (nums[middle] > target) {
+ right = middle - 1
+ }
+ else {
+ // target 在右边
+ if (nums[middle] < target) {
+ left = middle + 1
+ }
+ // 找到了,返回
+ else return middle
+ }
+ }
+ // 没找到,返回
+ return -1
+ }
+}
+```
+
-----------------------
From ab5b82969f30b767f2815883272f4b3a49ad0c82 Mon Sep 17 00:00:00 2001
From: SevenMonths
Date: Tue, 31 May 2022 15:56:00 +0800
Subject: [PATCH 10/17] =?UTF-8?q?=E6=96=B0=E5=A2=9E(0150.=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)=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/0150.逆波兰表达式求值.md | 24 +++++++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git a/problems/0150.逆波兰表达式求值.md b/problems/0150.逆波兰表达式求值.md
index fd3d69aa..7e142ba3 100644
--- a/problems/0150.逆波兰表达式求值.md
+++ b/problems/0150.逆波兰表达式求值.md
@@ -326,5 +326,29 @@ func evalRPN(_ tokens: [String]) -> Int {
}
```
+PHP:
+```php
+class Solution {
+ function evalRPN($tokens) {
+ $st = new SplStack();
+ for($i = 0;$ipush($tokens[$i]);
+ }else{
+ // 是符号进行运算
+ $num1 = $st->pop();
+ $num2 = $st->pop();
+ if ($tokens[$i] == "+") $st->push($num2 + $num1);
+ if ($tokens[$i] == "-") $st->push($num2 - $num1);
+ if ($tokens[$i] == "*") $st->push($num2 * $num1);
+ // 注意处理小数部分
+ if ($tokens[$i] == "/") $st->push(intval($num2 / $num1));
+ }
+ }
+ return $st->pop();
+ }
+}
+```
-----------------------
From ca18b7d1f5c31a91c262ec4a1cf0b93cc00b3cf1 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Tue, 31 May 2022 22:13:58 +0800
Subject: [PATCH 11/17] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200538.=E6=8A=8A?=
=?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E8=BD=AC=E6=8D=A2?=
=?UTF-8?q?=E4=B8=BA=E7=B4=AF=E5=8A=A0=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
---
...538.把二叉搜索树转换为累加树.md | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/problems/0538.把二叉搜索树转换为累加树.md b/problems/0538.把二叉搜索树转换为累加树.md
index 37eb7d0f..13637784 100644
--- a/problems/0538.把二叉搜索树转换为累加树.md
+++ b/problems/0538.把二叉搜索树转换为累加树.md
@@ -352,6 +352,24 @@ function convertBST(root: TreeNode | null): TreeNode | null {
};
```
+## Scala
+
+```scala
+object Solution {
+ def convertBST(root: TreeNode): TreeNode = {
+ var sum = 0
+ def convert(node: TreeNode): Unit = {
+ if (node == null) return
+ convert(node.right)
+ sum += node.value
+ node.value = sum
+ convert(node.left)
+ }
+ convert(root)
+ root
+ }
+}
+```
-----------------------
From 752cfda89351cc2d54897a6309a181f197cc6c50 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=A1=9C=E5=B0=8F=E8=B7=AF=E4=B8=83=E8=91=89?=
<20304773@qq.com>
Date: Tue, 31 May 2022 23:46:41 +0800
Subject: [PATCH 12/17] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880027.=E7=A7=BB?=
=?UTF-8?q?=E9=99=A4=E5=85=83=E7=B4=A0.md=EF=BC=89=EF=BC=9A=E5=A2=9E?=
=?UTF-8?q?=E5=8A=A0=20C#=20=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0027.移除元素.md | 15 +++++++++++++++
1 file changed, 15 insertions(+)
diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md
index 3a93ac88..5e7742ee 100644
--- a/problems/0027.移除元素.md
+++ b/problems/0027.移除元素.md
@@ -329,5 +329,20 @@ int removeElement(int* nums, int numsSize, int val){
}
```
+C#:
+```csharp
+public class Solution {
+ public int RemoveElement(int[] nums, int val) {
+ int slow = 0;
+ for (int fast = 0; fast < nums.Length; fast++) {
+ if (val != nums[fast]) {
+ nums[slow++] = nums[fast];
+ }
+ }
+ return slow;
+ }
+}
+```
+
-----------------------
From f51d72b71baf6698d2078dd9b2ce5432aa7cfe41 Mon Sep 17 00:00:00 2001
From: SevenMonths
Date: Wed, 1 Jun 2022 18:45:16 +0800
Subject: [PATCH 13/17] =?UTF-8?q?=E6=B7=BB=E5=8A=A0(0239.=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)?=
=?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
---
problems/0239.滑动窗口最大值.md | 77 ++++++++++++++++++++++++++
1 file changed, 77 insertions(+)
diff --git a/problems/0239.滑动窗口最大值.md b/problems/0239.滑动窗口最大值.md
index f269450f..227ab71c 100644
--- a/problems/0239.滑动窗口最大值.md
+++ b/problems/0239.滑动窗口最大值.md
@@ -631,5 +631,82 @@ func maxSlidingWindow(_ nums: [Int], _ k: Int) -> [Int] {
}
```
+PHP:
+```php
+class Solution {
+ /**
+ * @param Integer[] $nums
+ * @param Integer $k
+ * @return Integer[]
+ */
+ function maxSlidingWindow($nums, $k) {
+ $myQueue = new MyQueue();
+ // 先将前k的元素放进队列
+ for ($i = 0; $i < $k; $i++) {
+ $myQueue->push($nums[$i]);
+ }
+
+ $result = [];
+ $result[] = $myQueue->max(); // result 记录前k的元素的最大值
+
+ for ($i = $k; $i < count($nums); $i++) {
+ $myQueue->pop($nums[$i - $k]); // 滑动窗口移除最前面元素
+ $myQueue->push($nums[$i]); // 滑动窗口前加入最后面的元素
+ $result[]= $myQueue->max(); // 记录对应的最大值
+ }
+ return $result;
+ }
+
+}
+
+// 单调对列构建
+class MyQueue{
+ private $queue;
+
+ public function __construct(){
+ $this->queue = new SplQueue(); //底层是双向链表实现。
+ }
+
+ public function pop($v){
+ // 判断当前对列是否为空
+ // 比较当前要弹出的数值是否等于队列出口元素的数值,如果相等则弹出。
+ // bottom 从链表前端查看元素, dequeue 从双向链表的开头移动一个节点
+ if(!$this->queue->isEmpty() && $v == $this->queue->bottom()){
+ $this->queue->dequeue(); //弹出队列
+ }
+ }
+
+ public function push($v){
+ // 判断当前对列是否为空
+ // 如果push的数值大于入口元素的数值,那么就将队列后端的数值弹出,直到push的数值小于等于队列入口元素的数值为止。
+ // 这样就保持了队列里的数值是单调从大到小的了。
+ while (!$this->queue->isEmpty() && $v > $this->queue->top()) {
+ $this->queue->pop(); // pop从链表末尾弹出一个元素,
+ }
+ $this->queue->enqueue($v);
+ }
+
+ // 查询当前队列里的最大值 直接返回队首
+ public function max(){
+ // bottom 从链表前端查看元素, top从链表末尾查看元素
+ return $this->queue->bottom();
+ }
+
+ // 辅助理解: 打印队列元素
+ public function println(){
+ // "迭代器移动到链表头部": 可理解为从头遍历链表元素做准备。
+ // 【PHP中没有指针概念,所以就没说指针。从数据结构上理解,就是把指针指向链表头部】
+ $this->queue->rewind();
+
+ echo "Println: ";
+ while($this->queue->valid()){
+ echo $this->queue->current()," -> ";
+ $this->queue->next();
+ }
+ echo "\n";
+ }
+}
+```
+
-----------------------
From 90075121ecd61558cb50d80668298fb4263c6cc0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=A1=9C=E5=B0=8F=E8=B7=AF=E4=B8=83=E8=91=89?=
<20304773@qq.com>
Date: Wed, 1 Jun 2022 19:47:53 +0800
Subject: [PATCH 14/17] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880977.=E6=9C=89?=
=?UTF-8?q?=E5=BA=8F=E6=95=B0=E7=BB=84=E7=9A=84=E5=B9=B3=E6=96=B9.md?=
=?UTF-8?q?=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0=20C#=20=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0977.有序数组的平方.md | 20 +++++++++++++++++++-
1 file changed, 19 insertions(+), 1 deletion(-)
diff --git a/problems/0977.有序数组的平方.md b/problems/0977.有序数组的平方.md
index 0e79a3d6..f1b0e4ec 100644
--- a/problems/0977.有序数组的平方.md
+++ b/problems/0977.有序数组的平方.md
@@ -394,6 +394,24 @@ object Solution {
}
```
-
+C#:
+```csharp
+public class Solution {
+ public int[] SortedSquares(int[] nums) {
+ int k = nums.Length - 1;
+ int[] result = new int[nums.Length];
+ for (int i = 0, j = nums.Length - 1;i <= j;){
+ if (nums[i] * nums[i] < nums[j] * nums[j]) {
+ result[k--] = nums[j] * nums[j];
+ j--;
+ } else {
+ result[k--] = nums[i] * nums[i];
+ i++;
+ }
+ }
+ return result;
+ }
+}
+```
-----------------------
From 0bdb37d7fa0138c6870b882a8577146ad43a01ca Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 2 Jun 2022 10:05:46 +0800
Subject: [PATCH 15/17] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200077.=E7=BB=84?=
=?UTF-8?q?=E5=90=88.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0077.组合.md | 28 ++++++++++++++++++++++++++++
1 file changed, 28 insertions(+)
diff --git a/problems/0077.组合.md b/problems/0077.组合.md
index 9e0398ab..f280c176 100644
--- a/problems/0077.组合.md
+++ b/problems/0077.组合.md
@@ -673,5 +673,33 @@ func combine(_ n: Int, _ k: Int) -> [[Int]] {
}
```
+### Scala
+
+```scala
+object Solution {
+ import scala.collection.mutable // 导包
+ def combine(n: Int, k: Int): List[List[Int]] = {
+ var result = mutable.ListBuffer[List[Int]]() // 存放结果集
+ var path = mutable.ListBuffer[Int]() //存放符合条件的结果
+
+ def backtracking(n: Int, k: Int, startIndex: Int): Unit = {
+ if (path.size == k) {
+ // 如果path的size == k就达到题目要求,添加到结果集,并返回
+ result.append(path.toList)
+ return
+ }
+ for (i <- startIndex to n) { // 遍历从startIndex到n
+ path.append(i) // 先把数字添加进去
+ backtracking(n, k, i + 1) // 进行下一步回溯
+ path = path.take(path.size - 1) // 回溯完再删除掉刚刚添加的数字
+ }
+ }
+
+ backtracking(n, k, 1) // 执行回溯
+ result.toList // 最终返回result的List形式,return关键字可以省略
+ }
+}
+```
+
-----------------------
From e734d0a12265ac455691d5cd15efe2c7fbfb8491 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 2 Jun 2022 10:18:52 +0800
Subject: [PATCH 16/17] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200077.=E7=BB=84?=
=?UTF-8?q?=E5=90=88.md=20=E5=89=AA=E6=9E=9D=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0077.组合.md | 30 ++++++++++++++++++++++++++++++
1 file changed, 30 insertions(+)
diff --git a/problems/0077.组合.md b/problems/0077.组合.md
index f280c176..58382221 100644
--- a/problems/0077.组合.md
+++ b/problems/0077.组合.md
@@ -675,6 +675,7 @@ func combine(_ n: Int, _ k: Int) -> [[Int]] {
### Scala
+暴力:
```scala
object Solution {
import scala.collection.mutable // 导包
@@ -701,5 +702,34 @@ object Solution {
}
```
+剪枝:
+
+```scala
+object Solution {
+ import scala.collection.mutable // 导包
+ def combine(n: Int, k: Int): List[List[Int]] = {
+ var result = mutable.ListBuffer[List[Int]]() // 存放结果集
+ var path = mutable.ListBuffer[Int]() //存放符合条件的结果
+
+ def backtracking(n: Int, k: Int, startIndex: Int): Unit = {
+ if (path.size == k) {
+ // 如果path的size == k就达到题目要求,添加到结果集,并返回
+ result.append(path.toList)
+ return
+ }
+ // 剪枝优化
+ for (i <- startIndex to (n - (k - path.size) + 1)) {
+ path.append(i) // 先把数字添加进去
+ backtracking(n, k, i + 1) // 进行下一步回溯
+ path = path.take(path.size - 1) // 回溯完再删除掉刚刚添加的数字
+ }
+ }
+
+ backtracking(n, k, 1) // 执行回溯
+ result.toList // 最终返回result的List形式,return关键字可以省略
+ }
+}
+```
+
-----------------------
From 5ac414f466ac9d80511aa5c2c6659df8fa5419a0 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 2 Jun 2022 10:22:56 +0800
Subject: [PATCH 17/17] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200077.=E7=BB=84?=
=?UTF-8?q?=E5=90=88=E4=BC=98=E5=8C=96.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0077.组合优化.md | 29 +++++++++++++++++++++++++++++
1 file changed, 29 insertions(+)
diff --git a/problems/0077.组合优化.md b/problems/0077.组合优化.md
index 94608ec1..2c1a821b 100644
--- a/problems/0077.组合优化.md
+++ b/problems/0077.组合优化.md
@@ -346,5 +346,34 @@ func combine(_ n: Int, _ k: Int) -> [[Int]] {
}
```
+Scala:
+
+```scala
+object Solution {
+ import scala.collection.mutable // 导包
+ def combine(n: Int, k: Int): List[List[Int]] = {
+ var result = mutable.ListBuffer[List[Int]]() // 存放结果集
+ var path = mutable.ListBuffer[Int]() //存放符合条件的结果
+
+ def backtracking(n: Int, k: Int, startIndex: Int): Unit = {
+ if (path.size == k) {
+ // 如果path的size == k就达到题目要求,添加到结果集,并返回
+ result.append(path.toList)
+ return
+ }
+ // 剪枝优化
+ for (i <- startIndex to (n - (k - path.size) + 1)) {
+ path.append(i) // 先把数字添加进去
+ backtracking(n, k, i + 1) // 进行下一步回溯
+ path = path.take(path.size - 1) // 回溯完再删除掉刚刚添加的数字
+ }
+ }
+
+ backtracking(n, k, 1) // 执行回溯
+ result.toList // 最终返回result的List形式,return关键字可以省略
+ }
+}
+```
+
-----------------------