forked from gongluck/CVIP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
209.长度最小的子数组.cpp
46 lines (44 loc) · 1.05 KB
/
209.长度最小的子数组.cpp
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
36
37
38
39
40
41
42
43
44
45
46
/*
* @lc app=leetcode.cn id=209 lang=cpp
*
* [209] 长度最小的子数组
*/
// @lc code=start
class Solution
{
public:
int minSubArrayLen(int target, vector<int> &nums)
{
// int min = INT_MAX;
// for (int i = 0; i < nums.size(); ++i)
// {
// int sum = 0;
// for (int j = i; j < nums.size(); ++j)
// {
// sum += nums[j];
// if (sum >= target)
// {
// min = std::min(min, j - i + 1);
// break;
// }
// }
// }
// return min == INT_MAX ? 0 : min;
//滑动窗口
int begin = 0;
int end = 0;
int sum = 0;
int min = INT_MAX;
for (; end < nums.size(); ++end)
{
sum += nums[end];
while (sum >= target)
{
min = std::min(min, end - begin + 1);
sum -= nums[begin++];
}
}
return min == INT_MAX ? 0 : min;
}
};
// @lc code=end