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 1/6] =?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 2/6] =?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 3/6] =?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 4/6] =?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 5/6] =?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 6/6] =?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
+ }
+}
+```
+
-----------------------