Last active
July 18, 2022 08:06
-
-
Save icameling/0f29fb2efa9edee0994f1f68905f231c to your computer and use it in GitHub Desktop.
#数组 #长度最小的子数组 #leetcode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int minSubArrayLen(int target, vector<int>& nums) { | |
int sub_len = nums.size() + 1; | |
int i = 0; | |
int sum = 0; | |
for (int j = 0; j < nums.size(); ++j) { | |
sum += nums[j]; | |
while (sum >= target){ | |
int len = j - i + 1; | |
sub_len = len < sub_len ? len : sub_len; | |
sum -= nums[i++]; | |
} | |
} | |
return sub_len < nums.size() + 1 ? sub_len : 0; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment