LeetCode209

public class Solution {
    /*
    * @param 
    
    */
    
    public int minSubArrayLen(int s, int[] nums) {
        if(nums == null || nums.length == 1)
            return 0;
        int head = 0;
        int tail = 0;
        int sum = 0;
        int result = nums.length;
        boolean exists = false;
        
        //Here's the main loop.
        while(tail <= nums.length){
            if(sum >= s){
                exists = true;
                if(tail - head == 1)
                    return 1;
                result = Math.min(result, tail - head);
                sum -= nums[head];
                head++;
            }else{
                if(tail == nums.length)
                    break;
                sum += nums[tail];
                tail++;
            }
        }
        if(exists)
            return result;
        else
            return 0;

    }
}
 
  

 
  
 
 

你可能感兴趣的:(LeetCode209)