leetcode_209题——Minimum Size Subarray Sum(两个指针)

Minimum Size Subarray Sum

Total Accepted: 10318 Total Submissions: 44504My Submissions

Question Solution

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.

click to show more practice.

Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.

Hide Tags

Array Two Pointers Binary Search

Hide Similar Problems

(H) Minimum Window Substring

Have you met this question in a real interview?

Yes

No

Discuss

这道题采用的是两个指针(滑动窗口的方法),先从0开始找到一个窗口,然后通过这两个指针来移动窗口和改变窗口的大小,再保存一个最小值。一直到最后。

#include<iostream>
#include<vector>
using namespace std;

int minSubArrayLen(int s, vector<int>& nums) {
	int n=nums.size();
	int start=0;
	int end=0;
	int min;
	int sum=0;
	while(end<n)
	{
		sum+=nums[end];
		if(sum>=s)
		{
			min=end+1;
			break;
		}
		end++;
	}
	if(end>=n)
		return 0;
	while(end<n)
	{
		if(sum>=s)
		{
			while(start<=end)
			{
				sum-=nums[start];
				start++;
				if(sum>=s)
				{
					int a=end-start+1;
					if(a<min)
						min=a;
				}
				else
					break;
			}
			if(end<n&&start<end)
			{
				end++;
				sum+=nums[end];
			}
			else
				break;
		}
		else
		{
			end++;
			if(end>n)
				break;
			else
				sum+=nums[end];
		}
	}
	return min;
}

int main()
{
	int a[6]={2,3,1,2,4,3};
	vector<int> vec(a,a+6);
	cout<<minSubArrayLen(7,vec)<<endl;
}

  

时间: 2024-10-24 13:13:11

leetcode_209题——Minimum Size Subarray Sum(两个指针)的相关文章

[LeetCode] Minimum Size Subarray Sum 解题思路

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

【LeetCode】209. Minimum Size Subarray Sum

Minimum Size Subarray Sum 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 subar

Leetcode 209. Minimum Size Subarray Sum

思路一: 本题nums里面的数由于都是正数,所以可以用两个指针 start 和 end = 0.end++向后寻找,直到 start和end之间的sum>=s.例如 nums = [2, 3, 1, 2, 4, 3], s = 7.这一步结束时找到了start = 0 的最短的满足条件的subarray. 显然当前的这个解可能不是以end = 3的最优解,所以start++,直到total < 7. 这时我们又需要end++,直到total>=s来寻找以start = 1开头的最短sub

LeetCode OJ:Minimum Size Subarray Sum(最小子数组的和)

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

【leetcode】Minimum Size Subarray Sum(middle)

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

Minimum Size Subarray Sum LT209

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. Example:  Input: s = 7, nums = [2,3,1,2,4,3] Output: 2 Explanation: the subar

LeetCode -- Minimum Size Subarray Sum

Question: 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 th

LeetCode OJ 209. Minimum Size Subarray Sum

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

[LeetCode-JAVA] Minimum Size Subarray Sum

题目: 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 mini