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 ...
随机推荐
- lua的luasocket程序
-- load namespace local socket = require("socket") -- create a TCP socket and bind it to t ...
- Java学习笔记(6)
java是面向对象的语言. 对象:真实存在的唯一的事物. 类:实际就是对某种类型事物的共性属性与行为的抽取 面向对象的计算机语言的核心思想:找适合的对象做适合的事情. 如何找适合的对象呢: 1.sun ...
- Problem M: 输出九九乘法表
#include<stdio.h> int main() { int n,i,j; scanf("%d",&n); n>=&&n<= ...
- Problem T: 零起点学算法15——交换变量
#include<stdio.h> int main() { int a,b,c; scanf("%d %d",&a,&b); c=a; a=b; b= ...
- ListView控件(下)简单适配器
(一) 1.效果图 2.activiy_main.xml <?xml version="1.0" encoding="utf-8"?> <Li ...
- 如何提高码农产量,基于ASP.NET MVC的敏捷开发框架之移动端开发随笔二
前言 在前一篇文章中我已经做过开篇,接下来的随笔会详细讲一下我们的开发框架是如何实现的,专业的事由专业的人来讲,以后就由我们的高级码农小李英文名查尔斯和他的师父厂长(因为姓陈,酷爱摄影,我们的文艺片都 ...
- linux tail 命令详解!Linux 文件内容查看工具介绍
转:http://blog.csdn.net/carzyer/article/details/4759593 1.cat 显示文件连接文件内容的工具: cat 是一个文本文件查看和连接工具.查看一个文 ...
- mormot支持websocket
mormot支持websocket 根据定义,HTTP连接是无状态的和单向的,也就是说,客户机向服务器发送一个请求,该服务器通过一个应答回复.没有客户端的预先请求,就没有办法让服务器发送消息给客户机. ...
- NSString方法compare详解
传入一个需要比较的字符串.例如 [value compare:@"********"] ,返回 NSOrderedSame. options:(NSStringCompareOpt ...
- android 两次调用DatePickerDialog.onDateSet()
在4.2.2版本号上调用系统的DatePickerDialog,发现点击确定时系统调用onDateSet()两次,须要过滤当中的一次. Calendar mCalendar = Calendar.ge ...