Skip to content

Latest commit

 

History

History
27 lines (24 loc) · 901 Bytes

算法-数组-滑动窗口-最小连续子数组.md

File metadata and controls

27 lines (24 loc) · 901 Bytes

https://leetcode.com/problems/minimum-size-subarray-sum/

给定一个整形数组和一个数字s,找到数组中最短的一个连续子数组,使得连续子数组的数字和sum>=s,返回这个最短的连续子数组的长度值

class Solution {
    public int minSubArrayLen(int s, int[] nums) {
        int i = 0, j = -1, sum = 0, minLength = Integer.MAX_VALUE;
        while(i < nums.length){
            if(j + 1 < nums.length && sum < s){
                sum+=nums[++j];
            }else{
                sum-=nums[i++];
            }
            if(sum >= s){
                minLength = Math.min(j-i+1, minLength);
            }
        }
        if(minLength == Integer.MAX_VALUE){
            return 0;
        }
        return minLength;
    }
}

欢迎光临我的博客,发现更多技术资源~