From 5627d4ba8605f35b8d63cf4af363c64d721f69a3 Mon Sep 17 00:00:00 2001
From: Jamcy123 <1219502823@qq.com>
Date: Thu, 12 May 2022 16:04:42 +0800
Subject: [PATCH 1/6] =?UTF-8?q?=E8=83=8C=E5=8C=85=E7=90=86=E8=AE=BA?=
=?UTF-8?q?=E5=9F=BA=E7=A1=8001=E8=83=8C=E5=8C=85-1=20js=20=E7=89=88?=
=?UTF-8?q?=E6=9C=AC=E4=BF=AE=E6=94=B9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/背包理论基础01背包-1.md | 51 +++++++++---------------
1 file changed, 19 insertions(+), 32 deletions(-)
diff --git a/problems/背包理论基础01背包-1.md b/problems/背包理论基础01背包-1.md
index fe940b4c..e833ea80 100644
--- a/problems/背包理论基础01背包-1.md
+++ b/problems/背包理论基础01背包-1.md
@@ -380,44 +380,31 @@ func main() {
### javascript
```js
-function testweightbagproblem (wight, value, size) {
- const len = wight.length,
- dp = array.from({length: len + 1}).map(
- () => array(size + 1).fill(0)
- );
-
- for(let i = 1; i <= len; i++) {
- for(let j = 0; j <= size; j++) {
- if(wight[i - 1] <= j) {
- dp[i][j] = math.max(
- dp[i - 1][j],
- value[i - 1] + dp[i - 1][j - wight[i - 1]]
- )
- } else {
- dp[i][j] = dp[i - 1][j];
- }
+function testWeightBagProblem (weight, value, size) {
+ // 定义 dp 数组
+ const len = weight.length,
+ dp = Array(len).fill().map(() => Array(size + 1).fill(0));
+
+ // 初始化
+ for(let j = weight[0]; j <= size; j++) {
+ dp[0][j] = value[0];
}
- }
-// console.table(dp);
-
- return dp[len][size];
-}
-
-function testWeightBagProblem2 (wight, value, size) {
- const len = wight.length,
- dp = Array(size + 1).fill(0);
- for(let i = 1; i <= len; i++) {
- for(let j = size; j >= wight[i - 1]; j--) {
- dp[j] = Math.max(dp[j], value[i - 1] + dp[j - wight[i - 1]]);
+ // weight 数组的长度len 就是物品个数
+ for(let i = 1; i < len; i++) { // 遍历物品
+ for(let j = 0; j <= size; j++) { // 遍历背包容量
+ if(j < weight[i]) dp[i][j] = dp[i - 1][j];
+ else dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]);
+ }
}
- }
- return dp[size];
-}
+ console.table(dp)
+
+ return dp[len - 1][size];
+}
function test () {
- console.log(testWeightBagProblem([1, 3, 4, 5], [15, 20, 30, 55], 6));
+ console.log(testWeightBagProblem([1, 3, 4, 5], [15, 20, 30, 55], 6));
}
test();
From 40457f0f44d3981dc071fd56871f66d7e5946af1 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Thu, 12 May 2022 16:16:48 +0800
Subject: [PATCH 2/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880121.=E4=B9=B0?=
=?UTF-8?q?=E5=8D=96=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6?=
=?UTF-8?q?=E6=9C=BA.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/0121.买卖股票的最佳时机.md | 40 ++++++++++++++++++++
1 file changed, 40 insertions(+)
diff --git a/problems/0121.买卖股票的最佳时机.md b/problems/0121.买卖股票的最佳时机.md
index f0bc3b97..a2498bb6 100644
--- a/problems/0121.买卖股票的最佳时机.md
+++ b/problems/0121.买卖股票的最佳时机.md
@@ -426,6 +426,46 @@ var maxProfit = function(prices) {
};
```
+TypeScript:
+
+> 贪心法
+
+```typescript
+function maxProfit(prices: number[]): number {
+ if (prices.length === 0) return 0;
+ let buy: number = prices[0];
+ let profitMax: number = 0;
+ for (let i = 1, length = prices.length; i < length; i++) {
+ profitMax = Math.max(profitMax, prices[i] - buy);
+ buy = Math.min(prices[i], buy);
+ }
+ return profitMax;
+};
+```
+
+> 动态规划
+
+```typescript
+function maxProfit(prices: number[]): number {
+ /**
+ dp[i][0]: 第i天持有股票的最大现金
+ dp[i][1]: 第i天不持有股票的最大现金
+ */
+ const length = prices.length;
+ if (length === 0) return 0;
+ const dp: number[][] = [];
+ dp[0] = [-prices[0], 0];
+ for (let i = 1; i < length; i++) {
+ dp[i] = [];
+ dp[i][0] = Math.max(dp[i - 1][0], -prices[i]);
+ dp[i][1] = Math.max(dp[i - 1][0] + prices[i], dp[i - 1][1]);
+ }
+ return dp[length - 1][1];
+};
+```
+
+
+
-----------------------
From 8ba775d04acd100e6cdac8ebd913aaead67fae15 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Thu, 12 May 2022 16:40:54 +0800
Subject: [PATCH 3/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880122.=E4=B9=B0?=
=?UTF-8?q?=E5=8D=96=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6?=
=?UTF-8?q?=E6=9C=BAII=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92.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
---
...票的最佳时机II(动态规划).md | 36 +++++++++++++++++++
1 file changed, 36 insertions(+)
diff --git a/problems/0122.买卖股票的最佳时机II(动态规划).md b/problems/0122.买卖股票的最佳时机II(动态规划).md
index 5a165a14..12b21fde 100644
--- a/problems/0122.买卖股票的最佳时机II(动态规划).md
+++ b/problems/0122.买卖股票的最佳时机II(动态规划).md
@@ -295,6 +295,42 @@ const maxProfit = (prices) => {
}
```
+TypeScript:
+
+> 动态规划
+
+```typescript
+function maxProfit(prices: number[]): number {
+ /**
+ dp[i][0]: 第i天持有股票
+ dp[i][1]: 第i天不持有股票
+ */
+ const length: number = prices.length;
+ if (length === 0) return 0;
+ const dp: number[][] = new Array(length).fill(0).map(_ => []);
+ dp[0] = [-prices[0], 0];
+ for (let i = 1; i < length; i++) {
+ dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] - prices[i]);
+ dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + prices[i]);
+ }
+ return dp[length - 1][1];
+};
+```
+
+> 贪心法
+
+```typescript
+function maxProfit(prices: number[]): number {
+ let resProfit: number = 0;
+ for (let i = 1, length = prices.length; i < length; i++) {
+ if (prices[i] > prices[i - 1]) {
+ resProfit += prices[i] - prices[i - 1];
+ }
+ }
+ return resProfit;
+};
+```
+
-----------------------
From 0a58ef986de4c20c5d69a4b490b478f44b7f3f5c Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 12 May 2022 17:35:30 +0800
Subject: [PATCH 4/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200142.=E7=8E=AF?=
=?UTF-8?q?=E5=BD=A2=E9=93=BE=E8=A1=A8II.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0142.环形链表II.md | 26 +++++++++++++++++++++++++-
1 file changed, 25 insertions(+), 1 deletion(-)
diff --git a/problems/0142.环形链表II.md b/problems/0142.环形链表II.md
index e8ca950d..f8e62d45 100644
--- a/problems/0142.环形链表II.md
+++ b/problems/0142.环形链表II.md
@@ -370,7 +370,31 @@ ListNode *detectCycle(ListNode *head) {
}
```
-
+Scala:
+```scala
+object Solution {
+ def detectCycle(head: ListNode): ListNode = {
+ var fast = head // 快指针
+ var slow = head // 慢指针
+ while (fast != null && fast.next != null) {
+ fast = fast.next.next // 快指针一次走两步
+ slow = slow.next // 慢指针一次走一步
+ // 如果相遇,fast快指针回到头
+ if (fast == slow) {
+ fast = head
+ // 两个指针一步一步的走,第一次相遇的节点必是入环节点
+ while (fast != slow) {
+ fast = fast.next
+ slow = slow.next
+ }
+ return fast
+ }
+ }
+ // 如果fast指向空值,必然无环返回null
+ null
+ }
+}
+```
-----------------------
From a25409d7ec351aaf2e6e513e3e091fc97cd17d60 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 12 May 2022 19:16:20 +0800
Subject: [PATCH 5/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200242.=E6=9C=89?=
=?UTF-8?q?=E6=95=88=E7=9A=84=E5=AD=97=E6=AF=8D=E5=BC=82=E4=BD=8D=E8=AF=8D?=
=?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/0242.有效的字母异位词.md | 25 +++++++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git a/problems/0242.有效的字母异位词.md b/problems/0242.有效的字母异位词.md
index 080166fd..4f0d143e 100644
--- a/problems/0242.有效的字母异位词.md
+++ b/problems/0242.有效的字母异位词.md
@@ -307,6 +307,31 @@ impl Solution {
}
}
```
+
+Scala:
+```scala
+object Solution {
+ def isAnagram(s: String, t: String): Boolean = {
+ // 如果两个字符串的长度不等,直接返回false
+ if (s.length != t.length) return false
+ val record = new Array[Int](26) // 记录每个单词出现了多少次
+ // 遍历字符串,对于s字符串单词对应的记录+=1,t字符串对应的记录-=1
+ for (i <- 0 until s.length) {
+ record(s(i) - 97) += 1
+ record(t(i) - 97) -= 1
+ }
+ // 如果不等于则直接返回false
+ for (i <- 0 until 26) {
+ if (record(i) != 0) {
+ return false
+ }
+ }
+ // 如果前面不返回false,说明匹配成功,返回true,return可以省略
+ true
+ }
+}
+```
+
## 相关题目
* 383.赎金信
From a71f7d2127bae83cc42dffd20b4c460865006d15 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Thu, 12 May 2022 23:23:59 +0800
Subject: [PATCH 6/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880123.=E4=B9=B0?=
=?UTF-8?q?=E5=8D=96=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6?=
=?UTF-8?q?=E6=9C=BAIII.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
---
.../0123.买卖股票的最佳时机III.md | 30 +++++++++++++++++++
1 file changed, 30 insertions(+)
diff --git a/problems/0123.买卖股票的最佳时机III.md b/problems/0123.买卖股票的最佳时机III.md
index 56ade343..67c99497 100644
--- a/problems/0123.买卖股票的最佳时机III.md
+++ b/problems/0123.买卖股票的最佳时机III.md
@@ -352,6 +352,36 @@ const maxProfit = prices => {
};
```
+TypeScript:
+
+> 版本一
+
+```typescript
+function maxProfit(prices: number[]): number {
+ /**
+ dp[i][0]: 无操作;
+ dp[i][1]: 第一次买入;
+ dp[i][2]: 第一次卖出;
+ dp[i][3]: 第二次买入;
+ dp[i][4]: 第二次卖出;
+ */
+ const length: number = prices.length;
+ if (length === 0) return 0;
+ const dp: number[][] = new Array(length).fill(0)
+ .map(_ => new Array(5).fill(0));
+ dp[0][1] = -prices[0];
+ dp[0][3] = -prices[0];
+ for (let i = 1; i < length; i++) {
+ dp[i][0] = dp[i - 1][0];
+ dp[i][1] = Math.max(dp[i - 1][1], -prices[i]);
+ dp[i][2] = Math.max(dp[i - 1][2], dp[i - 1][1] + prices[i]);
+ dp[i][3] = Math.max(dp[i - 1][3], dp[i - 1][2] - prices[i]);
+ dp[i][4] = Math.max(dp[i - 1][4], dp[i - 1][3] + prices[i]);
+ }
+ return Math.max(dp[length - 1][2], dp[length - 1][4]);
+};
+```
+
Go:
> 版本一: