↓ 跳过正文
  1. Posts/

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)

如果只扫一遍呢?

一个容易想到的思路是两个指针:

需要注意 的是,交换2后要再次遍历到当前位置,或者说,只有当不交换2的时候,才执行cur++

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2017年04月13日 星期四 18时24分25秒
 4File Name :75.cpp
 5************************************************ */
 6class Solution {
 7
 8public:
 9
10    //思路:把0往前仍,把2往后仍
11    void pr(vector<int>& nums)
12    {
13	int siz = nums.size();
14	for ( int i = 0 ; i < siz;  i++) printf("%d%c",nums[i],i==siz-1?'\n':' ');
15    }
16    void sortColors(vector<int>& nums) {
17	int n = nums.size();
18	int l=0,r=n-1;
19	int cur = 0;
20	while (cur<r+1)
21	{
22	    if (nums[cur]==0)
23	    {
24		swap(nums[cur],nums[l]);
25		l++;
26		cur++;
27		continue;
28	    }
29	    if (nums[cur]==2)
30	    {
31		swap(nums[cur],nums[r]);
32		r--;
33		continue;
34	    }
35	    cur++; //只有不交换的时候才cur++
36	    pr(nums);
37	}
38
39    }
40
41};

还有一个神奇的思路:r,w,b分别表示下一个对应颜色要放的位置。

如果当前是0,那么0,1,2都要后移一个。

如果当前是1,那么1,2的位置需要后移。

如果当前是2,那么2的位置需要后移。

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2017年04月13日 星期四 18时24分25秒
 4File Name :75.cpp
 5************************************************ */
 6class Solution {
 7
 8public:
 9
10    //思路:把0往前仍,把2往后仍
11    void pr(vector<int>& nums)
12    {
13	int siz = nums.size();
14	for ( int i = 0 ; i < siz;  i++) printf("%d%c",nums[i],i==siz-1?'\n':' ');
15    }
16    void sortColors(vector<int>& nums) {
17	int n = nums.size();
18	int r,w,b;
19	r=w=b=0; //r,w,b,分别表示,当前如果要放一个相应颜色,应该放在第几个。
20	for ( auto x :nums)
21	{
22	    if (x==0)nums[b++]=2,nums[w++]=1,nums[r++]=0;
23	    else if (x==1) nums[b++]=2,nums[w++]=1;
24	    else nums[b++]=2;  //因为b,r,w前进的速度是不一样的,取决于当前的值
25	   // printf("r:%d  w:%d b:%d ",r,w,b);pr(nums);
26	}
27
28    }
29
30};

相关文章

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)