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 ...
随机推荐
- homework6-更加简单的题目
又把时间搞错了 以为这次要写客户端程序的博客 没想到这次是“怎么吃” 言归正传 cnblog上面有很多技术博客 http://perhaps.cnblogs.com/archive/2005/08/0 ...
- Android:从程序员到架构师之路Ⅲ_高焕堂
Part-2: 从Android框架代码中学习设计 一 基础设计模式(Pattern)的代码:以Android为例 1.Template Method模式:IoC(控制反转)机制 2.Observer ...
- sql的存储过程调用
USE [ChangHong_612]GO/****** Object: StoredProcedure [dbo].[st_MES_GetCodeRule] Script Date: 09/10/2 ...
- CloudStack 使用生态统计
http://shapeblue.com/
- 音频视频播放(jquery中将jquery方法转化成js方法)
在jQuery中没有音频视频直接播放的方法,我们在写音频视频时,应该将jquery的方法转化为js方法:play():pause() 补充: 将jq对象转化成js对象写法: var music=$ ...
- HDU 4122 Alice's mooncake shop 单调队列优化dp
Alice's mooncake shop Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem ...
- delphi 去掉TreeView水平滚动条
使用API函数:声明 FUNCTION ulong ShowScrollBar(ulong hwnd,ulong wBar,ulong bShow) LIBRARY "user32. ...
- 检测Insert、Capslock、NumLock、ScrollLock状态键的状态
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, ...
- Crouton
https://github.com/keyboardsurfer/Crouton https://github.com/GBouerat/Crouton https://github.com/ouy ...
- Android监听SD卡文件变化
今天再一次使用到FileObserver,上一次使用还是很久之前了.总结一下FileObserver里留的一些“坑” 1.FileObserver只能监听一个目录下的“一级”子文件,也就是说Fil ...