Merge pull request #1773 from juguagua/leetcode-modify-the-code-of-the-array

更新数组部分,0209.长度最小的子数组 python, js 代码
This commit is contained in:
程序员Carl
2022-11-23 10:02:25 +08:00
committed by GitHub
3 changed files with 36 additions and 59 deletions

View File

@ -199,21 +199,15 @@ Python
```python3 ```python3
class Solution: class Solution:
def removeElement(self, nums: List[int], val: int) -> int: def removeElement(self, nums: List[int], val: int) -> int:
if nums is None or len(nums)==0: # 快指针遍历元素
return 0 fast = 0
l=0 # 慢指针记录位置
r=len(nums)-1 slow = 0
while l<r: for fast in range(len(nums)):
while(l<r and nums[l]!=val): if nums[fast] != val:
l+=1 nums[slow] = nums[fast]
while(l<r and nums[r]==val): slow += 1
r-=1 return slow
nums[l], nums[r]=nums[r], nums[l]
print(nums)
if nums[l]==val:
return l
else:
return l+1
``` ```

View File

@ -169,39 +169,18 @@ Python
```python ```python
class Solution: class Solution:
def minSubArrayLen(self, s: int, nums: List[int]) -> int: def minSubArrayLen(self, s: int, nums: List[int]) -> int:
# 定义一个无限大的数 res = float("inf") # 定义一个无限大的数
res = float("inf") Sum = 0 # 滑动窗口数值之和
Sum = 0 i = 0 # 滑动窗口起始位置
index = 0 for j in range(len(nums)):
for i in range(len(nums)): Sum += nums[j]
Sum += nums[i]
while Sum >= s: while Sum >= s:
res = min(res, i-index+1) res = min(res, j-i+1)
Sum -= nums[index] Sum -= nums[i]
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]
i += 1 i += 1
if lenf == len(nums) + 1: return 0 if res == float("inf") else res
return 0
else:
return lenf
``` ```
Go Go
```go ```go
func minSubArrayLen(target int, nums []int) int { func minSubArrayLen(target int, nums []int) int {
@ -232,22 +211,23 @@ func minSubArrayLen(target int, nums []int) int {
JavaScript: JavaScript:
```js ```js
var minSubArrayLen = function(target, nums) { var minSubArrayLen = function(target, nums) {
// 长度计算一次 let start, end
const len = nums.length; start = end = 0
let l = r = sum = 0, let sum = 0
res = len + 1; // 子数组最大不会超过自身 let len = nums.length
while(r < len) { let ans = Infinity
sum += nums[r++];
// 窗口滑动 while(end < len){
while(sum >= target) { sum += nums[end];
// r始终为开区间 [l, r) while (sum >= target) {
res = res < r - l ? res : r - l; ans = Math.min(ans, end - start + 1);
sum-=nums[l++]; sum -= nums[start];
start++;
} }
end++;
} }
return res > len ? 0 : res; return ans === Infinity ? 0 : ans
}; };
``` ```

View File

@ -108,9 +108,12 @@ public:
// 如果index大于链表的长度则返回空 // 如果index大于链表的长度则返回空
// 如果index小于0则置为0作为链表的新头节点。 // 如果index小于0则置为0作为链表的新头节点。
void addAtIndex(int index, int val) { void addAtIndex(int index, int val) {
if (index > _size || index < 0) { if (index > _size) {
return; return;
} }
if (index < 0) {
index = 0;
}
LinkedNode* newNode = new LinkedNode(val); LinkedNode* newNode = new LinkedNode(val);
LinkedNode* cur = _dummyHead; LinkedNode* cur = _dummyHead;
while(index--) { while(index--) {