[Leetcode] Sort Colors (C++)
题目:
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 i = -;
int j = -;
for (int p = ; p < n; p++) {
if (A[p] == ) {
// do nothing but moving the index of k
} else if (A[p] == ) {
//
A[p] = ;
A[++j] = ;
} else {
A[p] = ;
A[++j] = ;
A[++i] = ;
}
}
}
};
[Leetcode] Sort Colors (C++)的更多相关文章
- LeetCode: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
- [LeetCode] 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 @ Python
原题地址:https://oj.leetcode.com/problems/sort-colors/ 题意: Given an array with n objects colored red, wh ...
- 75.[LeetCode] 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 对于元素取值有限的数组,只遍历一遍的排序方法
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- [LeetCode] Sort Colors 只有3个类型的排序
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- LeetCode Sort Colors (技巧)
题意: 一个数组只可能含有3种数据,分别为1,2,3,请将数组排序(只能扫一遍). 思路: 如果扫两遍的话,用个桶记录一下,再赋值上去就行了. class Solution { public: voi ...
- 【LeetCode】Sort Colors 数组排序
题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组.包括0,1,2 ...
- LeetCode解题报告—— Rotate List & Set Matrix Zeroes & Sort Colors
1. Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. Exam ...
随机推荐
- JDK+Eclipse+MyEclipse+tomcat的安装与配置
以下我所使用的各软件版本为:JDK(1.6):eclipse(3.2.2):myEclipse(5.5.1GA):tomcat(5.5.12): 一.安装JDK: 下载完JDK(1.6)后双击进行安装 ...
- C# ITextShap 生成PDF 下载
using iTextSharp.text; using iTextSharp.text.pdf; //创建 Document Document pdfDoc = new Document(new R ...
- 微信分享jssdk config:invalid signature 签名错误
使用微信分享时,按照官方给的demo,使用时一直提示签名错误. 根据微信开发文档(http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd ...
- juqery easy ui 实现二级菜单联动
实现效果 代码: <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat= ...
- memcached在Windows下的安装
memcached简介详情请谷歌.这里介绍如何在windows下安装. 1.下载 下载地址:http://download.csdn.net/detail/u010562988/9456109 ...
- [TYVJ] P1017 冗余关系
冗余关系 背景 Background 太原成成中学第3次模拟赛 第4题 描述 Description Mrs.Chen是一个很认真很称职的语文老师 ......所以,当她看到学生作文里的人物关系描 ...
- shell 脚本监控程序是否正在执行, 如果没有执行, 则自动启动该进程
代码里面监控1个进程, 代码很简单, 我就不讲解了, 有不懂的, 可以在回复里面问. 我看见了会给予讲解. 当然了, 该脚本要执行,你需要开启系统的定时器进程 crond , 并且编辑配置文件. 执行 ...
- Send竞争对手:百度云一小时,QQ超大附件最多支持2G,邮件附件20M到50M不等(附国外所有storage列表)——痛点是,最大传输2G,最大容量只有3G(和微云不是一回事),转存到微云文件不能超过1G
QQ邮箱最大可发送50M普通附件(群邮件则限制在2M).此外也可以使用超大附件功能,支持将1G的文件发往任意邮箱.QQ邮箱根据你的QQ邮箱容量的不同制定相应的接受附件限制,包括附件在内,2G用户所发送 ...
- 详解JavaScript中的Object对象
Object是在javascript中一个被我们经常使用的类型,而且JS中的所有对象都是继承自Object对象的.虽说我们平时只是简单地使用了Object对象来存储数据,并没有使用到太多其他功能,但是 ...
- Python Cookie HTTP获取cookie并处理
Cookie模块同样是Python标准库中的一员,它定义了一些类来解析和创建HTTP 的 cookie头部信息. 一.创建和设置Cookie >>> import Cookie #导 ...