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. PythonStudy——字典的定义 Dictionary definition

    # 空字典 d1 = {} d2 = dict() # 用map映射创建字典 d3 = dict({'a': 1, 'b': 1}) print(d3) # 用关键字赋值方式 d4 = dict(na ...

  2. PythonStudy——列表类型 List type

    # 1.定义 ls = [3, 1, 2] # 语法糖 | 笑笑语法 print(ls) ls = list([3, 1, 2]) # 本质 print(ls) # 嵌套 ls = [3, 1, [3 ...

  3. PythonStudy——流程控制 Process control

    1. 分支结构 -- if -- if...else...-- if...elif...else...-- if嵌套 ''' if 条件: 同缩进的代码块 ''' if age > 18: pr ...

  4. qsort函数排序各种类型的数据。

    qsort函数是库函数中的一员,我们先来看看官方文档是怎么写的: 其中qsort的参数void* base是传入一个数组,size_t num 为数组整体大小,size_t size 为单个元素的大小 ...

  5. sedsed 一个更好理解sed执行过程的工具

    官网:http://aurelio.net/projects/sedsed/    这个上面好多学习的资料 嘿嘿 留着慢慢看 昨晚在看sed的具体执行过程,看到有hold space,pattern ...

  6. UBUNTU中使用pip安装,提示cannt import main问题

    在pip==8.1.1版本中,使用pip install Django==1.8.16时,提示 Traceback (most recent call last):  File "/usr/ ...

  7. Delphi XE4 For IOS之部署问题

    在XE4中编写完程序后,怎么把相应的文件部署到ios虚拟机和真实机子中呢?下面就来说说. 首先选择你要部署的项目,选择Project->Deployment菜单 Deployment菜单打开如下 ...

  8. Batch Close process

    @echo offecho.rem Kill all chrome drivertaskkill /im chromedriver.exe /f pause

  9. JavaScript中的匿名函数遇上!会怎么样

    通常,我们声明一个函数test){},可以通过test()来调用这个函数.但是,如果我们在这个函数声明的末尾加上(),解析器是无法理解的. function test(){ console.log(' ...

  10. VRay材质练习(一):水、玻璃、牛奶

    软件环境 a) 3ds max 2014b) V-Ray 3.60.03 渲染效果图集 玻璃杯 玻璃杯+水 玻璃杯+牛奶 材质详细参数 一.玻璃材质 Diffuse (0,0,0), Roughnes ...