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 01/24] =?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 02/24] =?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 03/24] =?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 04/24] =?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 05/24] =?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 06/24] =?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 07/24] =?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 08/24] =?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 09/24] =?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 10/24] =?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: Sun, 10 Apr 2022 21:59:21 +0800 Subject: [PATCH 11/24] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880406.=E6=A0=B9?= =?UTF-8?q?=E6=8D=AE=E8=BA=AB=E9=AB=98=E9=87=8D=E5=BB=BA=E9=98=9F=E5=88=97?= =?UTF-8?q?.md=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/0406.根据身高重建队列.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/problems/0406.根据身高重建队列.md b/problems/0406.根据身高重建队列.md index b2354d09..ecb05301 100644 --- a/problems/0406.根据身高重建队列.md +++ b/problems/0406.根据身高重建队列.md @@ -290,6 +290,24 @@ var reconstructQueue = function(people) { }; ``` +### TypeScript + +```typescript +function reconstructQueue(people: number[][]): number[][] { + people.sort((a, b) => { + if (a[0] === b[0]) return a[1] - b[1]; + return b[0] - a[0]; + }); + const resArr: number[][] = []; + for (let i = 0, length = people.length; i < length; i++) { + resArr.splice(people[i][1], 0, people[i]); + } + return resArr; +}; +``` + + + -----------------------
From ef44e750cec20980095e63db3c090c8df48f620c Mon Sep 17 00:00:00 2001 From: Guanzhong Pan Date: Mon, 11 Apr 2022 20:34:32 +0100 Subject: [PATCH 12/24] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200376.=E6=91=86?= =?UTF-8?q?=E5=8A=A8=E5=BA=8F=E5=88=97.md=20C=E8=AF=AD=E8=A8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0376.摆动序列.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/problems/0376.摆动序列.md b/problems/0376.摆动序列.md index 5076c9ad..6bd2277f 100644 --- a/problems/0376.摆动序列.md +++ b/problems/0376.摆动序列.md @@ -298,5 +298,32 @@ var wiggleMaxLength = function(nums) { }; ``` +### C +**贪心** +```c +int wiggleMaxLength(int* nums, int numsSize){ + if(numsSize <= 1) + return numsSize; + + int length = 1; + int preDiff , curDiff; + preDiff = curDiff = 0; + for(int i = 0; i < numsSize - 1; ++i) { + // 计算当前i元素与i+1元素差值 + curDiff = nums[i+1] - nums[i]; + + // 若preDiff与curDiff符号不符,则子序列长度+1。更新preDiff的符号 + // 若preDiff与curDiff符号一致,当前i元素为连续升序/连续降序子序列的中间元素。不被记录入长度 + // 注:当preDiff为0时,curDiff为正或为负都属于符号不同 + if((curDiff > 0 && preDiff <= 0) || (preDiff >= 0 && curDiff < 0)) { + preDiff = curDiff; + length++; + } + } + + return length; +} +``` + -----------------------
From ae38a29068241636052e831d96b1e446dddd979e Mon Sep 17 00:00:00 2001 From: Jamcy123 <1219502823@qq.com> Date: Tue, 12 Apr 2022 16:57:25 +0800 Subject: [PATCH 13/24] =?UTF-8?q?=E4=BF=AE=E6=94=B9=200503.=E4=B8=8B?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E6=9B=B4=E5=A4=A7=E5=85=83=E7=B4=A0II=20java?= =?UTF-8?q?script=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0503.下一个更大元素II.md | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/problems/0503.下一个更大元素II.md b/problems/0503.下一个更大元素II.md index 36e183e1..d6d3850f 100644 --- a/problems/0503.下一个更大元素II.md +++ b/problems/0503.下一个更大元素II.md @@ -166,20 +166,19 @@ JavaScript: * @return {number[]} */ var nextGreaterElements = function (nums) { - // let map = new Map(); + const len = nums.length; let stack = []; - let res = new Array(nums.length).fill(-1); - for (let i = 0; i < nums.length * 2; i++) { + let res = Array(len).fill(-1); + for (let i = 0; i < len * 2; i++) { while ( stack.length && - nums[i % nums.length] > nums[stack[stack.length - 1]] + nums[i % len] > nums[stack[stack.length - 1]] ) { - let index = stack.pop(); - res[index] = nums[i % nums.length]; + const index = stack.pop(); + res[index] = nums[i % len]; } - stack.push(i % nums.length); + stack.push(i % len); } - return res; }; ``` From 82ab1706f88e390ff2f13478cd7771cc92e76754 Mon Sep 17 00:00:00 2001 From: Jamcy123 <1219502823@qq.com> Date: Tue, 12 Apr 2022 17:09:20 +0800 Subject: [PATCH 14/24] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E4=B8=80=E3=80=81=E5=A2=9E=E5=8A=A0=E7=89=88=E6=9C=AC=E4=BA=8C?= =?UTF-8?q?=200739.=E6=AF=8F=E6=97=A5=E6=B8=A9=E5=BA=A6=20javascript=20?= =?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/0739.每日温度.md | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/problems/0739.每日温度.md b/problems/0739.每日温度.md index 710f5eb6..5f53e412 100644 --- a/problems/0739.每日温度.md +++ b/problems/0739.每日温度.md @@ -301,25 +301,22 @@ func dailyTemperatures(num []int) []int { JavaScript: ```javascript -/** - * @param {number[]} temperatures - * @return {number[]} - */ +// 版本一 var dailyTemperatures = function(temperatures) { - let n = temperatures.length; - let res = new Array(n).fill(0); - let stack = []; // 递减栈:用于存储元素右面第一个比他大的元素下标 + const n = temperatures.length; + const res = Array(n).fill(0); + const stack = []; // 递增栈:用于存储元素右面第一个比他大的元素下标 stack.push(0); for (let i = 1; i < n; i++) { // 栈顶元素 - let top = stack[stack.length - 1]; + const top = stack[stack.length - 1]; if (temperatures[i] < temperatures[top]) { stack.push(i); } else if (temperatures[i] === temperatures[top]) { stack.push(i); } else { while (stack.length && temperatures[i] > temperatures[stack[stack.length - 1]]) { - let top = stack.pop(); + const top = stack.pop(); res[top] = i - top; } stack.push(i); @@ -327,6 +324,23 @@ var dailyTemperatures = function(temperatures) { } return res; }; + + +// 版本二 +var dailyTemperatures = function(temperatures) { + const n = temperatures.length; + const res = Array(n).fill(0); + const stack = []; // 递增栈:用于存储元素右面第一个比他大的元素下标 + stack.push(0); + for (let i = 1; i < n; i++) { + while (stack.length && temperatures[i] > temperatures[stack[stack.length - 1]]) { + const top = stack.pop(); + res[top] = i - top; + } + stack.push(i); + } + return res; +}; ``` From 54d0d9d1c1a579e0a8750f1be864ad78b8d13c45 Mon Sep 17 00:00:00 2001 From: Guanzhong Pan Date: Tue, 12 Apr 2022 10:43:46 +0100 Subject: [PATCH 15/24] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200053.=E6=9C=80?= =?UTF-8?q?=E5=A4=A7=E5=AD=90=E6=95=B0=E7=BB=84=E5=92=8C.md=20C=E8=AF=AD?= =?UTF-8?q?=E8=A8=80=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0053.最大子序和.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/problems/0053.最大子序和.md b/problems/0053.最大子序和.md index 3d11c91e..9af02edf 100644 --- a/problems/0053.最大子序和.md +++ b/problems/0053.最大子序和.md @@ -230,6 +230,24 @@ var maxSubArray = function(nums) { }; ``` +### C: +```c +int maxSubArray(int* nums, int numsSize){ + int maxVal = INT_MIN; + int subArrSum = 0; + + int i; + for(i = 0; i < numsSize; ++i) { + subArrSum += nums[i]; + // 若当前局部和大于之前的最大结果,对结果进行更新 + maxVal = subArrSum > maxVal ? subArrSum : maxVal; + // 若当前局部和为负,对结果无益。则从nums[i+1]开始应重新计算。 + subArrSum = subArrSum < 0 ? 0 : subArrSum; + } + + return maxVal; +} +``` ----------------------- From af724ef6d5ed95c4ba545a932f8257b487648aec Mon Sep 17 00:00:00 2001 From: xuerbujia <83055661+xuerbujia@users.noreply.github.com> Date: Tue, 12 Apr 2022 17:45:50 +0800 Subject: [PATCH 16/24] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880143.=E9=87=8D?= =?UTF-8?q?=E6=8E=92=E9=93=BE=E8=A1=A8.md=EF=BC=89=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0143.重排链表.md | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/problems/0143.重排链表.md b/problems/0143.重排链表.md index 00622623..790bcb48 100644 --- a/problems/0143.重排链表.md +++ b/problems/0143.重排链表.md @@ -336,7 +336,33 @@ class Solution: return pre ``` ### Go - +```go +# 方法三 分割链表 +func reorderList(head *ListNode) { + var slow=head + var fast=head + for fast!=nil&&fast.Next!=nil{ + slow=slow.Next + fast=fast.Next.Next + } //双指针将链表分为左右两部分 + var right =new(ListNode) + for slow!=nil{ + temp:=slow.Next + slow.Next=right.Next + right.Next=slow + slow=temp + } //翻转链表右半部分 + right=right.Next //right为反转后得右半部分 + h:=head + for right.Next!=nil{ + temp:=right.Next + right.Next=h.Next + h.Next=right + h=h.Next.Next + right=temp + } //将左右两部分重新组合 +} +``` ### JavaScript ```javascript From dd63a865717b0a439cc2ce2b5abab0a55d18d7de Mon Sep 17 00:00:00 2001 From: Guanzhong Pan Date: Tue, 12 Apr 2022 11:01:53 +0100 Subject: [PATCH 17/24] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200053.=E6=9C=80?= =?UTF-8?q?=E5=A4=A7=E5=AD=90=E6=95=B0=E7=BB=84=E5=92=8C.md=20C=E8=AF=AD?= =?UTF-8?q?=E8=A8=80=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0053.最大子序和.md | 34 ++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/problems/0053.最大子序和.md b/problems/0053.最大子序和.md index 9af02edf..3d4459a3 100644 --- a/problems/0053.最大子序和.md +++ b/problems/0053.最大子序和.md @@ -231,6 +231,7 @@ var maxSubArray = function(nums) { ``` ### C: +贪心: ```c int maxSubArray(int* nums, int numsSize){ int maxVal = INT_MIN; @@ -249,6 +250,39 @@ int maxSubArray(int* nums, int numsSize){ } ``` +动态规划: +```c +/** + * 解题思路:动态规划: + * 1. dp数组:dp[i]表示从0到i的子序列中最大序列和的值 + * 2. 递推公式:dp[i] = max(dp[i-1] + nums[i], nums[i]) + 若dp[i-1]<0,对最后结果无益。dp[i]则为nums[i]。 + * 3. dp数组初始化:dp[0]的最大子数组和为nums[0] + * 4. 推导顺序:从前往后遍历 + */ + +#define max(a, b) (((a) > (b)) ? (a) : (b)) + +int maxSubArray(int* nums, int numsSize){ + int dp[numsSize]; + // dp[0]最大子数组和为nums[0] + dp[0] = nums[0]; + // 若numsSize为1,应直接返回nums[0] + int subArrSum = nums[0]; + + int i; + for(i = 1; i < numsSize; ++i) { + dp[i] = max(dp[i - 1] + nums[i], nums[i]); + + // 若dp[i]大于之前记录的最大值,进行更新 + if(dp[i] > subArrSum) + subArrSum = dp[i]; + } + + return subArrSum; +} +``` + -----------------------
From 1b935259a6c83beab9f730059da4bec509bb1f61 Mon Sep 17 00:00:00 2001 From: Guanzhong Pan Date: Tue, 12 Apr 2022 21:01:23 +0100 Subject: [PATCH 18/24] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200122.=E4=B9=B0?= =?UTF-8?q?=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA?= =?UTF-8?q?II.md=20C=E8=AF=AD=E8=A8=80=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92?= =?UTF-8?q?=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0122.买卖股票的最佳时机II.md | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/problems/0122.买卖股票的最佳时机II.md b/problems/0122.买卖股票的最佳时机II.md index 83b852c6..1b84f728 100644 --- a/problems/0122.买卖股票的最佳时机II.md +++ b/problems/0122.买卖股票的最佳时机II.md @@ -269,7 +269,7 @@ const maxProfit = (prices) => { ``` C: - +贪心: ```c int maxProfit(int* prices, int pricesSize){ int result = 0; @@ -284,5 +284,27 @@ int maxProfit(int* prices, int pricesSize){ } ``` +动态规划: +```c +#define max(a, b) (((a) > (b)) ? (a) : (b)) + +int maxProfit(int* prices, int pricesSize){ + int dp[pricesSize][2]; + dp[0][0] = 0 - prices[0]; + dp[0][1] = 0; + + int i; + for(i = 1; i < pricesSize; ++i) { + // dp[i][0]为i-1天持股的钱数/在第i天用i-1天的钱买入的最大值。 + // 若i-1天持股,且第i天买入股票比i-1天持股时更亏,说明应在i-1天时持股 + dp[i][0] = max(dp[i-1][0], dp[i-1][1] - prices[i]); + //dp[i][1]为i-1天不持股钱数/在第i天卖出所持股票dp[i-1][0] + prices[i]的最大值 + dp[i][1] = max(dp[i-1][1], dp[i-1][0] + prices[i]); + } + // 返回在最后一天不持股时的钱数(将股票卖出后钱最大化) + return dp[pricesSize - 1][1]; +} +``` + -----------------------
From e1abedc13ad4d5f744ecc27fa9931aa24508364b Mon Sep 17 00:00:00 2001 From: Guanzhong Pan Date: Wed, 13 Apr 2022 09:41:50 +0100 Subject: [PATCH 19/24] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200055.=E8=B7=B3?= =?UTF-8?q?=E8=B7=83=E6=B8=B8=E6=88=8F.md=20C=E8=AF=AD=E8=A8=80=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/0055.跳跃游戏.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/problems/0055.跳跃游戏.md b/problems/0055.跳跃游戏.md index c0890f75..4ba0c03b 100644 --- a/problems/0055.跳跃游戏.md +++ b/problems/0055.跳跃游戏.md @@ -154,6 +154,28 @@ var canJump = function(nums) { }; ``` +### C +```c +#define max(a, b) (((a) > (b)) ? (a) : (b)) + +bool canJump(int* nums, int numsSize){ + int cover = 0; + + int i; + // 只可能获取cover范围中的步数,所以i<=cover + for(i = 0; i <= cover; ++i) { + // 更新cover为从i出发能到达的最大值/cover的值中较大值 + cover = max(i + nums[i], cover); + + // 若更新后cover可以到达最后的元素,返回true + if(cover >= numsSize - 1) + return true; + } + + return false; +} +``` + -----------------------
From b73a7da7585b03b42ab51515f5463259ac2e0631 Mon Sep 17 00:00:00 2001 From: Guanzhong Pan Date: Fri, 15 Apr 2022 18:25:41 +0100 Subject: [PATCH 20/24] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=201005.K=E6=AC=A1?= =?UTF-8?q?=E5=8F=96=E5=8F=8D=E5=90=8E=E6=9C=80=E5=A4=A7=E5=8C=96=E7=9A=84?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E5=92=8C.md=20C=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...1005.K次取反后最大化的数组和.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/problems/1005.K次取反后最大化的数组和.md b/problems/1005.K次取反后最大化的数组和.md index 45f186e2..0d122d91 100644 --- a/problems/1005.K次取反后最大化的数组和.md +++ b/problems/1005.K次取反后最大化的数组和.md @@ -211,5 +211,44 @@ var largestSumAfterKNegations = function(nums, k) { }; ``` +### C +```c +#define abs(a) (((a) > 0) ? (a) : (-(a))) + +// 对数组求和 +int sum(int *nums, int numsSize) { + int sum = 0; + + int i; + for(i = 0; i < numsSize; ++i) { + sum += nums[i]; + } + return sum; +} + +int cmp(const void* v1, const void* v2) { + return abs(*(int*)v2) - abs(*(int*)v1); +} + +int largestSumAfterKNegations(int* nums, int numsSize, int k){ + qsort(nums, numsSize, sizeof(int), cmp); + + int i; + for(i = 0; i < numsSize; ++i) { + // 遍历数组,若当前元素<0则将当前元素转变,k-- + if(nums[i] < 0 && k > 0) { + nums[i] *= -1; + --k; + } + } + + // 若遍历完数组后k还有剩余(此时所有元素应均为正),则将绝对值最小的元素nums[numsSize - 1]变为负 + if(k % 2 == 1) + nums[numsSize - 1] *= -1; + + return sum(nums, numsSize); +} +``` + -----------------------
From 84f41f6685e744b138890cf7143ed3069a02a78e Mon Sep 17 00:00:00 2001 From: Guanzhong Pan Date: Tue, 19 Apr 2022 17:00:17 +0100 Subject: [PATCH 21/24] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200135.=E5=88=86?= =?UTF-8?q?=E5=8F=91=E7=B3=96=E6=9E=9C.md=20C=E8=AF=AD=E8=A8=80=E8=A7=A3?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0135.分发糖果.md | 41 +++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/problems/0135.分发糖果.md b/problems/0135.分发糖果.md index ccdabc16..27071aee 100644 --- a/problems/0135.分发糖果.md +++ b/problems/0135.分发糖果.md @@ -238,6 +238,47 @@ var candy = function(ratings) { }; ``` +### C +```c +#define max(a, b) (((a) > (b)) ? (a) : (b)) + +int *initCandyArr(int size) { + int *candyArr = (int*)malloc(sizeof(int) * size); + + int i; + for(i = 0; i < size; ++i) + candyArr[i] = 1; + + return candyArr; +} + +int candy(int* ratings, int ratingsSize){ + // 初始化数组,每个小孩开始至少有一颗糖 + int *candyArr = initCandyArr(ratingsSize); + + int i; + // 先判断右边是否比左边评分高。若是,右边孩子的糖果为左边孩子+1(candyArr[i] = candyArr[i - 1] + 1) + for(i = 1; i < ratingsSize; ++i) { + if(ratings[i] > ratings[i - 1]) + candyArr[i] = candyArr[i - 1] + 1; + } + + // 再判断左边评分是否比右边高。 + // 若是,左边孩子糖果为右边孩子糖果+1/自己所持糖果最大值。(若糖果已经比右孩子+1多,则不需要更多糖果) + // 举例:ratings为[1, 2, 3, 1]。此时评分为3的孩子在判断右边比左边大后为3,虽然它比最末尾的1(ratings[3])大,但是candyArr[3]为1。所以不必更新candyArr[2] + for(i = ratingsSize - 2; i >= 0; --i) { + if(ratings[i] > ratings[i + 1]) + candyArr[i] = max(candyArr[i], candyArr[i + 1] + 1); + } + + // 求出糖果之和 + int result = 0; + for(i = 0; i < ratingsSize; ++i) { + result += candyArr[i]; + } + return result; +} +``` -----------------------
From be4051893888427b41b172135b21add3fa096579 Mon Sep 17 00:00:00 2001 From: Jamcy123 <1219502823@qq.com> Date: Fri, 22 Apr 2022 15:34:06 +0800 Subject: [PATCH 22/24] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=201047.=20=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E5=AD=97=E7=AC=A6=E4=B8=B2=E4=B8=AD=E7=9A=84=E6=89=80?= =?UTF-8?q?=E6=9C=89=E7=9B=B8=E9=82=BB=E9=87=8D=E5=A4=8D=E9=A1=B9=20?= =?UTF-8?q?=E5=8F=8C=E6=8C=87=E9=92=88=E8=A7=A3=E6=B3=95=20javascript?= =?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 | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/problems/1047.删除字符串中的所有相邻重复项.md b/problems/1047.删除字符串中的所有相邻重复项.md index 54455f62..638c8f4e 100644 --- a/problems/1047.删除字符串中的所有相邻重复项.md +++ b/problems/1047.删除字符串中的所有相邻重复项.md @@ -250,11 +250,9 @@ func removeDuplicates(s string) string { javaScript: +法一:使用栈 + ```js -/** - * @param {string} s - * @return {string} - */ var removeDuplicates = function(s) { const stack = []; for(const x of s) { @@ -267,6 +265,25 @@ var removeDuplicates = function(s) { }; ``` +法二:双指针(模拟栈) + +```js +// 原地解法(双指针模拟栈) +var removeDuplicates = function(s) { + s = [...s]; + let top = -1; // 指向栈顶元素的下标 + for(let i = 0; i < s.length; i++) { + if(top === -1 || s[top] !== s[i]) { // top === -1 即空栈 + s[++top] = s[i]; // 入栈 + } else { + top--; // 推出栈 + } + } + s.length = top + 1; // 栈顶元素下标 + 1 为栈的长度 + return s.join(''); +}; +``` + TypeScript: ```typescript From ce28066ae92780a3829883b387dc004bc76df57d Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Wed, 4 May 2022 18:06:13 +0800 Subject: [PATCH 23/24] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=B5=84=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1d7f219d..501c5ed4 100644 --- a/README.md +++ b/README.md @@ -543,6 +543,7 @@ **来看看就知道了,你会发现相见恨晚!** - -
+ + +
From aacbbfbd02613a7110e913fa00298ef0734107ac Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Wed, 4 May 2022 18:11:46 +0800 Subject: [PATCH 24/24] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=B5=84=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 501c5ed4..38418322 100644 --- a/README.md +++ b/README.md @@ -531,7 +531,8 @@ 如果是已工作,备注:姓名-城市-岗位-组队刷题。如果学生,备注:姓名-学校-年级-组队刷题。**备注没有自我介绍不通过哦** -
+ +
@@ -545,5 +546,5 @@ -
+