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 ...
随机推荐
- Delimiter must not be alphanumeric or backslash 问题及解决
Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in 正则表达 ...
- Ajax学习(1)-简单ajax案例
1.什么是Ajax? Ajax是Asynchronous JavaScript and XML 的缩写,即异步的Javascript和XML. 可以使用Ajax在不加载整个网页的情况下更新部分网页信息 ...
- POJ 1502 MPI Maelstrom(最短路)
MPI Maelstrom Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4017 Accepted: 2412 Des ...
- maven系列(2)-第一个maven的项目
上一篇简单了介绍了maven和maven的安装,这篇介绍如何用maven创建项目. 1. 命令行创建maven项目 maven创建项目很简单,直接调用mvn archetype:generate命令即 ...
- poj 3026 Borg Maze (BFS + Prim)
http://poj.org/problem?id=3026 Borg Maze Time Limit:1000MS Memory Limit:65536KB 64bit IO For ...
- Codeforces Round #352 (Div. 2) D. Robin Hood (二分答案)
题目链接:http://codeforces.com/contest/672/problem/D 有n个人,k个操作,每个人有a[i]个物品,每次操作把最富的人那里拿一个物品给最穷的人,问你最后贫富差 ...
- java选项及系统属性
java选项 -d32 使用 32 位数据模型 (如果可用) -d64 使用 64 位数据模型 (如果可用) -server 选择 "server" VM 默认 VM 是 serv ...
- win8图片默认不显示
最近,发现了一个问题,在查看图片的时候,出现了这样的情况: 查看的时候很不方便,想要找到自己需要的图片就要误打误撞,也不知道自己在哪儿设置了,于是,上网查资料,才发现其实只需要简单的该一下设置就可以了 ...
- .NET的Snk使用方法
保护你Asp.Net生成的DLL和Code不被别人反编译 大家做项目开发一般都是分层的,比如UI层,业务层,数据访问层.业务层引用数据访问层的DLL(比如 dataAccess.dll),并使用da ...
- WordPress主题制作教程[壹] - 了解WP&结构&索引
最近开始筹备WordPress主题开发了.首先我们在此章节中进行了解什么是WP,以及WP的结构.通过这个文章索引到以后所写的WP系列教程. (抱歉,大家不要急,持续更新中....) 1.首先,我们来认 ...