Minimum Size Subarray Sum(和大于S的最小子数组)

http://www.lintcode.com/zh-cn/problem/minimum-size-subarray-sum/?rand=true
请参阅 Longest Substring Without Repeating Characters(最长无重复字符的子串)

public class Solution {
    /*
     * @param nums: an array of integers
     * @param s: An integer
     * @return: an integer representing the minimum size of subarray
     */
    public int minimumSize(int[] nums, int s) {
        // write your code here
        int j = 0;
        int res = Integer.MAX_VALUE;
        int temp = 0;
        int sum = 0;
        for (int i = 0; i < nums.length; i++) {
            while (j < nums.length && sum < s) {
                sum += nums[j];
                j++;
                temp++;
            }
            if (sum >= s) {
                res = Math.min(temp, res);
            }
            sum -= nums[i];
            temp--;
        }
        return res == Integer.MAX_VALUE ? -1 : res;
    }
}

你可能感兴趣的:(Minimum Size Subarray Sum(和大于S的最小子数组))