【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 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 = ;
int k = ;
for(int p = ; p < n; p ++)
{
if(A[p] == )
{
i ++;
}
else if(A[p] == )
{
j ++;
}
else
k ++;
}
for(int p = ; p < n; p ++)
{
if(p < i)
A[p] = ;
else if(p >= i && p < i + j)
A[p] = ;
else
A[p] = ;
}
}
};

如果只能扫一遍,很容易想到的就是左边存放0和1,右边存放2.两边往中间靠。
设置两个index,left记录第一个1的位置,left左边为0,right记录第一个非2的位置,right右边为2.
然后使用i从头到尾扫一遍,直到与right相遇。
i遇到0就换到左边去,遇到2就换到右边去,遇到1就跳过。
需要注意的是:由于left记录第一个1的位置,因此A[left]与A[i]交换后,A[left]为0,A[i]为1,因此i++;
而right记录第一个非2的位置,可能为0或1,因此A[right]与A[i]交换后,A[right]为2,A[i]为0或1,i不能前进,要后续判断。
由此该数组分为4段:[0,left)-->0; [left,i)-->1; [i,right]-->乱序; (right,n-1]-->2
0 0 0 1 1 1 2 1 0 2 1 2 2 2
^ ^ ^
left i right
【最直接:partition】
class Solution {
public:
void sortColors(int A[], int n) {
int left = ;
int right = n-;
int i = ;
while(i <= right)
{
if(A[i] == )
{
swap(A[left], A[i]);
left ++;
i ++;
}
else if(A[i] == )
{
i ++;
}
else
{
swap(A[i], A[right]);
right --;
}
}
}
};

这是网上看到一种漂亮的做法,膜拜。
【最直观:平移插入】
class Solution {
public:
void sortColors(int A[], int n) {
int i = -;
int j = -;
int k = -;
for(int p = ; p < n; p ++)
{
//根据第i个数字,挪动0~i-1串。
if(A[p] == )
{
A[++k] = ; //2往后挪
A[++j] = ; //1往后挪
A[++i] = ; //0往后挪
}
else if(A[p] == )
{
A[++k] = ;
A[++j] = ;
}
else
A[++k] = ;
}
}
};

【LeetCode】75. Sort Colors (3 solutions)的更多相关文章
- 【LeetCode】75. Sort Colors 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 计数排序 双指针 日期 题目地址:https://l ...
- 【leetcode】75. Sort Colors
题目如下: 解题思路:我的解题思路是遍历数组,遇到0删除该元素并插入到数组头部,遇到1则不处理,遇到2删除该元素并插入到数组尾部. 代码如下: class Solution(object): def ...
- 【一天一道LeetCode】#75. Sort Colors
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】075. Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- 【leetcode】905. Sort Array By Parity
题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数 ...
- 【LeetCode】排序 sort(共20题)
链接:https://leetcode.com/tag/sort/ [56]Merge Intervals (2019年1月26日,谷歌tag复习) 合并区间 Input: [[1,3],[2,6], ...
- LeetCode OJ 75. Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- 【LeetCode】90. Subsets II (2 solutions)
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
- 【LeetCode】912. Sort an Array 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数排序 桶排序 红黑树排序 归并排序 快速排序 ...
随机推荐
- [SQLite] SQLite学习手册(数据库和事务)
转载地址:http://www.cnblogs.com/stephen-liu74/archive/2012/02/18/2322575.html 一.Attach数据库: ATTACH DATABA ...
- 机器学习简史brief history of machine learning
BRIEF HISTORY OF MACHINE LEARNING My subjective ML timeline (click for larger) Since the initial sta ...
- 从原型模式(Prototype Pattern)到 Clone
前面提到抽象工厂的实现,这里说说抽象工厂的原型实现,与工厂方法的实现不同,原型实现有他自己的优点和缺点 原型的优点: 1. 效率:clone是native方法,比new的效率高,当使用复杂循环嵌套对象 ...
- Windows + IIS 环境部署Asp.Net Core App
环境:Windows Server 2012, IIS 8, Asp.Net Core 1.1. 不少人第一次在IIS中部署Asp.Net Core App的人都会遇到问题,会发现原来的部署方式无法运 ...
- Objective-C:NSNumber类的常见用法
NSNumber基本数据类型包装类: // // main.m // 04-NSNumber // // Created by ma c on 15/8/17. // Copyright (c ...
- 让IE6/IE7/IE8支持HTML5标签的js代码
让IE(ie6/ie7/ie8)支持HTML5元素,我们需要在HTML头部添加以下JavaScript,这是一个简单的document.createElement声明,利用条件注释针对IE来调用这个j ...
- 【架构】Kubernetes和Spring Cloud哪个部署微服务更好?
Spring Cloud 和Kubernetes都自称自己是部署和运行微服务的最好环境,但是它们在本质上和解决不同问题上是有很大差异的.在本文中,我们将看到每个平台如何帮助交付基于微服务的架构(MSA ...
- 基于Java spring框架的微信企业号开发中关于js-sdk的配置
在调用js-sdk的第一步,我们需要引入js-sdk的js链接,然后执行wx.config,官方示例如下所示: wx.config({ debug: true, // 开启调试模式,调用的所有api的 ...
- json数据的用法
json数据在后台跟前台传递数据使用是非常多站点开发者再熟悉只是的数据格式了,可是呢在这之前肯定有不少人还不知道json数据是怎么使用的, {"name":"01&quo ...
- 3. Layout -- 1
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orient ...