From 8f9c8e5fb1f9d89830b92a06f1fc59d97fd84772 Mon Sep 17 00:00:00 2001
From: Luo <82520819+jerry-306@users.noreply.github.com>
Date: 2022年5月23日 10:10:48 +0800
Subject: [PATCH 1/5] =?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
---
...4347円232円204円351円207円215円351円207円217円II.md" | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git "a/problems/1049.346円234円200円345円220円216円344円270円200円345円235円227円347円237円263円345円244円264円347円232円204円351円207円215円351円207円217円II.md" "b/problems/1049.346円234円200円345円220円216円344円270円200円345円235円227円347円237円263円345円244円264円347円232円204円351円207円215円351円207円217円II.md"
index ee0ddef2d1..8d3eeb3b23 100644
--- "a/problems/1049.346円234円200円345円220円216円344円270円200円345円235円227円347円237円263円345円244円264円347円232円204円351円207円215円351円207円217円II.md"
+++ "b/problems/1049.346円234円200円345円220円216円344円270円200円345円235円227円347円237円263円345円244円264円347円232円204円351円207円215円351円207円217円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: 2022年5月23日 10:20:08 +0800
Subject: [PATCH 2/5] =?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
---
...4.347円233円256円346円240円207円345円222円214円.md" | 22 +++++++++++++++++++
1 file changed, 22 insertions(+)
diff --git "a/problems/0494.347円233円256円346円240円207円345円222円214円.md" "b/problems/0494.347円233円256円346円240円207円345円222円214円.md"
index 99b768347d..929b97d417 100644
--- "a/problems/0494.347円233円256円346円240円207円345円222円214円.md"
+++ "b/problems/0494.347円233円256円346円240円207円345円222円214円.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: 2022年5月23日 11:18:04 +0800
Subject: [PATCH 3/5] =?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
---
...27347円232円204円346円225円260円345円255円227円.md" | 40 +++++++++++++++++++
1 file changed, 40 insertions(+)
diff --git "a/problems/1365.346円234円211円345円244円232円345円260円221円345円260円217円344円272円216円345円275円223円345円211円215円346円225円260円345円255円227円347円232円204円346円225円260円345円255円227円.md" "b/problems/1365.346円234円211円345円244円232円345円260円221円345円260円217円344円272円216円345円275円223円345円211円215円346円225円260円345円255円227円347円232円204円346円225円260円345円255円227円.md"
index 78fa84c094..ce1e77dfa7 100644
--- "a/problems/1365.346円234円211円345円244円232円345円260円221円345円260円217円344円272円216円345円275円223円345円211円215円346円225円260円345円255円227円347円232円204円346円225円260円345円255円227円.md"
+++ "b/problems/1365.346円234円211円345円244円232円345円260円221円345円260円217円344円272円216円345円275円223円345円211円215円346円225円260円345円255円227円347円232円204円346円225円260円345円255円227円.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: 2022年5月23日 19:14:45 +0800
Subject: [PATCH 4/5] =?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
---
...02347円202円271円344円270円252円346円225円260円.md" | 63 +++++++++++++++++++
1 file changed, 63 insertions(+)
diff --git "a/problems/0222.345円256円214円345円205円250円344円272円214円345円217円211円346円240円221円347円232円204円350円212円202円347円202円271円344円270円252円346円225円260円.md" "b/problems/0222.345円256円214円345円205円250円344円272円214円345円217円211円346円240円221円347円232円204円350円212円202円347円202円271円344円270円252円346円225円260円.md"
index ba7acc5a49..746d45cc4a 100644
--- "a/problems/0222.345円256円214円345円205円250円344円272円214円345円217円211円346円240円221円347円232円204円350円212円202円347円202円271円344円270円252円346円225円260円.md"
+++ "b/problems/0222.345円256円214円345円205円250円344円272円214円345円217円211円346円240円221円347円232円204円350円212円202円347円202円271円344円270円252円346円225円260円.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: 2022年5月23日 19:49:18 +0800
Subject: [PATCH 5/5] =?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
---
...00346円234円211円350円267円257円345円276円204円.md" | 30 +++++++++++++++++++
1 file changed, 30 insertions(+)
diff --git "a/problems/0257.344円272円214円345円217円211円346円240円221円347円232円204円346円211円200円346円234円211円350円267円257円345円276円204円.md" "b/problems/0257.344円272円214円345円217円211円346円240円221円347円232円204円346円211円200円346円234円211円350円267円257円345円276円204円.md"
index 1362897c89..70a3c66f5d 100644
--- "a/problems/0257.344円272円214円345円217円211円346円240円221円347円232円204円346円211円200円346円234円211円350円267円257円345円276円204円.md"
+++ "b/problems/0257.344円272円214円345円217円211円346円240円221円347円232円204円346円211円200円346円234円211円350円267円257円345円276円204円.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
+ }
+}
+```
+
-----------------------