sort-colors——排序3种数字
题目描述
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?
public class Solution {
public void sortColors(int[] A) {
int i, r, w, b;
r = w = b = 0;
for (i = 0; i < A.length; i++) {
if (A[i] == 0) r++;
else if (A[i] == 1) w++;
else b++;
}
for (i = 0; i < A.length; i++) {
if (i < r) A[i] = 0;
else if (i < r + w) A[i] = 1;
else A[i] = 2;
}
}
}
【扫描一遍,单向遍历】
注意,l记录0区,r记录2区的边界。因此循环遍历到i<=r即可。
class Solution {
public:
void swap(int A[], int i, int j){
int tmp=A[i];
A[i]=A[j];
A[j]=tmp;
}
void sortColors(int A[], int n) {
int l=,r=n-;
for(int i=;i<=r;i++){
if(A[i]==){
swap(A,i,l);
l++;
}
else if(A[i]==){
swap(A,i,r);
r--;
i--;
}
}
}
};
sort-colors——排序3种数字的更多相关文章
- 75. Sort Colors(颜色排序) from LeetCode
75. Sort Colors 给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和2分别表示红色, ...
- [LeetCode] Wiggle Sort 摆动排序
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...
- 【LeetCode】75. Sort Colors (3 solutions)
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- 【LeetCode】Sort Colors 解题报告
[题目] Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- 【LeetCode】Sort Colors 数组排序
题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组.包括0,1,2 ...
- Sort colors系列
75. Sort Colors 问题描述: 给一个包含n个数字的数组,其中有0,1,2:排序使得所有相同的数字相邻,且按照0,1,2的顺序. 思路: (1)计数排序: 需要扫两遍数组,一遍统计个数,第 ...
- Lintcode: Sort Colors II 解题报告
Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects ...
- Sort Colors I & II
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- [Leetcode Week2]Sort Colors
Sort Colors题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/sort-colors/description/ Description Give ...
随机推荐
- [ CodeVS冲杯之路 ] P3027
不充钱,你怎么AC? 题目:http://codevs.cn/problem/3027/ 显然是DP题,先按线段的右端点升序排序 设 f[i] 为dp到第 i 个线段时最大的价值 目标状态为 max( ...
- 转载:GCC 提供的原子操作
转载自:GCC 提供的原子操作 GCC 提供的原子操作 gcc从4.1.2提供了__sync_*系列的built-in函数,用于提供加减和逻辑运算的原子操作. 其声明如下: type __sync_f ...
- Relation(NOIP模拟赛)(二分图染色)
原题: Description 有n个人,编号为1àn,告诉你那些人之间是不友好的.现在,让你将这n个人分成两组,使得每一组之内的人是互相友好的,如果可以分成两组,则输出如何分组的,如果不可以分成两组 ...
- Error:Execution failed for task ':app:transformClassesWithDexForDebug'. > com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.Exec
Error:Execution failed for task ':app:transformClassesWithDexForDebug'.> com.android.build.api.tr ...
- org.hibernate.HibernateException: Unable to get the default Bean Validation factor
org.hibernate.HibernateException: Unable to get the default Bean Validation factor这个异常需要在hibernate.c ...
- SSOJ 2316 面积【DFS/Flood Fill】
题目描述 编程计算由“1”号围成的下列图形的面积.面积计算方法是统计1号所围成的闭合曲线中点的数目. 如图所示,在10*10的二维数组中,“1”围住了15个点,因此面积为15. 题目大意:对于给定的1 ...
- 51nod 1596 搬货物【贪心/二进制】
1596 搬货物 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 取消关注 现在有n个货物,第i个货物的重量是 2wi ...
- #423 Div2 D
#423 Div2 D 题意 构造一个 n 个节点的树,恰好有 k 个叶子节点 (叶子节点的定义是只与树上的某一个节点存在连边),要求任意两个叶子节点的距离的最大值最小,距离为两个节点间边的数量,输出 ...
- BFS+最小生成树+倍增+LCA【bzoj】4242 水壶
[bzoj4242 水壶] Description JOI君所居住的IOI市以一年四季都十分炎热著称. IOI市是一个被分成纵H*横W块区域的长方形,每个区域都是建筑物.原野.墙壁之一.建筑物的区域有 ...
- ReactiveCocoa(二)
前言 通过ReactiveCocoa(一)的学习,相信大家对ReactiveCocoa有了一些基本认识吧.下面就让我们来学习ReactiveCocoa的一些基本使用吧! ReactiveCocoa基本 ...