您的位置:首页 > 其它

Leetcode 75 Sort Colors

2015-05-08 09:28 363 查看
原题链接:https://leetcode.com/problems/sort-colors/

题目描述

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分别代表红、白。蓝。

题目中给出了一些要求

A rather straight forward solution is a two-pass algorithm using counting sort.

First, iterate the array counting number of 0’s, 1’s, and 2’s, then overwrite array with total number of 0’s, then 1’s and followed by 2’s.

一个很直接的解决方法是两次遍历计数。

先遍历数组,数出0、1、2的个数,然后再次遍历的时候,用相应个数的0、1、2来重写数组。

Could you come up with an one-pass algorithm using only constant space?

能否想出一个一次遍历且只用常数空间的算法。

下面先给出题中说的两次遍历计数法。

方法一:两次遍历计数法

算法描述

遍历数组,数0、1、2的个数,假设为x、y、z;

再次遍历数组,重写其前x个元素为0,中间y个元素为1,最后z个元素为2

代码

void sortColors(int* nums, int numsSize) {
int count[3] = { 0 }, i;

// 数0、1、2的个数,分别存在count[0]、count[1]、count[2]中
for (i = 0; i < numsSize; ++i)
++count[*(nums + i)];

// 重写数组
int p = 0, j; // p为下一次重写的位置
for (i = 0; i < 3; ++i)
for (j = 0; j < count[i]; ++j) // 重写count[i]个i(i=0,1,2)
*(nums + p++) = i;
}


Leetcode运行情况

Status : Accepted

Run Time : 2 ms

简析

两次遍历,two-pass,常数空间O(c)

方法二:首尾交换法

首先,这个名字是我自己随便乱取的,不要纠结这个。

想到这个方法的线索是快速排序,基本思路是从前面找一个比较大的,从后面找一个比较小的,然后交换。

再详细一点,我们先把0和1、2分开,让0都在前面,1、2在后面;然后再处理1、2让他们分开,1在前面,2在后面。

算法描述

把0和1、2分开

用两个指针,start指向第一个不是0的元素位置,end指向最后一个0元素的位置

暂存一下start处的值tmp,然后把end处的数据写到start处,移动start到下一个不是0的位置

把start处的数据写到end处,然后end移到上一个0元素的位置

重复上述1-3,直到start和end相遇

把tmp写到start处

至此0和1、2已经分开了。从start当前位置开始一直到整个数组的最后,现在只有1和2,将其分开即可。

把1和2分开

start指向第一个不是1的元素位置,end指向最后一个1元素的位置

暂存一下start处的值tmp,然后把end处的数据写到start处,移动start到下一个不是1的位置

把start处的数据写到end处,然后end移到上一个1元素的位置

重复上述1-3,直到start和end相遇

把tmp写到start处

至此,0、1、2已经分开了。

代码

void sortColors(int* nums, int numsSize) {

/* 先将0与1、2分开 */
int start = 0, end = numsSize - 1;
// start指向第一个不是0的位置
while (start < end && *(nums + start) == 0)
++start;
// end指向最后一个0的位置
while (start < end && *(nums + end) != 0)
--end;
// 暂存nums[start]
int tmp = *(nums + start);
while (start < end) { // start和end不相遇时循环处理
// nums[start] = nums[end],等价于nums[start] = 0
*(nums + start) = *(nums + end);
// start后移,指向下一个不为0的位置
++start;
while (start < end && *(nums + start) == 0)
++start;
// 如果start和end相遇,跳出
if (start == end)
break;
// nums[end] = nums[start],nums[end]等于1或2
*(nums + end) = *(nums + start);
// end前移,指向上一个0的位置
--end;
while (start < end && *(nums + end) != 0)
--end;
}
// 取回暂存的值
*(nums + start) = tmp;

/* 把1和2分开 */
// start指向第一个不是1的位置
// 由于start当前位置就是目前第一个不为0的元素,因此直接从此处开始
while (start < end && *(nums + start) == 1)
++start;
// end重置为最后一个元素的位置
end = numsSize - 1;
// end指向最后一个1的位置
while (start < end && *(nums + end) == 2)
--end;
// 暂存nums[start]
tmp = *(nums + start);
while (start < end) {
// nums[start] = nums[end],等价于nums[start] = 1
*(nums + start) = *(nums + end);
// start后移,指向下一个不为1的位置(即下一个2的位置)
++start;
while (start < end && *(nums + start) == 1)
++start;
// 如果start和end相遇,跳出
if (start == end)
break;
// nums[end] = nums[start],等价于nums[end] = 2
*(nums + end) = *(nums + start);
// end前移,指向上一个0的位置
--end;
while (start < end && *(nums + end) == 2)
--end;
}
// 取回暂存的值
*(nums + start) = tmp;
}


Leetcode运行情况

Status : Accepted

Run Time : 2 ms

简析

少于两次遍历,多于一次遍历,具体遍历次数取决于数据中0的个数,常数空间O(c)

方法三:平移法

这个方法是从网上看到的别人的,膜拜一下,真的很棒,非常巧妙。

算法描述

用三个标志位x、y、z分别代表当前处理好的数据中存储着0、1、2的最后一个位置

遍历原始数据,如果是0,则x,y,z分别往后移动一位

如果是1,则y、z分别往后移动一位

如果是2,则仅z往后移动一位

在移动x、y、z的时候,其相应位置要分别赋值0、1、2

举个例子吧,假如我们已经处理到第4位了,此时数据为0012012012,因此有x=1,y=2,z=3

接下来,第五位为0,因此x后移变成2,相应的第2位重写为0,y后移变成3,第四位重写为1,z后移重写为4,第五位重写为2,处理后,数据变为0001212012,x=2,y=3,z=4

然后,第六位为1,y后移变成4,第五位重写为1,z后移重写为5,第六位重写为2,处理后,数据变为0001122012,x=2,y=4,z=5

以此类推

注意

在平移的时候有个需要特别注意的地方,对x、y、z的移动顺序及数据重写顺序是有先后要求的,考虑如下数据:x=y=z=-1,原始数据为data[6]={0, 1, 2, 0, 1, 2},则在第一次处理时,第一位为0,根据算法,需要移动x、y、z并分别赋值重写,假设我们按如下顺序处理:x=x+1=0,data[x]=0(即data[0]=0),y=y+1=0,data[y]=1(即data[0]=1),z=z+1=2,data[z]=0(即data[0]=2)。做完上述处理后,x=0,y=0,z=0,data[6]=[2, 1, 2, 0, 1, 2]。这显然是错误的。

我们在平移和赋值时应该先移动z,再移动y,最后移动x

如果非要问为什么,可以这么理解,我们平移的时候前面的数字会覆盖后面的数字,比如x移动时会用0来覆盖1,y移动时会用1来覆盖2,因此我们应该先把被覆盖的数据(后面的数据)后移,然后再用前面的数据来覆盖。

代码

void sortColors(int* nums, int numsSize) {
int p, x = -1, y = -1, z = -1;
for (p = 0; p < numsSize; ++p)
if (*(nums + p) == 0) {
*(nums + ++z) = 2; // 2后移
*(nums + ++y) = 1; // 1后移
*(nums + ++x) = 0; // 添加一个0
} else if (*(nums + p) == 1) {
*(nums + ++z) = 2; // 2后移
*(nums + ++y) = 1; // 添加一个1
} else {
*(nums + ++z) = 2; // 添加一个2
}
}


Leetcode运行情况

Status : Accepted

Run Time : 2 ms

简析

一次遍历,one-pass,常数空间O(c)

总结

但看运行时间上,三种方法基本差不多,因为其时间复杂度都是O(n),因此不会有太大的差别。

从代码量或者代码逻辑复杂性上来看,第一种和第三种比较简单,第二种较复杂。

如果非要要求更少的遍历次数,第三种优于第二种优于第一种。

// 个人学习记录,若有错误请指正,大神勿喷

// sfg1991@163.com

// 2015-05-08
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: