leetcode 209: Minimum Size Subarray Sum

来源:互联网 发布:股票软件源码下载 编辑:程序博客网 时间:2024/05/23 01:14

Setting a window on the array. Loop the end of the window, whenever a sum of all numbers in the window is found bigger than or equal to s, shrink that window from the start side to make the window size minimum.

class Solution {public:    int minSubArrayLen(int s, vector<int>& nums) {        int start,end,sum=0,res=INT_MAX;        int n=nums.size();        for(start=0,end=0;end<n;end++)        {            sum+=nums[end];            if(sum>=s)            {                while(sum-nums[start]>=s)                {                    sum-=nums[start];                    start++;                }                if(end-start+1<res)                    res=end-start+1;                sum-=nums[start];//make the window move faster                start++;            }        }        if(res==INT_MAX)            return 0;        return res;    }};


0 0
原创粉丝点击