leetCode 75.Sort Colors (颜色排序) 解题思路和方法
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.
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 (颜色排序) 解题思路和方法的更多相关文章
- [LeetCode] 75. Sort Colors 颜色排序
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- 75. Sort Colors(颜色排序) from LeetCode
75. Sort Colors 给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和2分别表示红色, ...
- 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 in-place so that objects of the ...
- LeetCode 75. Sort Colors (python一次遍历,模拟三路快排)
LeetCode 75. Sort Colors (python一次遍历,模拟三路快排) 题目分析: 本题需要实现数字只包含0,1,2的排序,并且要求一次遍历. 由于只用把数字隔离开,很容易想到快排的 ...
- LeetCode 75 Sort Colors(颜色排序)
翻译 给定一个包括红色.白色.蓝色这三个颜色对象的数组.对它们进行排序以使同样的颜色变成相邻的,其顺序是红色.白色.蓝色. 在这里,我们将使用数字0.1和2分别来代表红色.白色和蓝色. 原文 Give ...
- [LeetCode] 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 in-place so that objects of the ...
随机推荐
- iOS viewDidLoad 什么时候调用
- (void)viewDidLoad; // Called after the view has been loaded. For view controllers created in code, ...
- MOD_EXPIRES安装和配置 提高网站速率
MOD_EXPIRES安装和配置 提高网站速率 实施这一方法将节省你难以置信数额的带宽,极大地加快你的网站为你的网站访客.基本上,对于图片,CSS , JavaScript以及其他文件可以通过优化 ...
- 线程协作-Semaphore并发限制
Semaphore(信号量)是用来控制同时访问特定资源的线程数量,它通过协调各个线程,以保证合理的使用公共资源.
- U盘安装Ubuntu 16.04出现:Failed to load ldlinux.c32
启动的时候如果不开启UEFI,则会提示: Failed to load ldlinux.c32 Boot failed: please change disks and press a key to ...
- dot language 学习笔记
dot language 学习笔记 UP | HOME dot language 学习笔记 Table of Contents 1 dot 语言简介 2 基本语法 2.1 常用图形 2.2 常用线 ...
- ES6笔记之参数默认值(译)
原文链接:http://dmitrysoshnikov.com/ 原文作者:Dmitry Soshnikov 译者做了少量补充.这样的的文字是译者加的,可以选择忽略. 作者微博:@Bosn 在这个简短 ...
- Unity GPU Query OpenGLES 3.0
https://github.com/google/render-timing-for-unity/blob/master/RenderTimingPlugin/RenderTimingPlugin. ...
- 【CloudFoundry】架构、设计参考
参考资料: Cloud Foundry:http://baike.baidu.com/link?url=eIfPiUI8UlsqwnnSmmZ-WFyzrf38P33lJae4Hipsd0ynwXZp ...
- Node.js nvshens图片批量下载爬虫 1.00
//====================================================== // www.nvshens.com图片批量下载Node.js爬虫1.00 // 此程 ...
- iOS-国家代码选择功能github开源分享
三行代码集成国家区号选择功能 功能执行效果如图: 开源链接: https://github.com/qxuewei/XWCountryCode 用法: 1.导入XWCountryCode类 2.在须要 ...