[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 ...
随机推荐
- Slasher Flick-freecodecamp算法题目
Slasher Flick(截断数组) 要求 返回一个数组被截断n个元素后还剩余的元素,截断从索引0开始. 思路 利用.splice(0,howMany)删除数组中索引从0开始的howMany个元素 ...
- 【思维题 状压dp】APC001F - XOR Tree
可能算是道中规中矩的套路题吧…… Time limit : 2sec / Memory limit : 256MB Problem Statement You are given a tree wit ...
- poj 1017 装箱子问题 贪心算法
题意:有1*1到6*6的的东西,需要用6*6的箱子将它们装起来.问:至少需要多少个6*6箱子 思路: 一个瓶子怎么装东西最多?先装石头,在装沙子,然后装水. 同样放在本题就是先装6*6然后5*5... ...
- 算法导论 第七章 快速排序(python)
用的最多的排序 平均性能:O(nlogn){随机化nlogn} 原地址排序 稳定性:不稳定 思想:分治 (切分左右) 学习方式:自己在纸上走一遍 def PARTITION(A,p,r): x = ...
- 使用fio测试磁盘I/O性能
简介: fio是测试IOPS的非常好的工具,用来对硬件进行压力测试和验证,支持13种不同的I/O引擎,包括:sync,mmap, libaio, posixaio, SG v3, splice, nu ...
- [转载]C语言头文件的作用
最近在工作当中遇到了一点小问题,关于C语言头文件的应用问题,主要还是关于全局变量的定义和声明问题.学 习C语言已经有好几年了,工作使用也近半年了,但是对于这部分的东西的确还没有深入的思考过.概念上还是 ...
- vi 编辑器命令
插入命令 a append after the cursor A append after the current line i insert before the cursor I insert b ...
- 【ajax】全面总结
Ajax 的全面总结 2017-11-03 山外de楼 JavaScript Ajax在前端开发中有着举足轻重的地位,关于Ajax的使用和注意事项一直是一个重要的话题,借此机会,本文希望对Ajax做一 ...
- 中国首届CSS开发者大会讲师照片
中国首届CSS开发者大会讲师照片 Bert Bos Winter 点头猪 灭灭 jaychsu Hax 尤雨溪 一丝 勾三股四 小倩 **
- Leetcode7--->Reverse Integer(逆转整数)
题目: 给定一个整数,求将该整数逆转之后的值: 举例: Example1: x = 123, return 321Example2: x = -123, return -321 解题思路: 在这里只用 ...