Skip to content

Commit

Permalink
feat: azl397985856#209: add C++ implementation (azl397985856#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
raof01 authored and azl397985856 committed Aug 8, 2019
1 parent a5e592f commit 981d032
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions problems/209.minimum-size-subarray-sum.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ If you have figured out the O(n) solution, try coding another solution of which

## 代码

* 语言支持:JS,C++

JavaScript Code:

```js
/*
* @lc app=leetcode id=209 lang=javascript
Expand Down Expand Up @@ -93,6 +97,26 @@ var minSubArrayLen = function(s, nums) {
};
```

C++ Code:
```C++
class Solution {
public:
int minSubArrayLen(int s, vector<int>& nums) {
int num_len= nums.size();
int left=0, right=0, total=0, min_len= num_len+1;
while (right < num_len) {
do {
total += nums[right++];
} while (right < num_len && total < s);
while (left < right && total - nums[left] >= s) total -= nums[left++];
if (total >=s && min_len > right - left)
min_len = right- left;
}
return min_len <= num_len ? min_len: 0;
}
};
```
## 扩展
如果题目要求是 sum = s, 而不是 sum >= s 呢?
Expand Down

0 comments on commit 981d032

Please sign in to comment.