简体   繁体   中英

why is it showing time limit exceeded?

Given an array of positive integers nums and a positive integer target, return the minimal length of a contiguous subarray [numsl, numsl+1, ..., numsr-1, numsr] of which the sum is greater than or equal to target. If there is no such subarray, return 0 instead.

 class Solution{
    public:
int minSubArrayLen(int target, vector<int> &nums)
{
    int right = 0;
    int left = 0;
    int size = nums.size();
    long sum = 0;
    int length = 100000;
    while (left < size)
    {
        sum = sum + nums[right];
        if (sum >= target)
        {
            length = min(length, right - left + 1);

            sum = sum - nums[left];
            left++;
            if (right != size)
                right++;
        }

        else if (right != size)
            right++;
    }
    return length;
}
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM