forked from moranzcw/LeetCode-NOTES
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.cpp
More file actions
26 lines (26 loc) · 695 Bytes
/
solution.cpp
File metadata and controls
26 lines (26 loc) · 695 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution
{
public:
int minSubArrayLen(int s, vector<int>& nums)
{
int length = nums.size()+1;
int startIndex = 0, endIndex = 0;
int sum = 0;
while(endIndex < nums.size())
{
while(endIndex < nums.size() && sum < s)
{
sum += nums[endIndex];
endIndex += 1;
}
while(startIndex < endIndex && sum >= s)
{
if( sum >= s)
length = min(length,endIndex - startIndex);
sum -= nums[startIndex];
startIndex += 1;
}
}
return length <= nums.size()? length:0;
}
};