一天一道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的更多相关文章

  1. LeetCode 75. Sort Colors (颜色分类):三路快排

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  2. [LeetCode] 75. Sort Colors 颜色排序

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  3. LeetCode 75. Sort Colors(排序颜色)

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

  4. Leetcode 75. Sort Colors

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

  5. [leetcode]75. Sort Colors三色排序

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  6. LeetCode 75 Sort Colors(颜色排序)

    翻译 给定一个包括红色.白色.蓝色这三个颜色对象的数组.对它们进行排序以使同样的颜色变成相邻的,其顺序是红色.白色.蓝色. 在这里,我们将使用数字0.1和2分别来代表红色.白色和蓝色. 原文 Give ...

  7. leetCode 75.Sort Colors (颜色排序) 解题思路和方法

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

  8. leetcode 75. Sort Colors (荷兰三色旗问题)

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  9. leetcode 75 Sort Colors 计数排序,三路快排

    解法一:计数排序:统计0,1,2 的个数 时间复杂度:O(n) 空间复杂度:O(k)    k为元素的取值范围, 此题为O(1) class Solution { public: void sortC ...

  10. LeetCode 75. Sort Colors (python一次遍历,模拟三路快排)

    LeetCode 75. Sort Colors (python一次遍历,模拟三路快排) 题目分析: 本题需要实现数字只包含0,1,2的排序,并且要求一次遍历. 由于只用把数字隔离开,很容易想到快排的 ...

随机推荐

  1. ubuntu 下查看caj文件

    知网的学位论文只有CAJ版,而我又偏偏使用Ubuntu,所以就有了这篇文章. 前端时间发现第一种方法在ubuntu 16 上不行, 请使用第二种方法. 第一种方法: 环境:Ubuntu 14.04 6 ...

  2. ubuntu 14.04 64位 安装Opencv3.1.0 (包含opencv_contrib模块)

    写在前边: 据官方说法,目前还不是太稳定的算法模块都在opencv_contrib里边,由于不稳定,所以不能在release版本里发行,只有在稳定以后才会放进release里边.但是这里边有很多我们经 ...

  3. 第一次作业:基于Linux操作系统深入源码进程模型分析

    1.Linux操作系统的简易介绍 Linux系统一般有4个主要部分:内核.shell.文件系统和应用程序.内核.shell和文件系统一起形成了基本的操作系统结构,它们使得用户可以运行程序.管理文件并使 ...

  4. React Native 4 for Android源码分析 一《JNI智能指针之介绍篇》

    文/ Tamic: http://blog.csdn.net/sk719887916/article/details/53455441 原文:http://blog.csdn.net/eewolf/a ...

  5. android混淆那些坑

    ProGuard简介 在最新的Android Studio 2.2.2版本创建的Android工程中,module中的build.gradle有如下一段配置.这里的minifyEnabled即用来控制 ...

  6. Unity UGUI图文混排(七) -- 下划线

    之前更新超链接的时候,忘了搭配实现一个下划线的功能,这篇文章就是来补上这一个功能,时间有点长,一方面没有很好的思路,一方面也没多少时间. 先在网上收集了一下下划线的实现操作,一种是在文本下再创建一个文 ...

  7. 对于给定的整数集合S,求出最大的d,使得a+b+c=d。

    对于给定的整数集合S,求出最大的d,使得a+b+c=d.a,b,c,d互不相同,且都属于S.集合的元素个数小于等于2000个,元素的取值范围在[-2^28,2^28 - 1],假定可用内存空间为100 ...

  8. Python 2.7的字典实现简化版(C语言)

    这是一个能自动调整大小的哈希字典,外部接口实现了下列功能. 1.字典级别: 创建字典 dict_new 归零字典 dict_clear 2.键值级别: 查找 dict_search 强制查找 dict ...

  9. Dynamics CRM 通过Odata创建及更新记录各类型字段的赋值方式

    CRM中通过Odata方式去创建或者更新记录时,各种类型的字段的赋值方式各不相同,这里转载一篇博文很详细的列出了各类型字段赋值方式,以供后期如有遗忘再次查询使用. http://luoyong0201 ...

  10. [django] 利用多线程增加异步任务

    看到django异步大家的反应应该是celery这种消息队列组件,现在用的最多的最推荐的也是这种方式.然而我这需求就是请求来了,执行一个小程序,但是又不能确定这个小程序啥时候执行完,响应又要及时,丢给 ...