Minimum Size Subarray Sum

暂时理解不了二分那个算法

O(n)

public class Solution {

    public int minSubArrayLen(int s, int[] nums) {

        // http://bookshadow.com/weblog/2015/05/12/leetcode-minimum-size-subarray-sum/

        // O(n)

        int m=nums.length+1, sum=0, len =nums.length, l=0, r=0;

        if(nums==null||nums.length==0) return 0;

        while(true){

            if(sum<s){

                if(r>=len)

                    break;

                sum+=nums[r];

                r++;

            }else{

                if(l>r)

                    break;

                m = Math.min(m,r-l);

                sum -= nums[l];

                l++;

            }

        }

        return m<=len? m:0;

        

        

    }

    

}

ref http://bookshadow.com/weblog/2015/05/12/leetcode-minimum-size-subarray-sum/

你可能感兴趣的:(array)