Java concise solution with O(n) complexity


  • 7
    A
    public int minSubArrayLen(int s, int[] nums) {
        if (nums == null || nums.length == 0) {
    	    return 0;
        }
        int minLen = Integer.MAX_VALUE;
        int start = 0;
        int localSum = 0;
        for (int end = 0; end < nums.length; end++) {
    	    localSum += nums[end];
    	    while (localSum >= s) {
    		    minLen = Math.min(minLen, end - start + 1);
    		    localSum -= nums[start++];
    	    }
        }
        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.