leetcode--- Minimum Size Subarray Sum

来源:互联网 发布:网络招聘网 编辑:程序博客网 时间:2024/05/16 18:21

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.

class Solution {public:    int minSubArrayLen(int s, vector<int>& nums) {        int n = nums.size();        if(n == 0)            return 0;        int min = n + 1;        int sum = 0;        for(int i=0; i<n; i++)        {            sum = 0;            for(int j=i; j<n; j++)            {                sum += nums[j];                if(sum >= s)                {                    if(j - i + 1 < min)                    {                        min = j - i + 1;                    }                    break;                }            }        }        if(min == n + 1)            return 0;        else            return min;    }};



 

0 0
原创粉丝点击