算法系列——Minimum Size Subarray Sum
2017-08-10 21:17
253 查看
题目描述
Given an array of n positive integers and a positive integer s, findthe minimal length of a contiguous 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.
解题思路
双指针+滑动窗口双指针 l,r 维护当前序列和>=s的区间,然后更新区间最小值。
算法实现
public class Solution { public int minSubArrayLen(int s, int[] nums) { int l=0,r=-1; int sum=0; int result=nums.length+1; while(l<nums.length){ if(r+1<nums.length&&sum<s) sum+=nums[++r]; else sum-=nums[l++]; if(sum>=s) result=Math.min(result,r-l+1); } if(result==nums.length+1) return 0; return result; } }
相关文章推荐
- 第五周算法分析与设计:Minimum Size Subarray Sum
- 【LeetCode】Minimum Size Subarray Sum
- LeetCode Exercise 12:Minimum Size Subarray Sum
- LeetCode -- Minimum Size Subarray Sum
- [Array]Minimum Size Subarray Sum
- leetcode Minimum Size Subarray Sum
- Leetcode Minimum Size Subarray Sum
- 【Leetcode】Minimum Size Subarray Sum
- Minimum Size Subarray Sum
- 209. Minimum Size Subarray Sum
- leetcode 76 Minimum Window Substring/ 209 Minimum Size Subarray Sum
- Leetcode_Minimum Size Subarray Sum
- (leetcode)Minimum Size Subarray Sum
- [Array]Minimum Size Subarray Sum
- Minimum size subarray sum
- Minimum Size Subarray Sum
- [leetcode]Minimum Size Subarray Sum
- [leetcode题后感]Minimum Size Subarray Sum
- 【leetcode】Array—— Minimum Size Subarray Sum(209)
- *LeetCode-Minimum Size Subarray Sum