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. iOS开发——给ImageView添加点击事件

          给ImageView添加点击事件   1: cell.pictureView.userInteractionEnabled = YES; 2: UITapGestureRecognizer ...

  2. delphi中使用mediaplayer控件播放音乐

    新建一个名字为media的文件夹,把要播放的音乐文件放在这个文件夹里. ExtractFilePath是用来获得产生的可执行程式所在的路径,因为我们把要播放的音乐文件放在了可执行程式的那个根目录下. ...

  3. IE刷新后,文本框的值不变

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. unity3D打造skybox淡入淡出 - 移动开发

    原地址:http://www.it2down.com/it-mobile/426479.htm 当前位置: IT异常查询网 » unity3D打造skybox淡入淡出 - 移动开发 www.it2do ...

  5. win10 为了对电脑进行保护,已经阻止此应用 解决方法

        win10 为了对电脑进行保护,已经阻止此应用 解决方法 正确方法: ."win+x"进入控制面板,选择安全性与维护,在左侧更改windows smartscreen筛选器 ...

  6. 原生js 操作类名

    添加类名: document.getElementById('navBar').getElementsByClassName('mui-tab-item')[0].classList.add('mui ...

  7. cocos2d-x OpenGL ES 坐标系总结

    很多教程都说cocos2d-x OpenGL ES世界坐标系原点在左下角,但至于为什么在左下角却从来没有人提过,这导致大部分人觉得这是OpenGL ES的规定,事实上这是错的.OpenGL ES的坐标 ...

  8. jquery绑定事件的区别

    query中绑定事件有三种方法:以click事件为例 (1)target.click(function(){}); (2)target.bind("click",function( ...

  9. lucene 异常 Lock obtain timed out 解决方法

    http://terje.blog.163.com/blog/static/119243712008102122316595/     一般都是索引建立的过程中,不正常操作影响了IndexWriter ...

  10. .net服务器端发起请求封装

    写一个静态类封装类似客户端的请求 public static class HttpHelper { #region Get public static string HttpGet(string ur ...