Level:

  Medium

题目描述:

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?

思路分析:

  三路快排的思想,以1作为标志,比1小的放在一边,比1大的放在另一边一边,但是要注意的一点是大的元素交换后由于该元素没有和标志1作比较,因此需要i=i-1。

代码:

public class Solution{
public void sortColors(int []nums){
if(nums==null||nums.length==0)
return;
int left=0;
int right=nums.length-1;
for(int i=0;i<=right;i++){
if(nums[i]<1){
nums[i]=nums[left];
nums[left]=0;
left++;
}else if(nums[i]>1){
nums[i]=nums[right];
nums[right]=2;
right--;
i=i-1;//交换过来的元素没有和1比较过,所以i减一
}
}
}
}

37.Sort Colors(颜色排序)的更多相关文章

  1. 75. Sort Colors(颜色排序) from LeetCode

      75. Sort Colors   给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和2分别表示红色, ...

  2. [LeetCode] Sort Colors 颜色排序

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

  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 in-place so that objects of the ...

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

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

  6. LeetCode OJ:Sort Colors(排序颜色)

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

  7. Leetcode75. Sort Colors颜色分类

    给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 0. 1 和 2 分别表示红色.白色和蓝色. ...

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

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

  9. [LeetCode] 324. Wiggle Sort II 摆动排序 II

    Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]... ...

随机推荐

  1. tensorflow创建cnn网络进行中文手写文字识别

    数据集下载地址:http://www.nlpr.ia.ac.cn/databases/handwriting/download.html chinese_write_detection.py # -* ...

  2. 数据库系统实现 第一章 DBMS实现概述

    DBMS提供的能力 1)持久存储 DBMS在灵活性方面比文件系统要好,同时支持对非常大量数据的存储 2)编程接口 3)事务管理 DBMS支持对数据的并发存取,即多个不同的进程(称作事物)同时存取操作, ...

  3. MFC的Dlg和App什么区别?应用程序类与对话框类

    MFC里有个app类..他是一个项目工程类,有一个全局的实例化.theApp你可以理解为整个项目的实例,它重载了入口函数,所有的窗口神马的,都是在这个类里实例化的. dlg是对话框,是一个窗口.一个程 ...

  4. [BZOJ5407]girls

    也是CF985G... 容斥+三元环计数 CF数据太弱啦 vis没赋初值-1竟然过了QAQ 所以又调了我半个小时才搞掉QAQ 数数真难QAQ 记得要写#include<vector>!!! ...

  5. docker:python与docker

    一:环境准备 pycharm:专业版(windows) docker ce 免费版(ubantu16.04) os: os:防火墙 二:开发流程 pycharm中开发环境搭建的工作原理: 1. pyc ...

  6. python基础:3.高级运算符

    1.异或运算 十进制的异或运算,先转成二进制进行异或,按位进行比较,对应位置相同则为0,对应位置不同则为1,,再从异或结果转成十进制. python中: 1 ^ 1 = 0 1 ^ 2 = 3 1 ^ ...

  7. Sublime Text3添加C++编译与运行

    安装MinGW 1.安装MinGW ,其安装方法一直下一步,安装完后点Continue会出现一个窗口,在Basic Setup下标记所有包,然后在菜单里点"Apply Changes&quo ...

  8. Monkey命令及调试

    monkey命令: 执行50w次,随机数200,忽略crash,忽略超时,详细信息级别为1:monkey -p 包名 -s 200 --throttle 100 --ignore-crashes -- ...

  9. 创建Spring Boot 工程

    先在eclipse中安装spring -tool -suite插件,然后根据以下步骤可以创建   1.新建Spring Starter Project 2.Packaging 选择 jar 3.勾选W ...

  10. 执行hbase zkcli命令报错

    执行hbase zkcli后报错信息如下: 15/10/02 15:17:55 INFO zookeeper.ZooKeeper: Client environment:java.library.pa ...