mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
Merge pull request #1773 from juguagua/leetcode-modify-the-code-of-the-array
更新数组部分,0209.长度最小的子数组 python, js 代码
This commit is contained in:
@ -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
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
@ -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
|
i += 1
|
||||||
return 0 if res == float("inf") else res
|
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
|
|
||||||
if lenf == len(nums) + 1:
|
|
||||||
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){
|
||||||
|
sum += nums[end];
|
||||||
while (sum >= target) {
|
while (sum >= target) {
|
||||||
// r始终为开区间 [l, r)
|
ans = Math.min(ans, end - start + 1);
|
||||||
res = res < r - l ? res : r - l;
|
sum -= nums[start];
|
||||||
sum-=nums[l++];
|
start++;
|
||||||
}
|
}
|
||||||
|
end++;
|
||||||
}
|
}
|
||||||
return res > len ? 0 : res;
|
return ans === Infinity ? 0 : ans
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -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--) {
|
||||||
|
Reference in New Issue
Block a user