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. javaScript--基础 选择结构

    2.短路现象--扩展 当 true 遇到 ||  ,  true || 表达式不执行,  右侧的表达式不执行 当false 遇到 && ,  false && 表达式不 ...

  2. zabbix入门之添加监控项

    zabbix入门之添加监控项 添加一个不带参数的监控项(system.cpu.switches) 进入"配置"-->"主机"选择某主机的"监控项 ...

  3. ORA-01555 快照过旧

    用户user1对表进行了更新操作,用户user2在user1还没有进行提交前读表中数据,而且是大批量的读取(打个比方:耗时3分钟)而在这3分钟内user1进行了提交操作,那又会产生什么影响呢?这个时候 ...

  4. ZYNQ跑系统 系列(二) petalinux方式移植linux

    三.搭建petalinux工程 0.定位目录    先在shell中找一个准备存放工程的地方,(我的是home/hlf/PRO),命令行cd home/hlf/PRO 1.定位编译链    根据安装p ...

  5. 商城分类导航实现 (css)

    代码实例:demo.html <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...

  6. Vue 左右翻页,点赞动画

    因做小活动比较多,使用了一些动画,做些笔记,供大家参考 翻页动画 router -> index.js import Vue from 'vue'; import Router from 'vu ...

  7. 使用字符流(Writer、Reader)完成对文件的读写操作

    字符流 字符输出流:Writer,对文件的操作使用子类FileWriter 字符输入流:Reader,对文件的操作使用子类FileReader 每次操作的是一个字符 文件字符操作流会自带缓存,默认大小 ...

  8. 编写mapreduce的程序的套路

    https://blog.csdn.net/qq_42881421/article/details/83543926 给出下面6个经典案例: http://www.cnblogs.com/xia520 ...

  9. 获取当前的方法名字,运用线程类Thread

    得到当前方法的名字String methodName = Thread.currentThread().getStackTrace()[1].getMethodName(); getStackTrac ...

  10. 获取mysql数据库表字段的备注信息

    SELECT COLUMN_NAME as field_name , COLUMN_COMMENT as remark  FROM information_schema.COLUMNS WHERE T ...