[LeetCode OJ] 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.
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?
class Solution {
public:
void sortColors(int A[], int n) {
int left=, right=n;
for(int i=; i<right; i++)
{
if(A[i]==)
{
A[i] = ;
A[left++] = ;
}
else if(A[i]==)
{
--right;
int temp = A[right];
A[right] = ;
A[i] = temp;
--i;
}
}
}
};
[LeetCode OJ] Sort Colors的更多相关文章
- [Leetcode Week2]Sort Colors
Sort Colors题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/sort-colors/description/ Description Give ...
- 【LeetCode】Sort Colors 数组排序
题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组.包括0,1,2 ...
- 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】Sort Colors
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- 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. Sort Colors(排序颜色)
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- [LeetCode题解]: Sort Colors
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given an a ...
- leetcode 【 Sort Colors 】python 实现
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- [LeetCode] 75. Sort Colors 颜色排序
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
随机推荐
- HW4.19
public class Solution { public static void main(String[] args) { for(int i = 1; i <= 8; i++) { fo ...
- 【Struts2+Spring3+Hibernate3】SSH框架整合实现CRUD_1.2
作者: hzboy192@192.com Blog: http://my.csdn.net/peng_hao1988 版本总览:http://blog.csdn.net/peng_hao1988/ar ...
- [学习笔记]设计模式之Factory Method
写在前面 为方便读者,本文已添加至索引: 设计模式 魔法手札索引 在上篇笔记Abstract Factory设计模式中,时の魔导士创建了一系列的FoodFactory,并教会了其中一名霍比特人theC ...
- HttpWebRequest抓数据遇到的问题
1.有些网站访问速度慢,而且这个网站的连接数(比如全球内衣,另外对于女生各种什么内衣不懂的也可以上去查看了解哈),因为没有即时的关闭,造成抓取页面数据的时候超时也严重. 解决:把相应的HttpWebR ...
- hadoop2.2.0集群安装
位说明. 位).Jdk使用的1.7(1.6也可以).网络配置好,相互可以ping通,java环境安装完毕. 第一部分 Hadoop 2.2 下载 位). 下载地址:http://apache.cl ...
- BA的广度和深度
http://insights.thoughtworkers.org/ba-capability-and-development-path-in-thoughtworks/ 作者:ThoughtWor ...
- foreach -begin -process -end
gc d:\vm.txt|foreach -begin {write-host "It's beginning."} -process {$_ +"aa"} ...
- 用Gitosis搭建Git服务器(经典资料)
该文档介绍了用Gitosis自己搭建Git服务器,文章来自于<Git权威指南>一书的第31章,讲述详细易懂易操作,是搭建Git服务器绝好资料! 下载地址:http://download.c ...
- shell 获取当前ip
HOST_IP=$(hostname --all-ip-addresses | awk '{print $1}')
- 在XMPP的JAVA开源实现Openfire中,增加LBS 附近的人功能
1. XMPP协议 与 Openfire XMPP协议是IM领域的标准协议了,具体可参考 http://xmpp.org 及RFC6120,RFC6121,RFC6122等相关文档. http: ...