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 ...
随机推荐
- Handler笔记
5.Handler内存泄露 在一个Activity里面,如果没有静态地创建一个Handler,有可能导致内存泄露. 因为Hander和Looper绑定,如果looper的MessageQueue有消息 ...
- delphi回调函数
文章来源: http://anony3721.blog.163.com/blog/static/5119742010866050589/ 一.主单元 unit UnMain; interface us ...
- ocp 1Z0-043 131-205题解析
131. Which three methods can you use to run an Automatic Database Diagnostic Monitor (ADDM) analysis ...
- Oracle的回收站和闪回查询机制(二)
上一篇中讲诉了Oracle中一些闪回查询(Flashback Query),这是利用回滚段信息来恢复一个或一些表到以前的一个时间点(一个快照).要注意的是,Flashback Query仅仅是查询以前 ...
- STC89C52RC片内资源介绍
STC89C52RC片内有:用户应用程序区(AP)8K,地址0000h-1FFFh. 数据flash区(EEPROM)4K,2000h-2FFFh ISP引导区空间1K/2k/4k. RAM 512B ...
- UVaLive 6862 Triples (数学+分类讨论)
题意:给定一个n和m,问你x^j + y^j = z^j 的数量有多少个,其中0 <= x <= y <= z <= m, j = 2, 3, 4, ... n. 析:是一个数 ...
- JMS开发(一):基础理论认知
JMS全称是Java Message Service.其是JavaEE技术规范中的一个重要组成部分,是一种企业消息处理的规范.它的作用就像一个智能交换机,它负责路由分布式应用中各个组件所发出的消息. ...
- Linq to SQL 绑定 ComboBox
最近学习Linq to SQL,发现Linq是一个开发轻量数据库的好东西,大大简化了数据连接.查询过程.但是在绑定ComBoBox的时间发现了一个问题:Linq查询后得到的数据tolist后,只能实现 ...
- 利用tcpdump抓取mysql sql语句
这个脚本是我之前在网上无意间找个一个利用tcpdump 抓包工具获取mysql流量,并通过过滤把sql 语句输入. 脚本不是很长,但是效果很好. #!/bin/bash #this script us ...
- 【转】selenium简介及安装方法
转自:http://www.cnblogs.com/fnng/p/3157639.html 1. selenium 介绍 selenium 是一个web 的自动化测试工具,不少学习功能自动化的同学开始 ...