From 92c6b3609fb85036f68039fd20ac52c8c919fba0 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Wed, 6 Apr 2022 22:46:46 +0800 Subject: [PATCH 01/18] =?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.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/0122.买卖股票的最佳时机II.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/problems/0122.买卖股票的最佳时机II.md b/problems/0122.买卖股票的最佳时机II.md index 83b852c6..1e4d017c 100644 --- a/problems/0122.买卖股票的最佳时机II.md +++ b/problems/0122.买卖股票的最佳时机II.md @@ -268,6 +268,18 @@ const maxProfit = (prices) => { }; ``` +TypeScript: + +```typescript +function maxProfit(prices: number[]): number { + let resProfit: number = 0; + for (let i = 1, length = prices.length; i < length; i++) { + resProfit += Math.max(prices[i] - prices[i - 1], 0); + } + return resProfit; +}; +``` + C: ```c From 1e6ec2d32a5cb21ba049646979b23ed948f46444 Mon Sep 17 00:00:00 2001 From: zhuye Date: Wed, 6 Apr 2022 23:05:01 +0800 Subject: [PATCH 02/18] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E3=80=900707-=E8=AE=BE?= =?UTF-8?q?=E8=AE=A1=E9=93=BE=E8=A1=A8.md=E3=80=91TypeScript=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/0707.设计链表.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/problems/0707.设计链表.md b/problems/0707.设计链表.md index 86f3a683..c5a55574 100644 --- a/problems/0707.设计链表.md +++ b/problems/0707.设计链表.md @@ -973,6 +973,9 @@ class MyLinkedList { // 处理头节点 if (index === 0) { this.head = this.head!.next; + if (index === this.size - 1) { + this.tail = null + } this.size--; return; } From 6e09456aefe7981d272e355c28e4ab43597ac5b1 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Wed, 6 Apr 2022 23:23:22 +0800 Subject: [PATCH 03/18] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880055.=E8=B7=B3?= =?UTF-8?q?=E8=B7=83=E6=B8=B8=E6=88=8F.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/0055.跳跃游戏.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/problems/0055.跳跃游戏.md b/problems/0055.跳跃游戏.md index c0890f75..94614242 100644 --- a/problems/0055.跳跃游戏.md +++ b/problems/0055.跳跃游戏.md @@ -154,6 +154,23 @@ var canJump = function(nums) { }; ``` +### TypeScript + +```typescript +function canJump(nums: number[]): boolean { + let farthestIndex: number = 0; + let cur: number = 0; + while (cur <= farthestIndex) { + farthestIndex = Math.max(farthestIndex, cur + nums[cur]); + if (farthestIndex >= nums.length - 1) return true; + cur++; + } + return false; +}; +``` + + + -----------------------
From 31800730a2634f0a40c99075d277acc251c1634a Mon Sep 17 00:00:00 2001 From: zhuye Date: Wed, 6 Apr 2022 23:30:18 +0800 Subject: [PATCH 04/18] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E3=80=900707-=E8=AE=BE?= =?UTF-8?q?=E8=AE=A1=E9=93=BE=E8=A1=A8.md=E3=80=91TypeScript=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/0707.设计链表.md | 1 + 1 file changed, 1 insertion(+) diff --git a/problems/0707.设计链表.md b/problems/0707.设计链表.md index c5a55574..37ce15ad 100644 --- a/problems/0707.设计链表.md +++ b/problems/0707.设计链表.md @@ -973,6 +973,7 @@ class MyLinkedList { // 处理头节点 if (index === 0) { this.head = this.head!.next; + // 如果链表中只有一个元素,删除头节点后,需要处理尾节点 if (index === this.size - 1) { this.tail = null } From f70acb963a499e3a627d2358a4119b24649d74d7 Mon Sep 17 00:00:00 2001 From: speed <771935730@qq.com> Date: Thu, 7 Apr 2022 00:03:02 +0800 Subject: [PATCH 05/18] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200763.=E5=88=92?= =?UTF-8?q?=E5=88=86=E5=AD=97=E6=AF=8D=E5=8C=BA=E9=97=B4.md=20=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E7=B1=BB=E4=BC=BC=E5=BC=95=E7=88=86=E6=B0=94=E7=90=83?= =?UTF-8?q?=E7=9A=84=E6=80=9D=E8=B7=AF=E4=BB=A5=E5=8F=8A=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0763.划分字母区间.md | 47 +++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/problems/0763.划分字母区间.md b/problems/0763.划分字母区间.md index 03d3a73b..d3958bd5 100644 --- a/problems/0763.划分字母区间.md +++ b/problems/0763.划分字母区间.md @@ -77,6 +77,53 @@ public: 但这道题目的思路是很巧妙的,所以有必要介绍给大家做一做,感受一下。 +## 补充 + +这里提供一种与[452.用最少数量的箭引爆气球](https://programmercarl.com/0452.用最少数量的箭引爆气球.html)、[435.无重叠区间](https://programmercarl.com/0435.无重叠区间.html)相同的思路。 + +统计字符串中所有字符的起始和结束位置,记录这些区间(实际上也就是[435.无重叠区间](https://programmercarl.com/0435.无重叠区间.html)题目里的输入),**将区间按左边界从小到大排序,找到边界将区间划分成组,互不重叠。找到的边界就是答案。** + +```CPP +class Solution { +public: + static bool cmp(vector &a, vector &b) { + return a[0] < b[0]; + } + // 记录每个字母出现的区间 + void countLabels(string s, vector> &hash) { + for (int i = 0; i < s.size(); ++i) { + if (hash[s[i] - 'a'][0] == INT_MIN) { + hash[s[i] - 'a'][0] = i; + } + hash[s[i] - 'a'][1] = i; + } + } + vector partitionLabels(string s) { + vector res; + vector> hash(26, vector(2, INT_MIN)); + countLabels(s, hash); + // 按照左边界从小到大排序 + sort(hash.begin(), hash.end(), cmp); + // 记录最大右边界 + int rightBoard = INT_MIN; + int leftBoard = 0; + for (int i = 0; i < hash.size(); ++i) { + // 过滤掉字符串中没有的字母 + if (hash[i][0] == INT_MIN) { + continue; + } + // 一旦下一区间左边界大于当前右边界,即可认为出现分割点 + if (rightBoard != INT_MIN && hash[i][0] > rightBoard) { + res.push_back(rightBoard - leftBoard + 1); + leftBoard = hash[i][0]; + } + rightBoard = max(rightBoard, hash[i][1]); + } + res.push_back(rightBoard - leftBoard + 1); + return res; + } +}; +``` ## 其他语言版本 From 3048b00d72146838275c37ec549b76eeca8813a9 Mon Sep 17 00:00:00 2001 From: speed Date: Thu, 7 Apr 2022 10:52:27 +0800 Subject: [PATCH 06/18] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BA=860763.=E5=88=92?= =?UTF-8?q?=E5=88=86=E5=AD=97=E6=AF=8D=E5=8C=BA=E9=97=B4.md=20=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E8=B4=AA=E5=BF=83=E6=80=9D=E8=B7=AF=E7=9A=84=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0763.划分字母区间.md | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/problems/0763.划分字母区间.md b/problems/0763.划分字母区间.md index d3958bd5..d350f255 100644 --- a/problems/0763.划分字母区间.md +++ b/problems/0763.划分字母区间.md @@ -90,35 +90,43 @@ public: return a[0] < b[0]; } // 记录每个字母出现的区间 - void countLabels(string s, vector> &hash) { + vector> countLabels(string s) { + vector> hash(26, vector(2, INT_MIN)); + vector> hash_filter; for (int i = 0; i < s.size(); ++i) { if (hash[s[i] - 'a'][0] == INT_MIN) { hash[s[i] - 'a'][0] = i; } hash[s[i] - 'a'][1] = i; } + // 去除字符串中未出现的字母所占用区间 + for (int i = 0; i < hash.size(); ++i) { + if (hash[i][0] != INT_MIN) { + hash_filter.push_back(hash[i]); + } + } + return hash_filter; } vector partitionLabels(string s) { vector res; - vector> hash(26, vector(2, INT_MIN)); - countLabels(s, hash); + // 这一步得到的 hash 即为无重叠区间题意中的输入样例格式:区间列表 + // 只不过现在我们要求的是区间分割点 + vector> hash = countLabels(s); // 按照左边界从小到大排序 sort(hash.begin(), hash.end(), cmp); // 记录最大右边界 - int rightBoard = INT_MIN; + int rightBoard = hash[0][1]; int leftBoard = 0; - for (int i = 0; i < hash.size(); ++i) { - // 过滤掉字符串中没有的字母 - if (hash[i][0] == INT_MIN) { - continue; - } + for (int i = 1; i < hash.size(); ++i) { + // 由于字符串一定能分割,因此, // 一旦下一区间左边界大于当前右边界,即可认为出现分割点 - if (rightBoard != INT_MIN && hash[i][0] > rightBoard) { + if (hash[i][0] > rightBoard) { res.push_back(rightBoard - leftBoard + 1); leftBoard = hash[i][0]; } rightBoard = max(rightBoard, hash[i][1]); } + // 最右端 res.push_back(rightBoard - leftBoard + 1); return res; } From 3e856447424fd1a4cd37ed7b54331fea33fec588 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Thu, 7 Apr 2022 15:25:48 +0800 Subject: [PATCH 07/18] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880045.=E8=B7=B3?= =?UTF-8?q?=E8=B7=83=E6=B8=B8=E6=88=8FII.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/0045.跳跃游戏II.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/problems/0045.跳跃游戏II.md b/problems/0045.跳跃游戏II.md index 7a3f048c..4caff042 100644 --- a/problems/0045.跳跃游戏II.md +++ b/problems/0045.跳跃游戏II.md @@ -250,6 +250,27 @@ var jump = function(nums) { }; ``` +### TypeScript + +```typescript +function jump(nums: number[]): number { + const length: number = nums.length; + let curFarthestIndex: number = 0, + nextFarthestIndex: number = 0; + let curIndex: number = 0; + let stepNum: number = 0; + while (curIndex < length - 1) { + nextFarthestIndex = Math.max(nextFarthestIndex, curIndex + nums[curIndex]); + if (curIndex === curFarthestIndex) { + curFarthestIndex = nextFarthestIndex; + stepNum++; + } + curIndex++; + } + return stepNum; +}; +``` + From 38deed9e52058872eb522a8d629a265ae78df1e2 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Thu, 7 Apr 2022 15:58:32 +0800 Subject: [PATCH 08/18] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=881005.K?= =?UTF-8?q?=E6=AC=A1=E5=8F=96=E5=8F=8D=E5=90=8E=E6=9C=80=E5=A4=A7=E5=8C=96?= =?UTF-8?q?=E7=9A=84=E6=95=B0=E7=BB=84=E5=92=8C.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 --- ...1005.K次取反后最大化的数组和.md | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/1005.K次取反后最大化的数组和.md b/problems/1005.K次取反后最大化的数组和.md index 45f186e2..80c47147 100644 --- a/problems/1005.K次取反后最大化的数组和.md +++ b/problems/1005.K次取反后最大化的数组和.md @@ -211,5 +211,29 @@ var largestSumAfterKNegations = function(nums, k) { }; ``` +### TypeScript + +```typescript +function largestSumAfterKNegations(nums: number[], k: number): number { + nums.sort((a, b) => Math.abs(b) - Math.abs(a)); + let curIndex: number = 0; + const length = nums.length; + while (curIndex < length && k > 0) { + if (nums[curIndex] < 0) { + nums[curIndex] *= -1; + k--; + } + curIndex++; + } + while (k > 0) { + nums[length - 1] *= -1; + k--; + } + return nums.reduce((pre, cur) => pre + cur, 0); +}; +``` + + + -----------------------
From 82df90fd14fc8897559256a65d3ba53258daf262 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Thu, 7 Apr 2022 23:58:15 +0800 Subject: [PATCH 09/18] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880134.=E5=8A=A0?= =?UTF-8?q?=E6=B2=B9=E7=AB=99.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/0134.加油站.md | 45 +++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/problems/0134.加油站.md b/problems/0134.加油站.md index 1062a91c..d3b3d453 100644 --- a/problems/0134.加油站.md +++ b/problems/0134.加油站.md @@ -235,7 +235,7 @@ class Solution { return index; } } -``` +``` ### Python ```python @@ -340,7 +340,50 @@ var canCompleteCircuit = function(gas, cost) { }; ``` +### TypeScript + +**暴力法:** + +```typescript +function canCompleteCircuit(gas: number[], cost: number[]): number { + for (let i = 0, length = gas.length; i < length; i++) { + let curSum: number = 0; + let index: number = i; + while (curSum >= 0 && index < i + length) { + let tempIndex: number = index % length; + curSum += gas[tempIndex] - cost[tempIndex]; + index++; + } + if (index === i + length && curSum >= 0) return i; + } + return -1; +}; +``` + +**解法二:** + +```typescript +function canCompleteCircuit(gas: number[], cost: number[]): number { + let total: number = 0; + let curGas: number = 0; + let tempDiff: number = 0; + let resIndex: number = 0; + for (let i = 0, length = gas.length; i < length; i++) { + tempDiff = gas[i] - cost[i]; + total += tempDiff; + curGas += tempDiff; + if (curGas < 0) { + resIndex = i + 1; + curGas = 0; + } + } + if (total < 0) return -1; + return resIndex; +}; +``` + ### C + ```c int canCompleteCircuit(int* gas, int gasSize, int* cost, int costSize){ int curSum = 0; From 1496b59646e00b189bd3586145e1474d593b65cb Mon Sep 17 00:00:00 2001 From: mxdneu Date: Fri, 8 Apr 2022 00:17:47 +0800 Subject: [PATCH 10/18] =?UTF-8?q?fix=20js=E8=9E=BA=E6=97=8B=E6=95=B0?= =?UTF-8?q?=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0059.螺旋矩阵II.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/problems/0059.螺旋矩阵II.md b/problems/0059.螺旋矩阵II.md index 5c679982..a7b19a34 100644 --- a/problems/0059.螺旋矩阵II.md +++ b/problems/0059.螺旋矩阵II.md @@ -246,11 +246,11 @@ var generateMatrix = function(n) { res[row][col] = count++; } // 下行从右到左(左闭右开) - for (; col > startX; col--) { + for (; col > startY; col--) { res[row][col] = count++; } // 左列做下到上(左闭右开) - for (; row > startY; row--) { + for (; row > startX; row--) { res[row][col] = count++; } From 0ecc3bd7cfdea391e9725030442cad617c277989 Mon Sep 17 00:00:00 2001 From: sanwulol Date: Fri, 8 Apr 2022 17:00:59 +0800 Subject: [PATCH 11/18] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E3=80=900122-=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.md=E3=80=91javaScript=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0122.买卖股票的最佳时机II.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0122.买卖股票的最佳时机II.md b/problems/0122.买卖股票的最佳时机II.md index 83b852c6..1e6dc7c0 100644 --- a/problems/0122.买卖股票的最佳时机II.md +++ b/problems/0122.买卖股票的最佳时机II.md @@ -264,7 +264,7 @@ const maxProfit = (prices) => { dp[i][1] = Math.max(dp[i-1][1], dp[i-1][0] + prices[i]); } - return dp[prices.length -1][0]; + return dp[prices.length -1][1]; }; ``` From ff135662de4d1f282ead7f76c17a35806d99259e Mon Sep 17 00:00:00 2001 From: sanwulol Date: Fri, 8 Apr 2022 17:01:19 +0800 Subject: [PATCH 12/18] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E3=80=900122-=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.md(=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92)=E3=80=91ja?= =?UTF-8?q?vaScript=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0122.买卖股票的最佳时机II(动态规划).md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0122.买卖股票的最佳时机II(动态规划).md b/problems/0122.买卖股票的最佳时机II(动态规划).md index 615d79bb..5a165a14 100644 --- a/problems/0122.买卖股票的最佳时机II(动态规划).md +++ b/problems/0122.买卖股票的最佳时机II(动态规划).md @@ -276,7 +276,7 @@ const maxProfit = (prices) => { dp[i][1] = Math.max(dp[i-1][1], dp[i-1][0] + prices[i]); } - return dp[prices.length -1][0]; + return dp[prices.length -1][1]; }; // 方法二:动态规划(滚动数组) From 278bcb67b8250c0cea57b6a8f970e007d1259f23 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Fri, 8 Apr 2022 20:28:24 +0800 Subject: [PATCH 13/18] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880135.=E5=88=86?= =?UTF-8?q?=E5=8F=91=E7=B3=96=E6=9E=9C.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/0135.分发糖果.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/problems/0135.分发糖果.md b/problems/0135.分发糖果.md index ccdabc16..b8bdae0e 100644 --- a/problems/0135.分发糖果.md +++ b/problems/0135.分发糖果.md @@ -238,6 +238,32 @@ var candy = function(ratings) { }; ``` +### TypeScript + +```typescript +function candy(ratings: number[]): number { + const candies: number[] = []; + candies[0] = 1; + // 保证右边高分孩子一定比左边低分孩子发更多的糖果 + for (let i = 1, length = ratings.length; i < length; i++) { + if (ratings[i] > ratings[i - 1]) { + candies[i] = candies[i - 1] + 1; + } else { + candies[i] = 1; + } + } + // 保证左边高分孩子一定比右边低分孩子发更多的糖果 + for (let i = ratings.length - 2; i >= 0; i--) { + if (ratings[i] > ratings[i + 1]) { + candies[i] = Math.max(candies[i], candies[i + 1] + 1); + } + } + return candies.reduce((pre, cur) => pre + cur); +}; +``` + + + -----------------------
From 2f5996de0d7bcb91c615dbe9ff8e07cb339a1e42 Mon Sep 17 00:00:00 2001 From: Austin <40263822+LookCos@users.noreply.github.com> Date: Fri, 8 Apr 2022 21:01:24 +0800 Subject: [PATCH 14/18] =?UTF-8?q?Update=200416.=E5=88=86=E5=89=B2=E7=AD=89?= =?UTF-8?q?=E5=92=8C=E5=AD=90=E9=9B=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 错别字。 --- problems/0416.分割等和子集.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0416.分割等和子集.md b/problems/0416.分割等和子集.md index c8d9bc04..70f0cb51 100644 --- a/problems/0416.分割等和子集.md +++ b/problems/0416.分割等和子集.md @@ -50,7 +50,7 @@ ## 01背包问题 -背包问题,大家都知道,有N件物品和一个最多能被重量为W 的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。每件物品只能用一次,求解将哪些物品装入背包里物品价值总和最大。 +背包问题,大家都知道,有N件物品和一个最多能背重量为W 的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。每件物品只能用一次,求解将哪些物品装入背包里物品价值总和最大。 **背包问题有多种背包方式,常见的有:01背包、完全背包、多重背包、分组背包和混合背包等等。** From 2f3f35c5727cb9469e3c186f26217d8cb053e242 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Fri, 8 Apr 2022 21:33:51 +0800 Subject: [PATCH 15/18] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880860.=E6=9F=A0?= =?UTF-8?q?=E6=AA=AC=E6=B0=B4=E6=89=BE=E9=9B=B6.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 --- problems/0860.柠檬水找零.md | 34 ++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/problems/0860.柠檬水找零.md b/problems/0860.柠檬水找零.md index f48ecf4d..026c2e63 100644 --- a/problems/0860.柠檬水找零.md +++ b/problems/0860.柠檬水找零.md @@ -254,5 +254,39 @@ var lemonadeChange = function(bills) { ``` +### TypeScript + +```typescript +function lemonadeChange(bills: number[]): boolean { + let five: number = 0, + ten: number = 0; + for (let bill of bills) { + switch (bill) { + case 5: + five++; + break; + case 10: + if (five < 1) return false; + five--; + ten++ + break; + case 20: + if (ten > 0 && five > 0) { + five--; + ten--; + } else if (five > 2) { + five -= 3; + } else { + return false; + } + break; + } + } + return true; +}; +``` + + + -----------------------
From 0e1cbda7153b3b5b2892110696f10afe7833a38f Mon Sep 17 00:00:00 2001 From: "Neil.Liu" <88214924@qq.com> Date: Fri, 8 Apr 2022 23:56:49 +0800 Subject: [PATCH 16/18] =?UTF-8?q?Update=200121.=E5=8D=96=E8=82=A1=E7=A5=A8?= =?UTF-8?q?=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA.md=20=E8=B4=AA?= =?UTF-8?q?=E5=BF=83=E4=BB=A5=E5=8F=8A=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92?= =?UTF-8?q?=20Go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0121.买卖股票的最佳时机.md | 52 ++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/problems/0121.买卖股票的最佳时机.md b/problems/0121.买卖股票的最佳时机.md index e7c0ac65..f0bc3b97 100644 --- a/problems/0121.买卖股票的最佳时机.md +++ b/problems/0121.买卖股票的最佳时机.md @@ -311,7 +311,36 @@ class Solution: ``` Go: +> 贪心法: +```Go +func maxProfit(prices []int) int { + low := math.MaxInt32 + rlt := 0 + for i := range prices{ + low = min(low, prices[i]) + rlt = max(rlt, prices[i]-low) + } + return rlt +} +func min(a, b int) int { + if a < b{ + return a + } + + return b +} + +func max(a, b int) int { + if a > b{ + return a + } + + return b +} +``` + +> 动态规划:版本一 ```Go func maxProfit(prices []int) int { length:=len(prices) @@ -338,6 +367,29 @@ func max(a,b int)int { } ``` +> 动态规划:版本二 +```Go +func maxProfit(prices []int) int { + dp := [2][2]int{} + dp[0][0] = -prices[0] + dp[0][1] = 0 + for i := 1; i < len(prices); i++{ + dp[i%2][0] = max(dp[(i-1)%2][0], -prices[i]) + dp[i%2][1] = max(dp[(i-1)%2][1], dp[(i-1)%2][0]+prices[i]) + } + + return dp[(len(prices)-1)%2][1] +} + +func max(a, b int) int { + if a > b{ + return a + } + + return b +} +``` + JavaScript: > 动态规划 From 2e18054079ecfa1147bfc4335fd6c62609a44c37 Mon Sep 17 00:00:00 2001 From: jonathanx111 <57882619+jonathanx111@users.noreply.github.com> Date: Sat, 9 Apr 2022 13:49:55 -0400 Subject: [PATCH 17/18] =?UTF-8?q?=E7=BA=A0=E6=AD=A30027=E7=9A=84Swift?= =?UTF-8?q?=E7=AD=94=E6=A1=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Swift答案写错了。改成对的答案。 --- problems/0027.移除元素.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md index 8d6ca502..590cf0b9 100644 --- a/problems/0027.移除元素.md +++ b/problems/0027.移除元素.md @@ -281,10 +281,8 @@ func removeElement(_ nums: inout [Int], _ val: Int) -> Int { for fastIndex in 0.. Date: Tue, 26 Apr 2022 22:08:25 +0800 Subject: [PATCH 18/18] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E5=89=8D=E5=BA=8F.A?= =?UTF-8?q?CM=E6=A8=A1=E5=BC=8F=E5=A6=82=E4=BD=95=E6=9E=84=E5=BB=BA?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91.md=20Java=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../前序/ACM模式如何构建二叉树.md | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/problems/前序/ACM模式如何构建二叉树.md b/problems/前序/ACM模式如何构建二叉树.md index 28c4b6f7..07674b31 100644 --- a/problems/前序/ACM模式如何构建二叉树.md +++ b/problems/前序/ACM模式如何构建二叉树.md @@ -217,6 +217,59 @@ int main() { ## Java ```Java +public class Solution { + // 节点类 + static class TreeNode { + // 节点值 + int val; + + // 左节点 + TreeNode left; + + // 右节点 + TreeNode right; + + // 节点的构造函数(默认左右节点都为null) + public TreeNode(int x) { + this.val = x; + this.left = null; + this.right = null; + } + } + + /** + * 根据数组构建二叉树 + * @param arr 树的数组表示 + * @return 构建成功后树的根节点 + */ + public TreeNode constructBinaryTree(final int[] arr) { + // 构建和原数组相同的树节点列表 + List treeNodeList = arr.length > 0 ? new ArrayList<>(arr.length) : null; + TreeNode root = null; + // 把输入数值数组,先转化为二叉树节点列表 + for (int i = 0; i < arr.length; i++) { + TreeNode node = null; + if (arr[i] != -1) { // 用 -1 表示null + node = new TreeNode(arr[i]); + } + treeNodeList.add(node); + if (i == 0) { + root = node; + } + } + // 遍历一遍,根据规则左右孩子赋值就可以了 + // 注意这里 结束规则是 i * 2 + 2 < arr.length,避免空指针 + for (int i = 0; i * 2 + 2 < arr.length; i++) { + TreeNode node = treeNodeList.get(i); + if (node != null) { + // 线性存储转连式存储关键逻辑 + node.left = treeNodeList.get(2 * i + 1); + node.right = treeNodeList.get(2 * i + 2); + } + } + return root; + } +} ```