From 27f8efa24c9d43b911fc7c05a7182508d50365cd Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Mon, 21 Nov 2022 23:12:33 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=9C=A8=E8=AE=BE=E8=AE=A1=E9=93=BE?= =?UTF-8?q?=E8=A1=A8=E7=9A=84C++=E4=BB=A3=E7=A0=81=E9=83=A8=E5=88=86?= =?UTF-8?q?=EF=BC=8CaddAtIndex=E6=96=B9=E6=B3=95=E4=B8=AD=EF=BC=8C?= =?UTF-8?q?=E9=92=88=E5=AF=B9index=20<=200=20=E7=9A=84=E6=83=85=E5=86=B5?= =?UTF-8?q?=E7=BA=A0=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0707.设计链表.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/problems/0707.设计链表.md b/problems/0707.设计链表.md index 1264983b..9461e263 100644 --- a/problems/0707.设计链表.md +++ b/problems/0707.设计链表.md @@ -108,9 +108,12 @@ public: // 如果index大于链表的长度,则返回空 // 如果index小于0,则置为0,作为链表的新头节点。 void addAtIndex(int index, int val) { - if (index > _size || index < 0) { + if (index > _size) { return; } + if (index < 0) { + index = 0; + } LinkedNode* newNode = new LinkedNode(val); LinkedNode* cur = _dummyHead; while(index--) { From cbe3bcf50a8633b2c07ff92b86b7bd9780d62848 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 22 Nov 2022 10:13:52 +0800 Subject: [PATCH 2/3] =?UTF-8?q?update=20027.=E7=A7=BB=E9=99=A4=E5=85=83?= =?UTF-8?q?=E7=B4=A0=20python=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0027.移除元素.md | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md index e1de7243..a563a13f 100644 --- a/problems/0027.移除元素.md +++ b/problems/0027.移除元素.md @@ -199,21 +199,15 @@ Python: ```python3 class Solution: def removeElement(self, nums: List[int], val: int) -> int: - if nums is None or len(nums)==0: - return 0 - l=0 - r=len(nums)-1 - while l Date: Tue, 22 Nov 2022 22:27:35 +0800 Subject: [PATCH 3/3] =?UTF-8?q?update=200209.=E9=95=BF=E5=BA=A6=E6=9C=80?= =?UTF-8?q?=E5=B0=8F=E7=9A=84=E5=AD=90=E6=95=B0=E7=BB=84=20python,=20js=20?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0209.长度最小的子数组.md | 66 ++++++++--------------- 1 file changed, 23 insertions(+), 43 deletions(-) diff --git a/problems/0209.长度最小的子数组.md b/problems/0209.长度最小的子数组.md index c912c259..220630f2 100644 --- a/problems/0209.长度最小的子数组.md +++ b/problems/0209.长度最小的子数组.md @@ -169,39 +169,18 @@ Python: ```python class Solution: def minSubArrayLen(self, s: int, nums: List[int]) -> int: - # 定义一个无限大的数 - res = float("inf") - Sum = 0 - index = 0 - for i in range(len(nums)): - Sum += nums[i] + res = float("inf") # 定义一个无限大的数 + Sum = 0 # 滑动窗口数值之和 + i = 0 # 滑动窗口起始位置 + for j in range(len(nums)): + Sum += nums[j] while Sum >= s: - res = min(res, i-index+1) - Sum -= nums[index] - index += 1 - return 0 if res==float("inf") else res -``` -```python -# 滑动窗口 -class Solution: - def minSubArrayLen(self, target: int, nums: List[int]) -> int: - if nums is None or len(nums) == 0: - return 0 - lenf = len(nums) + 1 - total = 0 - i = j = 0 - while (j < len(nums)): - total = total + nums[j] - j += 1 - while (total >= target): - lenf = min(lenf, j - i) - total = total - nums[i] + res = min(res, j-i+1) + Sum -= nums[i] i += 1 - if lenf == len(nums) + 1: - return 0 - else: - return lenf + return 0 if res == float("inf") else res ``` + Go: ```go func minSubArrayLen(target int, nums []int) int { @@ -232,22 +211,23 @@ func minSubArrayLen(target int, nums []int) int { JavaScript: ```js - var minSubArrayLen = function(target, nums) { - // 长度计算一次 - const len = nums.length; - let l = r = sum = 0, - res = len + 1; // 子数组最大不会超过自身 - while(r < len) { - sum += nums[r++]; - // 窗口滑动 - while(sum >= target) { - // r始终为开区间 [l, r) - res = res < r - l ? res : r - l; - sum-=nums[l++]; + let start, end + start = end = 0 + let sum = 0 + let len = nums.length + let ans = Infinity + + while(end < len){ + sum += nums[end]; + while (sum >= target) { + ans = Math.min(ans, end - start + 1); + sum -= nums[start]; + start++; } + end++; } - return res > len ? 0 : res; + return ans === Infinity ? 0 : ans }; ```