-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path209.cpp
More file actions
35 lines (32 loc) · 834 Bytes
/
209.cpp
File metadata and controls
35 lines (32 loc) · 834 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
27
28
29
30
31
32
33
34
35
//
// 209.cpp
// LeetCode
//
// Created by 张佐玮 on 15/6/2.
// Copyright (c) 2015年 JarvisZhang. All rights reserved.
//
// Title: Minimum Size Subarray Sum
//
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int minSubArrayLen(int s, vector<int>& nums) {
if (nums.empty()) {
return 0;
}
int maxLength = INT_MAX, start = 0, end = 0, sum = 0;
while (end < nums.size() || sum >= s) {
if (sum >= s) {
int currentLength = end - start;
maxLength = (currentLength < maxLength) ? currentLength : maxLength;
sum -= nums[start++];
}
else {
sum += nums[end++];
}
}
return (maxLength == INT_MAX) ? 0 : maxLength;
}
};