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]

思路:

fb的高频题,边界处理很容易出bug,要小心

扫一遍,maintain四个区域,

0 0  |  1 1 | XXXX |2 2
[0, zero) = 0
[zero, i) = 1
[i, two] = unchecked elements
(two, len-1] = 2

代码:

 /**
0 0 | 1 | xxx | 2 if x == 1:
0 0 | 1 | 1 xx | 2
^
0 0 | 1 | 1 xx | 2
^ if x == 2:
0 0 | 1 | 2xx | 2
^i ^two
0 0 | 1 | x x 2| 2
^i ^two if x == 0:
0 0 | 1 | 0 xx | 2
^zero ^i
0 0 | 0 | 1 xx | 2
^i
**/ class Solution {
public void sortColors(int[] nums) {
// corner
if(nums == null || nums.length == 0) return ; int zero = -1; // nums[0...zero] = 0
int two = nums.length; // nums[two...nums.length-1] = 2
int i = 0; while(i < two){
if(nums[i] == 1){
i++;
}else if(nums[i] == 2){
two--; // to reserve a room
swap(nums, i , two);
}else{ // nums[i] == 0
zero++; // to reserve a room
swap(nums, i , zero);
i++;
}
}
}
private void swap(int[] nums, int a, int b){
int temp = nums[a];
nums[a] = nums[b];
nums[b] = temp;
}
}

[leetcode]75. Sort Colors三色排序的更多相关文章

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

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

  2. LeetCode 75. Sort Colors (颜色分类):三路快排

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

  3. [LeetCode] 75. Sort Colors 颜色排序

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

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

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

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

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

  7. Leetcode 75. Sort Colors

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

  8. [leetcode]75.Sort Color三指针

    import java.util.Arrays; /** * Given an array with n objects colored red,white or blue, * sort them ...

  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. mongodb集群配置主从模式

    测试环境 操作系统:CentOS 7.2 最小化安装 主服务器IP地址:192.168.197.21 master-node 从服务器IP地址:192.168.197.22 slave-node 关闭 ...

  2. 关于DDR3控制器的使用

    关于DDR3控制器的使用 mig_7series_0 u_mig_7series_0 ( // Memory interface ports .ddr3_addr (ddr3_addr), // ou ...

  3. JAVA 集合操作总结

    1.Collection 1.基本操作 对集合的基础操作 1.boolean add(Object o) //添加对象到集合 2.boolean remove(Object o) //删除指定的对象 ...

  4. Flask-ORM-数据库的对象关系映射模型-备忘

    ORM对象关系映射模型的特点: 优点 : 只需要面向对象编程, 不需要面向数据库编写代码. 对数据库的操作都转化成对类属性和方法的操作. 不用编写各种数据库的sql语句. 实现了数据模型与数据库的解耦 ...

  5. 【算法和数据结构】_14_小算法_Blank字符替换

    /* 本程序用来将输入的制表符替换为\t, 而将退格替换为\b, 将反斜杠替换为\\ */ #include <stdio.h> #include <stdlib.h> typ ...

  6. PAT 甲级 1027 Colors in Mars (20 分)

    1027 Colors in Mars (20 分) People in Mars represent the colors in their computers in a similar way a ...

  7. 源码小结:Java 集合ArrayList,LinkedList 源码

    现在这篇主要讲List集合的三个子类: ArrayList 底层数据结构是数组.线程不安全 LinkedList 底层数据结构是链表.线程不安全 Vector 底层数据结构是数组.线程安全 Array ...

  8. Promise的实现原理

    1.Promise 介绍 Promise类似一个事务管理器,将用户异步操作流程用流水的形式来表达,用来延迟deferred和异步asynchronous. 特点如下: (1)对象的状态不受外界影响 P ...

  9. (转)cenntos 安装mongodb

    转自 https://www.cnblogs.com/layezi/p/7290082.html 安装前注意: 此教程是通过yum安装的.仅限64位centos系统 安装步骤: 1.创建仓库文件: 1 ...

  10. leetcode347

    public class Solution { public IList<int> TopKFrequent(int[] nums, int k) { var dic = new Dict ...