Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 981d032

Browse files
raof01azl397985856
authored andcommitted
feat: azl397985856#209: add C++ implementation (azl397985856#89)
1 parent a5e592f commit 981d032

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

‎problems/209.minimum-size-subarray-sum.md‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ If you have figured out the O(n) solution, try coding another solution of which
3131

3232
## 代码
3333

34+
* 语言支持:JS,C++
35+
36+
JavaScript Code:
37+
3438
```js
3539
/*
3640
* @lc app=leetcode id=209 lang=javascript
@@ -93,6 +97,26 @@ var minSubArrayLen = function(s, nums) {
9397
};
9498
```
9599

100+
C++ Code:
101+
```C++
102+
class Solution {
103+
public:
104+
int minSubArrayLen(int s, vector<int>& nums) {
105+
int num_len= nums.size();
106+
int left=0, right=0, total=0, min_len= num_len+1;
107+
while (right < num_len) {
108+
do {
109+
total += nums[right++];
110+
} while (right < num_len && total < s);
111+
while (left < right && total - nums[left] >= s) total -= nums[left++];
112+
if (total >=s && min_len > right - left)
113+
min_len = right- left;
114+
}
115+
return min_len <= num_len ? min_len: 0;
116+
}
117+
};
118+
```
119+
96120
## 扩展
97121
98122
如果题目要求是 sum = s, 而不是 sum >= s 呢?

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /