mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
添加(0209.长度最小的子数组.md):增加typescript版本
This commit is contained in:
@ -214,6 +214,28 @@ var minSubArrayLen = function(target, nums) {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Typescript:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
function minSubArrayLen(target: number, nums: number[]): number {
|
||||||
|
let left: number = 0, right: number = 0;
|
||||||
|
let res: number = nums.length + 1;
|
||||||
|
let sum: number = 0;
|
||||||
|
while (right < nums.length) {
|
||||||
|
sum += nums[right];
|
||||||
|
if (sum >= target) {
|
||||||
|
// 不断移动左指针,直到不能再缩小为止
|
||||||
|
while (sum - nums[left] >= target) {
|
||||||
|
sum -= nums[left++];
|
||||||
|
}
|
||||||
|
res = Math.min(res, right - left + 1);
|
||||||
|
}
|
||||||
|
right++;
|
||||||
|
}
|
||||||
|
return res === nums.length + 1 ? 0 : res;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
Swift:
|
Swift:
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
|
Reference in New Issue
Block a user