[LeetCode] 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?
- 创建一个zeroidx 表示0 的末尾下标,初始化为0,twoidx 表示2的下标,初始化为n,不同地方是前者操作时++,后者--。
- 遍历数组如果遇到1,则继续遍历。
- 如果遇到0,则替换 zeroidx 与i 上的值,然后zerosidx +1,如果此时该位置上的值位0,则数组从0 to i 上为0,所以zeroidx = i+1.
- 如果遇到2,则 twoidx -1, 然后替换 twoidx 与i 的值,因为替换后的i 位置上的值未判断,所以i-1 进行多一次该位置上的遍历。
3.中zeroidx 可以一直+1 不直接跳位,这样需要多次交换,discuss 实现多是这样。
下面是我写的代码:
#include <iostream>
using namespace std; class Solution {
public:
void sortColors(int A[], int n) {
int zeroIdx = ;
int twoIdx = n;
for(int i =;i<n&&i<twoIdx;i++){
if(A[i]==){
continue;
}
if(A[i]==){
A[i] = A[zeroIdx];
A[zeroIdx] = ;
if(++zeroIdx<n&&A[zeroIdx]==) zeroIdx=i+;
}
else if(A[i]==){
twoIdx--;
A[i]=A[twoIdx];
A[twoIdx]=;
i--;
}
else
return ;
}
return ;
}
}; int main()
{
int a[] = {,,,};
Solution sol;
sol.sortColors(a,sizeof(a)/sizeof(int));
for(int i=;i<sizeof(a)/sizeof(int);i++)
cout<<a[i]<<" ";
cout<<endl;
return ;
}
discuss 中有一个实现非常不错,逻辑清晰,可以将变量数3 扩展为k 个,只要不怕写起来麻烦,其逻辑类似于插入排序,将遍历的项插入正确的位置:
- 为3个(k个) 变量创建index a b c= -1;
- 遍历数组,如果为0,顺序修改a[++c] a[++b] a[++a],这样如果abc 一样,最终只有修改了 a[++a] 这一项,然后同时又更新了3者的idx。
- 如果为1,则顺序修改 a[++c] a[++b],这样 0的index 未变。
- 如果为2,则顺序修改 a[++c]。
public void sortColors(int[] A) {
int i=-, j=-, k=-;
for(int p = ; p < A.length; p++)
{
if(A[p] == )
{
A[++k]=;
A[++j]=;
A[++i]=;
}
else if (A[p] == )
{
A[++k]=;
A[++j]=;
}
else if (A[p] == )
{
A[++k]=;
}
}
}
[LeetCode] Sort Colors 只有3个类型的排序的更多相关文章
- 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 ...
- [Leetcode] Sort Colors (C++)
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- 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种数据,分别为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 ...
随机推荐
- Windows上PostgreSQL安装配置教程
Windows上PostgreSQL安装配置教程 这篇文章主要为大家详细介绍了Windows上PostgreSQL安装配置教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 PostgreSQL的 ...
- 01_5_Struts_ActionMethod_DMI_动态方法调用
01_5_Struts_ActionMethod_DMI_动态方法调用 1. ActionMethod_DMI_动态方法调用 Action执行的时候并不一定要执行execute()方法 可以在配置文件 ...
- cocos2dx lua 吞噬层的触摸事件
首先要创建一个layer,设置该层为可触摸 layer:setTouchEnabled(true) 注册触摸事件 local listener = cc.EventListenerTouchOneBy ...
- Caesars Cipher-freecodecamp算法题目
Caesars Cipher(凯撒密码.移位密码) 要求 字母会按照指定的数量来做移位. 一个常见的案例就是ROT13密码,字母会移位13个位置.由'A' ↔ 'N', 'B' ↔ 'O',以此类推. ...
- php生成zip压缩文件的方法,支持文件和压缩包路径查找
/* * new creatZip($_dir,$_zipName); *@ _dir是被压缩的文件夹名称,可使用路径,例 'a'或者'a/test.txt'或者'test.txt' *@ _zipN ...
- redis集群监控之Redis-monitor部
为了对以后有可能面临的redis集群监控做准备,这两天在准备这方面的事情,现在将其中的过程记录一下. 首先是“Ronney-Hua”的这篇文章对三中开源监控软件做了对比 文章地址:https://bl ...
- 用decimal模块增加python的浮点数精度
浮点数python默认是17位精度,也就是小数点后16位(16位以后的全部四舍五入了),虽然有16位,但是这个精度越往后越不准. 如果有特殊需求,需要更多的精度,可以用decimal模块,通过更改其里 ...
- 经典MSSQL语句大全和常用SQL语句命令的作用
下列语句部分是Mssql语句,不可以在access中使用. SQL分类: DDL类型包括数据库.表的创建,修改,删除,声明—数据定义语言(CREATE,ALTER,DROP,DECLARE) DML类 ...
- arm页表在linux中的融合
参考:arm-linux内存页表创建 arm的第一级页表条目数为4096个,对于4K页第二级目录条目个数为256个,一级二级条目都是每个条目4字节. 在linux下二级分页如下:虚拟地址——> ...
- STM32——PWM基本知识及配置过程
将通用定时器分为四个部分: 1,选择时钟 2,时基电路 3,输入捕获 4,输出比较 本节定时器PWM输出主要涉及到定时器框图右下方部分,即输出比较部分 和上一讲相同,时基时钟来源于内部默认时钟 对此有 ...