【LeetCode】【定制版排序】Sort Colors
之前转载过一篇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的更多相关文章
- LeetCode 75. 颜色分类(Sort Colors) 30
75. 颜色分类 75. Sort Colors 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中, ...
- [leetcode.com]算法题目 - Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- LeetCode(75) Sort Colors
题目 Given an array with n objects colored red, white or blue, sort them so that objects of the same c ...
- [LeetCode] Merge Intervals 排序sort
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- 算法与数据结构基础 - 排序(Sort)
排序基础 排序方法分两大类,一类是比较排序,快速排序(Quick Sort).归并排序(Merge Sort).插入排序(Insertion Sort).选择排序(Selection Sort).希尔 ...
- 【LeetCode题解】排序
1. 排序 排序(sort)是一种常见的算法,把数据根据特定的顺序进行排列.经典的排序算法如下: 冒泡排序(bubble sort) 插入排序(insertion sort) 选择排序(selecti ...
- 75. Sort Colors(颜色排序) from LeetCode
75. Sort Colors 给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和2分别表示红色, ...
- [Leetcode Week2]Sort Colors
Sort Colors题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/sort-colors/description/ Description Give ...
- 【LeetCode】Sort Colors 数组排序
题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组.包括0,1,2 ...
随机推荐
- 【转载】利用MAVEN打包时,如何包含更多的资源文件
首先,来看下MAVENx项目标准的目录结构: 一般情况下,我们用到的资源文件(各种xml,properites,xsd文件等)都放在src/main/resources下面,利用maven打包时,ma ...
- phpexcel图形图表(一)入门
PHPExcel - Excel的PHP处理引擎 PHPExcel 提供了一系列的 PHP语言 类,让你可以轻松地读写操作以下格式的文件:.xls/.xlsx/.csv/.ods/Gnumeric/P ...
- PHP编译选项
PHP安装 ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/u ...
- TP3.2 引入微信类
首先建立一个入口IndexController.php文件 <?php namespace Home\Controller; use Think\Controller; use Com\Wech ...
- 如何创建Windows定时任务
我们经常使用电脑,有没有那么一个瞬间想着要是电脑可以每隔一段时间,自动处理一件事情就好了呢? 其实Windows还真有这样的功能,很多软件检测更新就是通过这个方法实现的. 这次我们来做一个简易的喝水提 ...
- python django -2 ORM模型
ORM简介 MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库 ORM是“对象-关系-映射”的简称 ...
- SharePoint让所有用户访问站点
SharePoint让所有用户访问站点,可用在用户组里面添加:NT AUTHORITY\authenticated users
- Java多线程的两种实现方式
Java总共有两种方式实现多线程 方式1:通过继承Thread类的方式 package com.day04; /** * 通过继承Thread类并复写run方法来是实现多线程 * * @author ...
- 查看java进程的所有信息
查看java 进程下的所有信息 ll /proc/pid/fd ru:ll /proc/24047/fd
- Anker—工作学习笔记
1.前言 最近在项目中用nginx做反向代理,需要动态生成nginx的配置.大概流程是用户在页面上新增域名.http或https协议以及端口信息,后台会根据域名自动生成一个nginx的server配置 ...