【LeetCode & 剑指offer 刷题笔记】目录(持续更新中...)

Sort Colors

Given an array with n objects colored red, white or blue, sort them in-place 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.
Example:
Input: [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]

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 a one-pass algorithm using only constant space?

C++

//问题:颜色排序(以后看可不可以用partition)
//双指针法,只能用于只有三类的排序,不过只需一次遍历
#include <algorithm>
class Solution
{
public:
    void sortColors(vector<int>& A)
    {
        int left=0,right=A.size()-1;//双指针,left从左边扫描,right从右边扫描
        for(int i= 0;i<=right;i++) //扫描到right即可
        {
            if(A[i] == 0) swap(A[i++],A[left++]); //将0换到左边
            else if(A[i] == 2) swap(A[i--],A[right--]); //将2换到右边,i--是以免right换过来的值为0(抵消for循环中的i++)
        }
    }
};
//存储后移法(相当于直接插入排序,只不过预先知道了有哪些数,故简单一点)
class Solution
{
public:
    void sortColors(vector<int>& A)
    {
        int n0,n1,n2;//类别索引
        n0 = n1 = n2 = 0;
        for(int i = 0; i<A.size(); i++)
        {
            switch(A[i])
            {
                case 0:
                    A[n2++] = 2; A[n1++] = 1; A[n0++] = 0;//后面的元素往后移
                    break;
                case 1:
                    A[n2++] = 2; A[n1++] = 1;
                    break;
                case 2:
                    A[n2++] = 2;
            }
        }
    }
};
//计数排序,需要两次遍历
class Solution
{
public:
    void sortColors(vector<int>& nums)
    {
        int count[3] = {0};//用于统计每个颜色出现的次数
        for(int i=0;i<nums.size();i++) //统计直方图
            count[nums[i]]++;
      
        for(int i = 0,index = 0;i<3;i++) //排序
            for(int j = 0; j<count[i];j++)
                nums[index++] = i;
    }
};

转载于:https://www.cnblogs.com/wikiwen/p/10225956.html

【LeetCode 剑指offer刷题】查找与排序题11:Sort Colors相关推荐

  1. 【LeetCode 剑指offer刷题】矩阵题1:4 有序矩阵中的查找( 74. Search a 2D Matrix )(系列)...

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 74. Search a 2D Matrix Write an efficient algorithm that s ...

  2. 【LeetCode 剑指offer刷题】查找与排序题14:Wiggle Sort(系列)

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) Wiggle Sort II Given an unsorted array nums, reorder it su ...

  3. 【LeetCode 剑指offer刷题】查找与排序题12:Top K Frequent Elements

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) Top K Frequent Elements Given a non-empty array of integer ...

  4. 【LeetCode 剑指offer刷题】数组题2:57 有序数组中和为s的两个数(167 Two Sum II - Input array is sorted)...

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 57 有序数组中和为s的两个数 题目描述 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是 ...

  5. 【LeetCode 剑指offer刷题】树题16:Kth Smallest Element in a BST

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) Kth Smallest Element in a BST Given a binary search tree, ...

  6. 【LeetCode 剑指offer刷题】树题19:8 二叉树中序遍历的下一个结点

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 8 二叉树中序遍历的下一个结点 题目描述 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回.注 ...

  7. 【LeetCode 剑指offer刷题】树题6:28 对称二叉树(101. Symmetric Tree)

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 101. Symmetric Tree /**  * Definition for a binary tree no ...

  8. 【LeetCode 剑指offer刷题】字符串题6:67 把字符串转成整数

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 67 把字符串转成整数 题目描述 将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数. 数值为0或者字符 ...

  9. 【LeetCode 剑指offer刷题】回溯法与暴力枚举法题6:Number of Islands

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) Number of Islands Given a 2d grid map of '1's (land) and ' ...

  10. 【LeetCode 剑指offer刷题】字符串题12:Valid Palindrome(回文词系列)

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) Valid Palindrome Given a string, determine if it is a pali ...

最新文章

  1. 《孙鑫老师谈如何学好编程》摘要
  2. LTE Module User Documentation(翻译6)——物理误差模型、MIMO模型、天线模型
  3. Linux编程练习 --多线程3--mutex
  4. Failed to start firewalld.service: Unit firewalld.service is masked.
  5. Java中after注解_Spring(12):使用注解(@AfterThrowing/@After/@Around)实现AOP异常增强与实例...
  6. Nginx安装与常用配置
  7. 工程项目利用AutoMake生成Makefile实战
  8. 在WIN10中安装经典计算器
  9. 问卷中多选题该怎么分析?
  10. LTE PDCP层协议概述
  11. github排版混乱
  12. 几种编码方式(RZ、NRZ、NRZI、曼彻斯特编码)
  13. C. Hilbert's Hotel
  14. java textpad_TextPad 和JDK使用方法
  15. Html5酷播云视频播放器同层播放(代码实例)
  16. Python实现拓扑排序并绘图
  17. 冷门绝技,让你的Origin坐标轴“断掉”
  18. Gotomeeting在视频会议行业的应用趋势分析
  19. 高光谱波谱库反射波谱曲线查看与绘制
  20. 服务器打开 显示屏不显示,服务器显示屏不显示

热门文章

  1. 嫦娥五号回来要打水漂,载人回来怎么办?
  2. 文字处理技术:复杂的行宽计算
  3. 怒:排序这样的最基本功能都错了,你们竟然不感到羞耻?不反思工作?
  4. 一旦辞职,应该立即批准。留一段时间没有好处
  5. JAVA中如何产生透明的VolatileImage
  6. RELEASE版本的RegisterClass()失败
  7. 编译好的C一执行就崩溃,第一句输出都没有,是怎么回事?
  8. 空间可能与时间一样,也是不可逆转的
  9. 天线工程手册_胆大心细 专业敬业——记FPSO改装MV30项目球形天线组装工程
  10. C#复制文件夹下的所有内容到另一个文件夹