LCP075 LeetCode 75. Sort Colors

原创 2016年05月31日 17:48:08
Total Accepted: 100513 Total Submissions: 287992 Difficulty: Medium

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.

Note:
You are not suppose to use the library's sort function for this problem.

click to show follow up.

Follow up:
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.

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

Subscribe to see which companies asked this question

Hide Tags
 Array Two Pointers Sort
Show Similar Problems



Follow up 的意思是遍历一遍数组,然后统计出0, 1, 2的个数,然后再重头挨个把数字置换成相应个数的0, 1, 2
大概的伪代码是这样的

int zerocnt = 0, onecnt = 0, twocnt = 0;
for (int i = 0, i < nums.size(); i++)
{
      if (nums[i] == 0)
            zerocnt++;
      if (nums[i] == 1)
            onecnt++;
      if (nums[i] == 2)
             twocnt++;
}
for (int i = 0; i < zerocnt; i++) 
    nums[i] = 0;
for (int i = zerocnt; i < zerocnt+onecnt; i++) 
    nums[i] = 1;
for (int i = zerocnt+onecnt; i < nums.size(); i++) 
    nums[i] = 2;

同时,它提示能否使用一次遍历完成题目要求呢。
于是就想起来以我们的最直观的想法就是,从前往后挨着看,如果是0,就放到最左边;如果是2,就放到最右边;如果是1,就放到中间。
那么怎样才能以计算机的语言来完成以上想法呢?
之前做题目的时候也经常遇到two pointers, 于是想着,可以一个指针z指向最右边,一个指针x指向最左边,再一个指针y指向中间。
那么这个中间到底在哪里呢?x+z 然后除2吗?显然不对。于是便想着,先试一下,让y紧邻着x,即 y = x + 1。然后再从左向右遍历,是2,就跟z换,是0,直接跳过,是1,跟y换。
balabalabala。。。。一阵手动执行代码之后发现,让y来作为主动指针,当*y = 0,跟x换,当*y = 2,跟z换。这样的效率要高一些。
于是便有了一下代码。

class Solution {
public:
    void swaptwo(int &a, int &b)
    {
    	int temp = a;
    	a = b;
    	b = temp;
    }
    void sortColors(vector<int>& nums) 
    {
        if (nums.size() == 1)
            return;
        if (nums.size() == 2)
        {
            if (nums[0] > nums[1])
                swaptwo(nums[0], nums[1]);
            return;
        }
       	int x, y, z;
    	for (x = 0; nums[x] == 0; x++);
    	for (y = x; nums[y] == 1; y++);
    	for (z = nums.size() - 1; nums[z] == 2; z--);
    	while (y <= z)
    	{
    		if (nums[y] == 2)
    		{
    			swaptwo(nums[y], nums[z]);
    			for (z = z - 1; nums[z] == 2; z--);
    		}
    		if (nums[y] == 0)
    		{
    			swaptwo(nums[y], nums[x]);
    			for (y = y + 1; nums[y] == 1; y++);
    			x++;
    		}
    		if (nums[y] == 1)
    		{
    		    for (y = y + 1; nums[y] == 1; y++);
    		}
    	}
    }
};

然而,是否这样的代码会比两次遍历的2n的时间复杂度更好些呢?
通过LeetCode的OJ系统并发现不了什么问题,毕竟两种方法都已经是4ms了。
所以,具体是怎样的情况,是个需要继续思考的问题。。。





版权声明:本文为博主原创文章,转载时请注明原作者及出处。

LeetCode075 Sort Colors

详细见:leetcode.com/problems/sort-colors Java Solution: github package leetcode; public class...
  • zxwtry
  • zxwtry
  • 2017年04月17日 22:11
  • 72

【LeetCode-面试算法经典-Java实现】【075-Sort Colors (颜色排序)】

【075-Sort Colors (颜色排序)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given an array with n objects colored ...

leetcode 75. Sort Colors 很不错的3种元素排序方法 + O(n)

Given an array with n objects colored red。 white or blue。 sort them so that objects of the same colo...

leetcode-75 Sort Colors

因为待排序的数字的大小在一个很小的范围之内(0-2),所以可以使用这种算法: A rather straight forward solution is a two-pass algorithm u...
  • sole_cc
  • sole_cc
  • 2015年03月15日 11:05
  • 321

[leetcode] 75. Sort Colors

Given an array with n objects colored red, white or blue, sort them so that objects of the same colo...

Leetcode 75:Sort Colors

Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of ...

leetcode:75. Sort Colors

Given an array with n objects colored red, white or blue, sort them so that objects of the same colo...

Leetcode75 - Sort Colors(two pointers)

题意给定一个数组,只包含0,1,2,将0,1,2按照顺序排列。要求: 1. 只能开常数空间 2. 对数组只扫描一遍思路算法1算法1是最容易想到的,我们扫描一遍数组统计一下每个数字的数目,然后再扫描...
  • Lzedo
  • Lzedo
  • 2017年01月19日 23:17
  • 87

[leetcode] 75. Sort Colors 解题报告

题目链接:https://leetcode.com/problems/sort-colors/ Given an array with n objects colored red, white o...

LeetCode 75 Sort Colors

Given an array with n objects colored red, white or blue, sort them so that objects of the same colo...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LCP075 LeetCode 75. Sort Colors
举报原因:
原因补充:

(最多只允许输入30个字)