[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 ...
随机推荐
- Makefile中的ifeq 多条件使用
Makefile中的ifeq 多条件使用 网上关于makefile中ifeq的介绍已经很多了,为什么我还要在写这篇文章,因为他们只说了if else两种条件的情况,并没有讲多于两种条件情况的使用. 多 ...
- 依据Axis2官网的高速入门英文文档总结
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/ksdb0468473/article/details/29918027 首先在Eclipse中创建一 ...
- 学Python的原因
先立个旗,不学会誓不为人!!!!!!!!!!! 一直以来总是三天打鱼,两天晒网的学习,但是在体制内混久了发现,失去了很多的东西,得到的确极其有限,总感觉这样的生活会失去意义. 寻找生活的激情,重新发现 ...
- 2.STM32启动文件
一.概念声明 中断向量:由硬件产生的中断标识码,一般用于存放中断服务程序的跳转指令.根据硬件产生的中断号查找中断向量表来确定对应的中断向量.CM3内核有15个异常 和240个中断源. 程序的内 ...
- logback不输出日志消息,且SLF4J绑定源错误
我之前的项目已经成功使用过logback作为日志输出,但是今天新项目在使用的时候,不输出日志信息. 最后终于找到问题所在,并成功解决.解决步骤如下: 第一步:检查pom.xml 按照以往惯例,我先检查 ...
- flutter学习地址
Flutter - 不一样的跨平台解决方案: 关于Flutter,你想知道的都在这里了!: Flutter 时间表 2015 年 4 月,Flutter(最初代号 Sky)在 Dart Devel ...
- 【Linux】【Jenkins】配置过程中,立即构建时,maven找不到的问题解决方案
在Linux环境下配置Jenkins执行时,发现不能执行Maven,这个比较搞了. A Maven installation needs to be available for this projec ...
- CUDA compiler driver nvcc 散点 part 1
▶ 参考[https://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/index.html] ▶ nvcc 预定义的宏 __NVCC__ // 编译 ...
- Maven私服(Nexus)资源上传下载
1.settings.xml (向私服上传资源需要) <!-- Snapshot包的管理/Releases包的管理/第三方包管理--> <server> <id>l ...
- 工作流调度器azkaban
为什么需要工作流调度系统 一个完整的数据分析系统通常都是由大量任务单元组成: shell脚本程序,java程序,mapreduce程序.hive脚本等 各任务单元之间存在时间先后及前后依赖关系 为了很 ...