问题:
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?

先计数后排序的思路如同Follow up所述,比较简单,就不写代码了。
这里主要分析一下one-pass的方法,其思路说穿了就是:
将2号球全部丢到队列尾部,将0号球全部丢到队列前端。

代码示例:

public class Solution {public void sortColors(int[] nums) {//zeroIndex将记录下一个0号球放置的位置//secondIndex将记录下一个2号球放置的位置int zeroIndex = 0, secondIndex = nums.length-1;for (int i=0; i <= secondIndex; i++) {//保证i位置之前的2号全部移动到了末尾while (nums[i] == 2 && i < secondIndex) {swap(nums, i, secondIndex--);}//保证i位置之前的0号球全部移动到了前端while (nums[i] == 0 && i > zeroIndex) {swap(nums, i, zeroIndex++);}}}private void swap(int[] nums, int begin, int end) {int temp = nums[begin];nums[begin] = nums[end];nums[end] = temp;}
}

个人记录-LeetCode 75. Sort Colors相关推荐

  1. 【排序】LeetCode 75. Sort Colors

    LeetCode 75. Sort Colors Solution1: 参考自:<leetcode-cpp 答案> 由于0,1,2非常紧凑,首先想到计数排序(counting sort), ...

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

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

  3. [LeetCode]75.Sort Colors

    [题目连接] 75.Sort Colors [题目] Given an array with n objects colored red, white or blue, sort them so th ...

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

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

  5. LeetCode 75. Sort Colors

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

  6. leetcode 75. Sort Colors | 75. 颜色分类(荷兰国旗问题,快速排序)

    题目 https://leetcode.com/problems/sort-colors/ 题解 快速排序3.0(随机快排+荷兰国旗技巧优化) 在arr[L-R]范围上,进行快速排序的过程: 1)在这 ...

  7. 75. Sort Colors - LeetCode

    Question 75. Sort Colors Solution 题目大意: 给一个数组排序,这个数组只有0,1,2三个元素,要求只遍历一遍 思路: 记两个索引,lowIdx初始值为0,highId ...

  8. Leetcode 075 Sort Colors

    题目连接:Leetcode 075 Sort Colors 解题思路:从左向右遍历,同时维护两个指针r和b,0~r位置上的元素为0,b~n-1位置上的为2.在遍历过程中,碰到0,放到r的位置:碰到2, ...

  9. LeetCode 75. Sort Colors--Python解法

    题目地址:Sort Colors - LeetCode Given an array with n objects colored red, white or blue, sort them in-p ...

最新文章

  1. 2019年这50个Kafka面试题,你知道答案么?
  2. OpenCV的轮廓查找和填充
  3. scrapy第一发——基础巩固
  4. C++中有关queue常用函数的用法及其注意要项
  5. 深圳linux测试题库,Linux认证考试题库及答案
  6. 创建SpringBoot项目的两种姿势
  7. 判断数据类型的几种办法
  8. JSBinding + SharpKit / JavaScript 加载流程
  9. jenkins使用_Spring Boot 系列:使用 Jenkins 部署 Spring Boot
  10. 体检预约系统软件测试计划书,体检中心-管理软件需求分析.doc
  11. 匹配代价函数之SAD
  12. mySQL数据库中.frm和.myi和.myd和.ibd文件是什么文件?
  13. 防止短信接口验证码被恶意点击
  14. 照片损坏只显示一半怎么修复?
  15. 基于51单片机的点阵贪吃蛇
  16. 使用哈希函数:H(k)=3k MOD 11,并采用链地址法处理冲突。试对关键字序列(22,41,53,46,30,13,01,67)构造哈希表,求等概率情况下查找成功的查找长度,并设计构造哈希表
  17. 支付业务名词及释义大全
  18. 裸辞4个月,面试了30家公司,终于找到了理想的工作
  19. c语言牛逼,C语言牛逼代码
  20. 微信小程(JAVAScript)实现自定义柱状图

热门文章

  1. android apk反编译工具汇总(2017年10月)
  2. 社交大革命,不可遏止的互联网春天
  3. 大数据技术---流计算(Storm)
  4. spring.main.allow-bean-definition-overriding(allowBeanDefinitionOverriding)
  5. 解决驱动器或者网络连接不可用
  6. 为啥我用navicat连接服务器数据库测试成功,但是打开失败
  7. android 8.0 ril.java,Android 8.0 RIL源码分析(一)
  8. PTA 7 - 3 h0053.求一元二次方程的根
  9. mybase破解方法
  10. 配电网可靠性评估程序 matlab代码