[LeetCode]209. Minimum Size Subarray Sum

来源:互联网 发布:淘宝账户被盗用 编辑:程序博客网 时间:2024/06/18 15:08

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

找到子数组的和大于等于s的最短子数组





非常简单,找到满足条件的边界条件,不断update

public class Solution {    public int minSubArrayLen(int s, int[] nums) {        if (nums == null || nums.length == 0) {            return 0;        }        int beg = 0;        int end = 0;        int sum = 0;        int len = Integer.MAX_VALUE;        while (end < nums.length) {            sum += nums[end++];            while (sum >= s) {                len = Math.min(len, end - beg);                sum -= nums[beg++];            }        }        return len == Integer.MAX_VALUE ? 0 : len;    }}


0 0
原创粉丝点击