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.
click to show follow up.
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?
/*************************************************************************
> File Name: LeetCode075.c
> Author: Juntaran
> Mail: JuntaranMail@gmail.com
> Created Time: Tue 19 May 2016 20:41:54 PM CST
************************************************************************/ /************************************************************************* 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. click to show follow up. 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? ************************************************************************/ #include <stdio.h>
#include <stdlib.h> void printNums(int* nums, int numsSize)
{
int i;
for( i=; i<numsSize; i++ )
{
printf("%d ",nums[i]);
}
printf("\n");
} /* 高端做法 */
void sortColors(int* nums, int numsSize)
{
int red=-, white=-, blue=-;
int i; printNums(nums, numsSize);
for( i=; i<numsSize; i++ )
{
if(nums[i] == )
{
nums[++blue] =;
nums[++white]=;
nums[++red] =;
printNums(nums, numsSize);
}
else if (nums[i] == )
{
nums[++blue] =;
nums[++white]=;
printNums(nums, numsSize); }
else if (nums[i] == )
{
nums[++blue] =;
printNums(nums, numsSize);
}
}
} /* ------- Before meeting nums[3] -------
index: 0 1 2 3 4 5 6 7 8
nums: 0 1 2 1 2 0 2 2 1
lables: r w b paint: 2 2 2
1 1
0 appear: 0 1 2 ------- After dealing with nums[3] -------
index: 0 1 2 3 4 5 6 7 8
nums: 0 1 2 1 2 0 2 2 1
lables: r w b paint: 2 2 2 2
1 1 1
0 appear: 0 1 1 2 ------- After finish iteration -------
index: 0 1 2 3 4 5 6 7 8
nums: 0 1 2 1 2 0 2 2 1
lables: r w b paint: 2 2 2 2 2 2 2 2 2
1 1 1 1 1
0 0 appear: 0 0 1 1 1 2 2 2 2 */ /* 智障做法 */
void sortColors2(int* nums, int numsSize)
{
int red = ;
int white = ;
int blue = ; int i;
for( i=; i<numsSize; i++ )
{
if( nums[i] == )
{
red ++;
}
if( nums[i] == )
{
white ++;
}
if( nums[i] == )
{
blue ++;
}
}
// printf("%d %d %d\n", red,white,blue); for( i=; i<red; i++ )
{
nums[i] = ;
}
// printNums(nums, numsSize); for( i=red; i<white+red; i++ )
{
nums[i] = ;
}
// printNums(nums, numsSize); for( i=white+red; i<white+red+blue; i++ )
{
nums[i] = ;
}
// printNums(nums, numsSize);
} int main()
{
int nums[] = {,,1,,,,,,};
int numsSize = ;
sortColors(nums, numsSize); return ;
}
LeetCode 75的更多相关文章
- [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) 30
75. 颜色分类 75. Sort Colors 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中, ...
- LeetCode 75,90%的人想不出最佳解的简单题
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode专题的44篇文章,我们一起来看下LeetCode的75题,颜色排序 Sort Colors. 这题的官方难度是Medi ...
- 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.颜色分类 By Python
给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 0. 1 和 2 分别表示红色.白色和蓝色. ...
- leetcode 75颜色分类
两趟扫描,由于排序变量的特殊性,使用计数排序方法可以明显降低至O(n)time O(n) space 关于计数排序:https://mp.weixin.qq.com/s/WGqndkwLlzyVOHO ...
- Java实现 LeetCode 75 颜色分类
75. 颜色分类 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 0. 1 和 2 分别表示红 ...
- [leetcode] 75. 分类颜色(常数空间且只扫描一次算法)
75. 分类颜色 我们直接按难度最高的要求做:你能想出一个仅使用常数空间的一趟扫描算法吗? 常数空间 只能扫描一趟.注意,是一趟,而不是O(n) 题中只会出现3个数字:0,1,2.换句话说,0肯定在最 ...
- Leetcode 75. Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
随机推荐
- homework_06 围棋程序改进
1) 把程序编译通过, 跑起来. 读懂程序,在你觉得比较难懂的地方加上一些注释,这样大家就能比较容易地了解这些程序在干什么. 把正确的 playPrev(GoMove) 的方法给实现了. 注释见Git ...
- 一些常用的IOS开发网站
开发教程: 即便过了入门阶段,还是要经常看看一些不错的实例教程.1.http://mobile.tutsplus.com/category/tutorials/iphone/ 比较新的一个网站,以前没 ...
- POj3268 Silver Cow Party
http://poj.org/problem?id=3268 题目大意:求到x距离与从x返回和的最大值 从x点到各个点最短路好求,直接用Dijkstar,但从各个点到x点却不好求,只要把路向翻转过来也 ...
- 斯特灵数 (Stirling数)
@维基百科 在组合数学,Stirling数可指两类数,都是由18世纪数学家James Stirling提出的. 第一类 s(4,2)=11 第一类Stirling数是有正负的,其绝对值是个元素的项目分 ...
- HDU 2275 multiset
题意:n个操作 Push 入容器 Pop弹出一个 满足<=该数的最大的数(若没有输出No Element!) 开始用set打了一遍wrong了,这里入容器的数是有重复的,所以用multiset ...
- 从Jetty、Tomcat和Mina中提炼NIO构架网络服务器的经典模式(二)
本文转载自 http://blog.csdn.net/cutesource/article/details/6192145 下面再来看看Tomcat是如何使用NIO来构架Connector这块的. 先 ...
- 关于session更新的问题
最近在学习用ssh框架做一个实习生招聘系统,已经做了大半.今天突然想到一个问题,在登录的时候我把用户的所有信息放到session中去,那么我不同用户同时登录的时候session中的信息是否会被覆盖掉( ...
- 解决安装SQL Server2008失败的问题
安装SQL Server2008时遇到"2008安装错误 必须重新启动计算机才能安装 SQL Server". 解决办法:HKEY_LOCAL_MACHINE\SYSTEM\Cu ...
- 【MyLocations】标记位置App开发体会
实现功能: 1.能通过Cora Location获取地址信息 2.用户获取地址信息后能编辑相关信息 3.使用Core Data保存数据 4.使用MapKit,在Map上显示标记的位置,并可以编辑位置信 ...
- 教你50招提升ASP.NET性能(二十二):利用.NET 4.5异步结构
(40)Take advantage of .NET 4.5 async constructs 招数40: 利用.NET 4.5异步结构 With the arrival of .NET 4.5, w ...