刷题记录第二十一天-长度最小的子数组

#include
#include
using namespace std;
int main(){
	vector<int> nums({1,4,4});
	int n = nums.size();
	int left = 0;
	int right = 0;
	int sum = 0;
	int result = INT_MAX;
	int target = 4; 
	while(right<n){
		sum = sum + nums[right];
		while(sum>=target&&left<=right){
			result = min(result,right-left+1);
			sum = sum-nums[left];
			left++;
		}
		right++;
	}
	if(result==INT_MAX)result=0;
	cout<<result;
	system("pause");
}

你可能感兴趣的:(每日一题,算法,c++,数据结构)