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. 7.1(java学习笔记)InetAddress&InetScoketAddress

    一.InetAddress 这个类主要表示IP地址.InetAddress封装了IP地址和域名.域名可以看做IP地址的一个别称,起这个别称的目的是为了便于记忆. 例如www.baidu.com 就是1 ...

  2. 1.7(java学习笔记)package和import

    package package主要用于管理类,在java中同一个包下不能有相同的类名,可有时项目总会出现很多同名的类,这时就需要通过包来管理类.不同的包下可以有相同的类名. 包就有点类似于文件夹,不同 ...

  3. 计算最大公约数 Exercise05_14

    import java.util.Scanner; /** * @author 冰樱梦 * 时间:2018年下半年 * 题目:计算最大公约数 * */ public class Exercise05_ ...

  4. Postman Json测试接口

    当传递Json数据时: 1.必须添加http头:content-type:application/json,否则会报错(后台取不到相对应的值) 注意:如果服务端只支持UTF-8,但程序未对提交数据进行 ...

  5. JQuery使用trigger模拟触发selete的选择change事件

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

  6. SQLSERVER LATCH WINDBG

    https://mssqlwiki.com/2012/09/07/latch-timeout-and-sql-server-latch/

  7. 并发的HashMap为什么会引起死循环?(转)

    本文转自http://blog.csdn.net/zhuqiuhui/article/details/51849692 今天研读Java并发容器和框架时,看到为什么要使用ConcurrentHashM ...

  8. Lucene的学习及使用实验

    实验一下Lucene是怎么使用的. 参考:http://www.importnew.com/12715.html (例子比较简单) http://www.yiibai.com/lucene/lucen ...

  9. hibernate4配置文件hibernate.cfg.xml配置详解

    <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configurat ...

  10. android 开发者的个人博客集

    1.  http://stormzhang.com/posts.html    //不少的好的工具与建议