设为首页 加入收藏

TOP

leetcode 209 : Minimum Size Subarray Sum
2015-11-21 01:02:24 来源: 作者: 【 】 浏览:1
Tags:leetcode 209 Minimum Size Subarray Sum

Minimum Size Subarray Sum

Total Accepted: 1954 Total Submissions: 8526

Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead.

For example, given the array [2,3,1,2,4,3] and s = 7,
the subarray [4,3] has the minimal length under the problem constraint.

[思路]

两个指针, start end, end向后走,直到 sum 大于 s. 然后start向后, 直到sum 小于s. 同时更新 min值.

?

?

public class Solution {
    //1,1,4
    public int minSubArrayLen(int s, int[] nums) {
        //init check
        int start = 0;
        int end = 0;
        
        int sum = 0;
        int min = Integer.MAX_VALUE;
        
        while(start=s && start<=end) {
                min = Math.min(min, end-start);
                sum -= nums[start++];
            }
        }
        return min==Integer.MAX_VALUE ? 0 : min;
    }
}

?

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇POJ - 2828 - Buy Tickets (线段.. 下一篇C++散列表二次探测

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: