Java Solution with 1ms, easy to understand.


  • 0
    public class Solution {
    public int minSubArrayLen(int s, int[] nums) {
        if(nums.length==0) return 0;
        int i=0,j=0;
        int minLen=Integer.MAX_VALUE;
        int sum=nums[i];
        while(i<=j){
            if(sum<s){    
                if(j==nums.length-1) break;  // check if j has reached the tail of the array.
                sum=sum+nums[++j];   //move towards j
                
            }
            else{
                int len=j-i+1;
                minLen=minLen<len?minLen:len;
                sum=sum-nums[i++];
            }
            
        }
        return minLen==Integer.MAX_VALUE?0:minLen;
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.