【一天一道LeetCode】#75. Sort Colors

原创 2016年05月31日 20:05:30

一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处

(一)题目

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.

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?

(二)解题

题目大意:数组包含0,1,2,按照大小排列这些数字。

1、最简单的方法

利用STL的sort一句话就解决了。

class Solution {
public:
    void sortColors(vector<int>& nums) {
        sort(nums.begin(),nums.end());
    }
};

2、两个指针

算法思想很简单,把所有的0交换到队首,把所有的1交换到队尾

class Solution {
public:
    void sortColors(vector<int>& nums) {
        int start = 0;
        int end = nums.size()-1;
        for(int i = 0 ; i<nums.size();i++)//把0交换到前面
        {
            if(nums[i]==0) {
                if(i!=start) swap(nums[i],nums[start]);
                start++;
            }
        }
        for(int i = nums.size()-1 ; i>=start ;i--)//把2交换到尾部
        {
            if(nums[i]==2){
                if(i!=end) swap(nums[i],nums[end]);
                end--;
            }
        }
    }
};

3、两个指针优化版

维持三个指针i、j和k,从0~i表示1,i+1~j表示1,k~nums.size()表示2
代码也比较简单:

class Solution {
public:
    void sortColors(vector<int>& nums) {
        int i = 0, j = i, k = nums.size() - 1;
        while(j <= k){
            if(nums[j] == 0) swap(nums[i++], nums[j++]);//0交换到前面
            else if(nums[j] == 1) j++;//1保持不动
            else swap(nums[k--], nums[j]);//2交换到尾部
        }
    }
};
版权声明:本文为博主原创文章,未经博主允许不得转载。

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...

LCP075 LeetCode 75. Sort Colors

Total Accepted: 100513 Total Submissions: 287992 Difficulty: Medium Given an array with n...

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...

LeetCode(75)Sort Colors (荷兰三色旗问题 Dutch National Flag)

题目如下: The flag of the Netherlands consists of three colours: red, white and blue. Given balls of the...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:【一天一道LeetCode】#75. Sort Colors
举报原因:
原因补充:

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