[LeetCode]209. Minimum Size Subarray Sum

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;
    }
}


發佈了363 篇原創文章 · 獲贊 1 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章