Skip to content

Instantly share code, notes, and snippets.

@icameling
Last active July 18, 2022 08:06
Show Gist options
  • Save icameling/0f29fb2efa9edee0994f1f68905f231c to your computer and use it in GitHub Desktop.
Save icameling/0f29fb2efa9edee0994f1f68905f231c to your computer and use it in GitHub Desktop.
#数组 #长度最小的子数组 #leetcode
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