首页 > 编程语言 > 详细

LeetCode——长度最小的子数组

时间:2020-07-08 17:27:38      阅读:54      评论:0      收藏:0      [点我收藏+]
public int minSubArrayLen(int s, int[] nums) {
        if( 0 == nums.length  || null == nums)
        {
            return 0;
        }
        int  l = 0 ;
        int r = 0;
        int result =  Integer.MAX_VALUE;
        int cur = nums[0];
        while(l < nums.length)
        {
            if(cur < s &&  r < nums.length -1)
            {
                r++;
                cur += nums[r];
            }
            else 
            {
                if(cur >= s ){
                    result = Math.min(result , r - l + 1);                   
                }
                cur -= nums[l];
                l++;

            }
        }
        return result == Integer.MAX_VALUE ? 0 : result;

    }

维护一个滑动窗口。

LeetCode——长度最小的子数组

原文:https://www.cnblogs.com/swqblog/p/13267919.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!