之前转载过一篇STL的sort方法底层详解的博客:https://www.cnblogs.com/ygh1229/articles/9806398.html

但是我们在开发中会根据自己特定的应用,有新的排序需求,比如下面这道题,当只有0,1,2这三个数字时的排序,我们就可以自己写定制版的排序算法

描述

Given an array with n objects colored red, white or blue, sort them in-place 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.

Example:

Input: [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]

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 a one-pass algorithm using only constant space?

思路一 标识位移动

我们定义 Low, Mid and High.

    ^         ^
L H
M Mid != ||
Mid++ ^ ^ ^
L M H Mid ==
Swap Low and Mid
Mid++
Low++ ^ ^ ^
L M H Mid ==
Swap High and Mid
High-- ^ ^ ^
L M H Mid ==
Swap Low and Mid
Mid++
Low++ ^ ^ ^
L M H Mid ==
Swap High and Mid
High-- ^ ^
L M
H Mid <= High is our exit case

依照此思路,算法解答如下

class Solution {
public:
void sortColors(vector<int>& nums)
{
int tmp = , low = , mid = , high = nums.size() - ; while(mid <= high)
{
if(nums[mid] == )
{
tmp = nums[low];
nums[low] = nums[mid];
nums[mid] = tmp;
low++;
mid++;
}
else if(nums[mid] == )
{
mid++;
}
else if(nums[mid] == )
{
tmp = nums[high];
nums[high] = nums[mid];
nums[mid] = tmp;
high--;
}
}
}
};

思路三  优化

优化为只找序列中的0,2并且每次移位都要移彻底,调换位置后可能还会出现0,2,所以在while中位移完全结束后才继续向前执行。

class Solution {
public:
void sortColors(vector<int>& nums) {
int low = , high = nums.size() - ;
for(int i = ;i<=high;++i){
while (nums[i]== && i<high) swap(nums[i], nums[high--]);
while (nums[i]== && i>low) swap(nums[i], nums[low++]);
}
}
};

【LeetCode】【定制版排序】Sort Colors的更多相关文章

  1. LeetCode 75. 颜色分类(Sort Colors) 30

    75. 颜色分类 75. Sort Colors 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中, ...

  2. [leetcode.com]算法题目 - Sort Colors

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  3. LeetCode(75) Sort Colors

    题目 Given an array with n objects colored red, white or blue, sort them so that objects of the same c ...

  4. [LeetCode] Merge Intervals 排序sort

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  5. 算法与数据结构基础 - 排序(Sort)

    排序基础 排序方法分两大类,一类是比较排序,快速排序(Quick Sort).归并排序(Merge Sort).插入排序(Insertion Sort).选择排序(Selection Sort).希尔 ...

  6. 【LeetCode题解】排序

    1. 排序 排序(sort)是一种常见的算法,把数据根据特定的顺序进行排列.经典的排序算法如下: 冒泡排序(bubble sort) 插入排序(insertion sort) 选择排序(selecti ...

  7. 75. Sort Colors(颜色排序) from LeetCode

      75. Sort Colors   给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和2分别表示红色, ...

  8. [Leetcode Week2]Sort Colors

    Sort Colors题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/sort-colors/description/ Description Give ...

  9. 【LeetCode】Sort Colors 数组排序

    题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组.包括0,1,2 ...

随机推荐

  1. Hibernate查询语言(HQL)

    Hibernate查询语言(HQL)与SQL(结构化查询语言)相同,但不依赖于数据库表. 我们在HQL中使用类名,而不是表名. 所以是数据库独立的查询语言. HQL的优点 HQL有很多优点. 它们如下 ...

  2. 通过 append() 和 prepend() 方法添加若干新元素

    在上面的例子中,我们只在被选元素的开头/结尾插入文本/HTML. 不过,append() 和 prepend() 方法能够通过参数接收无限数量的新元素.可以通过 jQuery 来生成文本/HTML(就 ...

  3. Unity3D学习笔记——Android重力感应控制小球

    一:准备资源 两张贴图:地图和小球贴图. 二:导入资源 在Assets下建立resources文件夹,然后将贴图导入. 三:建立场景游戏对象 1.建立灯光: 2.创建一个相机,配置默认. 3.建立一个 ...

  4. UVA 10319 - Manhattan(2-SET)

    UVA 10319 - Manhattan 题目链接 题意:一个城市,有南北和东西街道.每种街道都是单行道,如今给定几个起点和终点.要求起点和终点必须最多转一次弯能够到达,问能否够满足全部的起点终点 ...

  5. 实现 iPhone 电子书的分页显示功能的代码

     本文转载至 http://blog.csdn.net/zaitianaoxiang/article/details/6650497 原文地址:实现 iPhone 电子书的分页显示功能的代码作者:醉吻 ...

  6. 关于java后台如何接收xml格式的数据

    业务场景:用户发送下单请求,格式为xml格式,服务器接收数据完成下单,并返回结果给客户. 请求格式: <request> <head> <sign></sig ...

  7. 《从零开始学Swift》学习笔记(Day 3)——Swift 2.0之后增加的关键字

    Swift 2.0学习笔记(Day 3)——Swift 2.0之后增加的关键字 原创文章,欢迎转载.转载请注明:关东升的博客 看了之前的学习笔记知道了什么是关键字,现在提示各位在Swift 2.0之后 ...

  8. 使用Nexus管理Maven仓库时,上传带依赖的第三方jar

    总所周知,使用Maven构建非常方便.在企业中使用Nexus创建私服来管理Maven时,需要上传很多没有开放源码的第三方Jar包.本文将讲述当第三方Jar包有很多并且互相有依赖时如何上传. 核心操作 ...

  9. 你意识到苹果公司已经抛弃了GC吗?

    为什么移动Web应用程序很慢(译) - tangzhnju - 博客园 http://www.cnblogs.com/codemood/p/3213459.html

  10. NSCache类的简单介绍

    最近看SDWebImage,里面的内存缓存用到了NSCache这个类,由于以前没有使用过,特此记录学习一下. NSCache NSCache是苹果官方提供的缓存类,用法和NSMutableDicton ...