Loading

[LeetCode]长度最小的子数组

题目

 

代码 

class Solution {
public:
    int minSubArrayLen(int s, vector<int>& nums) {

       if(nums.empty())
           return 0;
        int start=0,end=0,size=nums.size(),sum=nums[0],record=INT_MAX;

        while(start<size)
        {
            while(sum<s)
            {
                end++;
                if(end==size)
                    return record==INT_MAX?0:record;
                sum+=nums[end];
            }
            record=min(record,end-start+1);
            sum-=nums[start];
            start++;
            
            
        }
        return record;
    }
};

 

posted @ 2019-02-23 09:57  李正浩  阅读(146)  评论(0编辑  收藏  举报