leetcode Minimum Size Subarray Sum

题目链接

思路:
这个思路很难说清楚。但是代码很简单。时间复杂度是n

public class Solution {
    public int minSubArrayLen(int s, int[] nums) {
         int left=0;
        int right=-1;
        int currentSum=0;
        int currentLength=0;
        int minLength=Integer.MAX_VALUE;
        while(right<nums.length)
        {

            if(currentSum>=s)
            {
                minLength=minLength>currentLength?currentLength:minLength;
                currentSum-=nums[left];
                left++;
                currentLength--;
            }
            else
            {
                right++;
                currentLength++;
                if(right<nums.length)
                {
                    currentSum+=nums[right];
                }
            }

        }


        if(minLength==Integer.MAX_VALUE)
        {
            return 0;
        }
        else
        {
            return minLength;
        }
    }
}

你可能感兴趣的:(leetcode Minimum Size Subarray Sum)