[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 ...
随机推荐
- C/C++ 中##(两个井号)和#(一个井号)用法
##(两个井号)和#(一个井号)都是什么意思 连接符 ##(两个井号) 不知道什么符 #(一个井号) ## 连接符号由两个井号组成,其功能是在带参数的宏定义中将两个子串(token)联接起来,从而形成 ...
- 如何让你的 footer 总是在浏览器底部无论什么分辨率无论什么浏览器?
一个可以用的CSS驱动的可粘在底部的Footer 我们曾经都或多或少的试过用CSS来把Footer固定在底部的经理,但是他们总是不能正常的粘在底部,不是吗?可喜的是,痛苦的研究如何让footer粘在底 ...
- iOS学习之视图加载过程中会触发的方法(loadView/viewDidLoad/didReceiveMemoryWarning)
1.loadView 这是视图控制器用来加载根视图的方法; 如果需要将自定义的视图作为根视图,则不需要调用父类对该方法的实现([super loadView]);直接将自定义视图通过self.view ...
- 爬虫框架YayCrawler
爬虫框架YayCrawler 各位好!从今天起,我将用几个篇幅的文字向大家介绍一下我的一个开源作品——YayCrawler,其在GitHub上的网址是:https://github.com/liush ...
- Java学习笔记--多线程
rollenholt的博文:http://www.cnblogs.com/rollenholt/archive/2011/08/28/2156357.html 弹球例子: 0. 创建Bounce框架 ...
- ORA-19502: write error on file "", blockno (blocksize=)/linux下磁盘空间满了解决办法--Virtualbox
今天,在测试环境启动数据库时,报错: SQL> startup; ORACLE instance started. Total System Global Area 285212672 byt ...
- 使用SALT-API进入集成开发的简单样例
测试的时候,可以CURL -K,但真正作集成的时候,却是不可以的. 必须,不可以让TOKEN满天飞吧. 现在进入这个阶段了.写个样例先: import salt import salt.auth im ...
- BZOJ 1068 (区间DP)
题意:字符串的压缩,f[l][r][0]代表还没M,f[l][r][1]代表有M. #include<cstdio> #include<cmath> #include<c ...
- LeetCode_Word Search
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- python 文件中的中文编码解决方法
# -*- coding: utf-8 -*- #查看安装的SDK默认的编码字符集在脚本中可以修改你的编码格式, 方法如下:#sys.getdefaultencoding()#reload(sys)# ...