之前转载过一篇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. 利用ascx输出knockoutjs的模板

    项目里面的UI模板在一个页面中有2K多行了.需要增加新的UI样式.问题来了.加上js代码,几乎是变成了不可维护的状态.增加和修改都需要用ctrl+f的方式找到对应的模板,然后进行处理.很容易出错.突然 ...

  2. neocomplcache 自动补全

    key description ctrl+n 当前词补全项,下一个 ctrl+p 当前词补全项,上一个 :help NERDTree NERDTree帮助手册

  3. 用关键字interface定义接口,通过关键字implements来实现接口

    [定义]Java中,能够完成特定功能的,由若干属性和方法组织成的,相对独立的属性和方法的集合. [用途]实现类的多继承,以解决Java只能单继承,不支持多继承的问题. [特点] 用关键字interfa ...

  4. (转载)Unity3D研究院之使用 C#合成解析XML与JSON(四十一)

    XML与JSON在开发中非常重要, 其实核心就是处理字符串.一个是XML的字符串一个是JSON的字符串,尤其是在处理网络请求的时候,肯定是要用的.另外现在JSON非常的流行,我写了一个简单的例子融合了 ...

  5. Laravel5.1 路由 -路由分组

    路由分组有啥好处? 有时候啊 一大堆路由它们都有共同的地方,比如都使用一个中间件(过两天写)或是前缀都一样,避免代码重复 我们可以将他们分到一组中. 1 路由分组可以共享哪些属性? 中间件 middl ...

  6. 如何导入和导出应用数据通过电子邮件应用程序在你的IOS

     本文转载至 http://blog.csdn.net/zaitianaoxiang/article/details/6657887 http://yueding920.blog.163.com/bl ...

  7. 如何在MySQL客户端Navicat 上创建存储过程返回结果集

    下述引用自<MySQL 必知必会> MySQL命令行客户机的分隔符 如果你使用的是MySQL命令行实用程序,应该仔细阅读此说明.默认的MySQL语句分隔符为;(正如你已经在迄今为止所使用的 ...

  8. 《从零开始学Swift》学习笔记(Day 9)——离开表达式你试试!

    原创文章,欢迎转载.转载请注明:关东升的博客 表达式啊是很重要地. 在Swift中,表达式有3种形式. 不指定数据类型 var a1 = 10 指定数据类型 var a1:Int  = 10 使用分号 ...

  9. Sleep Buddies

    Sleep Buddies time limit per test 2.0 s memory limit per test 256 MB input standard input output sta ...

  10. Xcode生成ipa文件

    想到蒲公英应用做分发测试的同学们可以用的到哈 在测试的Device中切换为IOS Device,选择当前项目修改BuildSeting 在code signing Identity中选择证书,没有测试 ...