↓ 跳过正文
  1. Posts/

leetcode 209. Minimum Size Subarray Sum (尺取法)

·248 字·1 分钟

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

思路:尺取即可。。好久没写,竟然调了半天。。。

代码实现
 1/* ***********************************************
 2Author :111qqz
 3Created Time :2017年04月13日 星期四 20时48分00秒
 4File Name :209.cpp
 5************************************************ */
 6class Solution {
 7
 8public:
 9
10	int ruler(vector<int>nums,int tar,int n)
11	{
12	    int head = 0;
13	    int tail = 0;
14	    int sum = 0 ;
15	    int res = 0x3f3f3f3f;
16	    while (tail<n&&head<=tail)
17	    {
18		sum = sum + nums[tail];
19		if (sum>=tar)
20		{
21		    res = min(res,tail-head+1);
22		    while (sum>=tar&&head<tail)
23		    {
24			sum-=nums[head];
25			head++;
26		    }
27		    if (sum>=tar)
28		    {
29			res = min(res,tail-head+1);
30		    }
31		    else
32		    {
33			head--;
34			sum+=nums[head];
35			res = min(res,tail-head+1);
36		    }
37		}
38
39
40
41		tail++;
42	    }
43	    return res==0x3f3f3f3f?0:res;
44	}
45
46
47    int minSubArrayLen(int s, vector<int>& nums) {
48	int n = nums.size();
49	int res = ruler(nums,s,n);
50	return res;
51
52
53    }
54
55};

相关文章

leetcode 75. Sort Colors

·628 字·2 分钟
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively. 题意:一个数组,由0,1,2组成,现在要求升序排列 思路:无脑做法就是计数排序,扫两遍,时间复杂度O(n),空间复杂度O(1)

leetcode 11. Container With Most Water (two pointer)

·328 字·1 分钟
Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. 题意:n条竖直的线段 (i,0)->(i,a[i]),从中选2条,和x轴共同组成一个开口的容器,问容器的最大面积。

leetcode 16. 3Sum Closest (k-sum问题,two pointer)

·224 字·1 分钟
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. 思路: 排序,然后two pointer,复杂度 O(n^2) 1/* *********************************************** 2Author :111qqz 3Created Time :2017年04月13日 星期四 16时24分28秒 4File Name :16.cpp 5************************************************ */ 6class Solution { 7 8public: 9 10 int n; 11 int threeSumClosest(vector<int>& nums, int target) { 12 n = nums.size(); 13 sort(nums.begin(),nums.end()); 14 int mn = 0x3f3f3f3f; 15 int x,y,z; 16 for ( int i = 0 ; i <=n-3 ; i++) 17 { 18 int head = i+1; 19 int tail = n-1; 20 int tar = target - nums[i]; 21 while (head<tail) 22 { 23 int cur = target-nums[i]-nums[head]-nums[tail]; 24 if (abs(cur)<mn) 25 { 26 mn = abs(cur); 27 x = nums[i]; 28 y = nums[head]; 29 z = nums[tail]; 30 } 31 if (nums[head]+nums[tail]==tar) return target; 32 if (nums[head]+nums[tail]<tar) head++; 33 else tail--; 34 } 35 } 36 return x + y + z; 37 38 39 40 41 } 42 43};

leetcode 18. 4Sum (k-sum问题,two pointer)

·274 字·1 分钟
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: The solution set must not contain duplicate quadruplets. 思路: O(n^2)枚举两个元素,变成2-sum问题,总体复杂度O(n^3)

leetcode 15. 3Sum (k-sum问题,two pointer)

·251 字·1 分钟
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not contain duplicate triplets. 思路:排序O(nlgn),然后枚举一个元素O(n),对于每个元素,在剩下的区间中 two pointer O(n)