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 1/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=881005.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=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 --- ...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 2/3] =?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 3/3] =?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++; }