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 ...
随机推荐
- jquery validate easyui tooltip
jquery validate.js onfocusin: function (element, event) { this.lastActive = element; // hide error l ...
- keil中编译时出现*** ERROR L107: ADDRESS SPACE OVERFLOW
解决方法: http://zhidao.baidu.com/link?url=DWTVVdALVqPtUt0sWPURD6c1eEppyu9CXocLTeRZlZlhwHOA1P1xdesqmUQNw ...
- FIREDAC直连ORACLE数据库
UniDac对Oracle的Direct连接,不需要套Oracle客户端dll,deploy时真的时 方便又快捷.FireDac连接Oracle,在没有Oracle Client的情况下,是可以连接上 ...
- ASP.NET MVC程序传值方式:ViewData,ViewBag,TempData和Session
转载原地址 http://www.cnblogs.com/sunshineground/p/4350216.html 在ASP.NET MVC中,页面间Controller与View之间主要有以下几种 ...
- cocos2dx搭建开发环境
windows7 64位 搭建cocos2dx 版本开发环境 目前cocos2dx分为2.x版本和3.x版本,搭建环境稍有不同 先搭建3.1版本win32开发环境 相关准备: 注意:安装路径尽可能不要 ...
- 【WinForm】使用NSIS发布程序
简介 NSIS(Nullsoft Scriptable Install System)是一个开源的 Windows 系统下安装程序制作程序.它提供了安装.卸载.系统设置.文件解压缩等功能 使用 以下是 ...
- Codeforces Round #277 (Div. 2) D. Valid Sets 暴力
D. Valid Sets Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/486/problem ...
- POJ 1743 Musical Theme 后缀数组 最长重复不相交子串
Musical ThemeTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=1743 Description ...
- 解决mac的日历问题:服务器响应一个错误
出了一个问题好久,平时也不用这个同步不靠谱的日历.今晚花点时间解决了下. 参考Apple 官网日历的问题解答. 当出现如下情况时: 退出日历和提醒事项. 从 Apple () 菜单中选取“系统偏好设 ...
- [Angular 2] @Input & @Output Event with ref
The application is simple, to build a color picker: When click the rect box, it will check the color ...