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.

click to show follow up.

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?

思路:假设空间上不做要求,这题还是比較简单的。能够再声明一个m,n矩阵,然后搜索原矩阵。是0。置于首位,是2置于末尾,中间置为1.

代码例如以下:

public class Solution {
public void sortColors(int[] nums) { int[] a = new int[nums.length];
a = Arrays.copyOf(nums, nums.length); int i = 0;
int j = nums.length - 1;
for(int k = 0; k < nums.length; k++){
if(a[k] == 0){
nums[i++] = a[k];
}else if(a[k] == 2){
nums[j--] = a[k];
}
}
while(i <= j){
nums[i++] = 1;
}
}
}

可是假设常数空间的话,就有点难度,网上通用的解法例如以下(能够对n个数排序):

public class Solution {
public void sortColors(int[] nums) {
int i = -1,j = -1,k = -1;
for(int m = 0; m < nums.length; m++){
if(nums[m] == 0){
nums[++k] = 2;
nums[++j] = 1;
nums[++i] = 0;
}else if(nums[m] == 1){
nums[++k] = 2;
nums[++j] = 1;
}else{
nums[++k] = 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. 75. Sort Colors(颜色排序) from LeetCode

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

  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 计数排序,三路快排

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

  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 (python一次遍历,模拟三路快排)

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

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

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

  8. [LeetCode] Sort Colors 颜色排序

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

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

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

随机推荐

  1. 关于char类型的说明

    #include<iostream> using namespace std; int main() {  char ch=128;//VC编译器默认是有符号的.但c并未明确给出.由编译器 ...

  2. 数据库问题5-SYS.SYSPROCESSES使用和查找死锁

    http://blog.sina.com.cn/s/blog_62c4727d0100jc5z.html (一)理論部份 sys.sysprocesses (Transact-SQL) http:// ...

  3. transform使用导致元素内字体出现模糊的坑~~~

    项目中遇到的,关于居中弹出层的字体模糊问题. 先来个对比图: 清晰版: 模糊版: 这个是一个不定宽高的弹出框,居中的方式如下代码: .layerdiv { position: fixed; top: ...

  4. 阿里p6面试

    电话面试: 第一次面试关注的问题,1)java基础: jvm 内存回收,垃圾回收基本原理,Java并发包的线程池,Java8的新特性.nio 堆排序.conrenthashmap , concurre ...

  5. oracle 11g jdbc jar包在哪个文件目录

    一. 如果装了oracle数据库的话, 大致是这样的目录:    D:\oracle\product\11.2.0\client_1\oui\jlib\classes12.jar 或者    D:\o ...

  6. Highcharts、AJAX、JSON、JQuery实现动态数据交互显示图表柱形图

    上个图给大家看下效果. 点击  查看图表 如下图展示效果 Highcharts简介 Highcharts 是一个用纯JavaScript编写的一个图表库, 能够很简单便捷的在web网站或是web应用程 ...

  7. Qt Creator 乱码问题

    当 把linux下的 ,QTproject文件移植到 Windows下时. 出现上图的 乱码现象.  为了,避免因为Windows下和Linux下编码不同,而产生的中文字符乱码的问题,需统一将代码中的 ...

  8. [转载]Oracle Merge的使用

    FROM: http://zhangqchang.blog.163.com/blog/static/464989732009219114653226/ 摘至网上的几个例子 一.************ ...

  9. Nehe OpenGL lesson 8

    lesson8 Blending: http://nehe.gamedev.net/tutorial/lessons_06__10/17010/ (也许须要特殊手段訪问) There was a re ...

  10. JavaEE应用程序

    一直想写一些关于JavaEE的东西,从刚開始看<Ejb in Action>的时候就想写,总是感觉自己知道的太少了.太不值得一提了.太欠缺了(我太谦虚了)--哈哈哈.到后来工作中一直在使用 ...