LeetCode *** 209. Minimum Size Subarray Sum

来源:互联网 发布:java电脑版86安装包 编辑:程序博客网 时间:2024/05/01 04:47

题目:

Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead.

For example, given the array [2,3,1,2,4,3] and s = 7,
the subarray [4,3] has the minimal length under the problem constraint.


分析:

我没做nlog(n)的那部分。


代码:

class Solution {public:    int minSubArrayLen(int s, vector<int>& nums) {        int start=0,res=INT_MAX,sum=0;        for(int i=0;i<nums.size();++i){            sum+=nums[i];            while(sum>=s){                    res=min(res,i-start+1);                    sum-=nums[start++];            }        }        return res==INT_MAX?0:res;    }};

0 0
原创粉丝点击