209. Minimum Size Subarray Sum
2017-06-20 16:32
344 查看
Given an array of n positive integers and a positive integer s, find the 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.
题意:与目标相等的最短子序列。用两个指针从头开始,当和sum小于s时,右指针+1,当和大于等于s,减去左指针的数,左指针+1
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.
题意:与目标相等的最短子序列。用两个指针从头开始,当和sum小于s时,右指针+1,当和大于等于s,减去左指针的数,左指针+1
class Solution { public: int minSubArrayLen(int s, vector<int>& nums) { int sz = nums.size(); if(sz == 0) return 0; int Min = 0; int sum = nums[0]; int l = 0, r = 0; while(l <= r){ if(sum >= s){ if(Min == 0) Min = r - l + 1; else Min = Min > (r - l + 1) ? (r - l + 1) : Min; sum -= nums[l]; ++l; } else { if(r < sz - 1){ ++r; sum += nums[r]; } else { break; } } } return Min; } };
相关文章推荐
- 209. Minimum Size Subarray Sum 【M】【35】
- [leetcode]209. Minimum Size Subarray Sum
- 【leetcode】209. Minimum Size Subarray Sum
- leetcode ----209. Minimum Size Subarray Sum
- 209. Minimum Size Subarray Sum
- 209. Minimum Size Subarray Sum**
- 209. Minimum Size Subarray Sum
- leetcode题解-209. Minimum Size Subarray Sum
- Middle-题目87:209. Minimum Size Subarray Sum
- leetcode209. Minimum Size Subarray Sum
- [LeetCode] 209. Minimum Size Subarray Sum
- LeetCode 209. Minimum Size Subarray Sum
- LeetCode 209. Minimum Size Subarray Sum
- leetcode 209. Minimum Size Subarray Sum 最短子数组的和 + 十分典型的移动窗口做法
- LeetCode 209. Minimum Size Subarray Sum
- LeetCode 209. Minimum Size Subarray Sum
- Leetcode 209. Minimum Size Subarray Sum
- 209. Minimum Size Subarray Sum
- leecode 解题总结:209. Minimum Size Subarray Sum
- 209. Minimum Size Subarray Sum