[leetcode]75. Sort Colors三色排序
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三色排序的更多相关文章
- LeetCode 75 Sort Colors(颜色排序)
翻译 给定一个包括红色.白色.蓝色这三个颜色对象的数组.对它们进行排序以使同样的颜色变成相邻的,其顺序是红色.白色.蓝色. 在这里,我们将使用数字0.1和2分别来代表红色.白色和蓝色. 原文 Give ...
- LeetCode 75. Sort Colors (颜色分类):三路快排
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- [LeetCode] 75. Sort Colors 颜色排序
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- LeetCode 75. Sort Colors(排序颜色)
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- leetCode 75.Sort Colors (颜色排序) 解题思路和方法
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- leetcode 75 Sort Colors 计数排序,三路快排
解法一:计数排序:统计0,1,2 的个数 时间复杂度:O(n) 空间复杂度:O(k) k为元素的取值范围, 此题为O(1) class Solution { public: void sortC ...
- Leetcode 75. Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- [leetcode]75.Sort Color三指针
import java.util.Arrays; /** * Given an array with n objects colored red,white or blue, * sort them ...
- leetcode 75. Sort Colors (荷兰三色旗问题)
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
随机推荐
- Django 数据库迁移
Django 数据库迁移 DATABASES = { # Django默认配置使用sqlite3数据库 # 'default': { # 'ENGINE': 'django.db.backends.s ...
- 给大厨写的R数据分析代码
###************************************** 新老客户统计 ***************************************### dachu &l ...
- oracle错误汇总1
这是遇见的第一个整个库正常,但某张表查询报错的情况 某张表数据可以查,但一排序查就报错 select * from acct_daily_bak; select * from acct_daily_b ...
- 第1章 Java语言概述--HelloWorld--环境搭建
SE学什么 第1章 Java语言概述 第2章 基本语法 第3章 数组 第4章 面向对象编程(上) 第5章 面向对象编程(中) 第6章 面向对象编程(下) 第7章 异常处理 第8章 枚举类&注解 ...
- [C#]typeof,Gettype()和is的区别
typeof 参数是一个类型名称,比如你自己编写的一个类 GetType()是类的方法,继承自object,返回该实例的类型 is 用来检测实例的兼容性(是否可以相互转换) 例: class Anim ...
- python设置路径值时为什么要输入r
r:代表处理不转义现象 Python中,u表示unicode string,表示使用unicode进行编码,没有u表示byte string,类型是str,在没有声明编码方式时,默认ASCI编码.如果 ...
- 剑指offer 2.字符串 替换空格
题目描述 请实现一个函数,将一个字符串中的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. public class Re ...
- 华硕飞马3S,日常使用续航测试
最近爱机荣耀6的电池1天2充,无奈换台新机,华为系列没大电池且价格贵,小米红米系列品控呵呵,其他品牌无小屏幕大容量电池: 然后换了台华硕飞马3S:5.2英寸 5000ma电池,日常工作娱乐使用1天半多 ...
- ubuntu16.04 安装最新版nodejs
ubuntu软件仓库中自带的nodejs版本过低 $ apt-cache policy nodejs nodejs: Installed: (none) Candidate: 4.2.6~dfsg-1 ...
- Java构造器练习题
仔细阅读下面的程序 public class Car { String name = "汽车"; public Car(String name) { this.name = nam ...