75.[LeetCode] Sort Colors
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?
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--;
}
}
}
};
解释:
The solution requires the use of tracking 3 positions, the Low, Mid and High.
We assume that the mid is the "Unknown" area that we must evaluate.
If we encounter a 0, we know that it will be on the low end of the array, and if we encounter a 2, we know it will be on the high end of the array.
To achieve this in one pass without preprocessing (counting), we simply traverse the unknown will generating the low and high ends.
Take this example:
Assume our input is: 1 0 2 2 1 0 (short for simplicity).
Running the algorithm by hand would look something like:
^ ^
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
75.[LeetCode] Sort Colors的更多相关文章
- LeetCode: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
- 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] 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 ...
- [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个类型的排序
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 ...
随机推荐
- new和delete的三种形式详解
一.new操作符.delete操作符 class String { public: String(const char *str="") { if(str== NULL) { da ...
- sublime Text3安装 markdownediting 报错 解决记录
看了一下官方文档,也是醉了,都怪自己的无知. 在安装时候不要打开.md的文件,因为你里面有可能有一些语法错误,所以会导致报错. 解决方法关闭其他文件,在重新安装! 官方解释: 参考文档:Trouble ...
- MySQL学习【第八篇索引优化】
一.建立索引的原则(规范) 1.选择唯一性索引 只要可以创建唯一性索引的,一律创建唯一索引(因为速度快呀) 判断是否能创建唯一索引,用count(列名),count(distinct(列名))一样就能 ...
- python使用tablib库生成xls表格
参考文档:http://python-tablib.org Tablib是一个MIT许可的格式不可知的表格数据集库.它允许您导入,导出和操作表格数据集.高级功能包括隔离,动态列,标签和过滤,以及无缝格 ...
- 移动端触摸滑动插件Swiper使用指南
Swiper是一款开源.免费.功能十分强大的移动端内容触摸滑动插件,目前的最新版本是Swiper4.Swiper主要面向的是手机.平板电脑等移动设备,帮助开发者轻松实现触屏焦点图.触屏Tab切换.触屏 ...
- 基于 HTML5 WebGL 智能城市的模拟运行
前言 智能城市是一个系统.也称为网络城市.数字化城市.信息城市. 智能城市建设是一个系统工程:首先实现的是城市管理智能化,由智能城市管理系统辅助管理城市,通过管理系统人们可以监视城市的运行,了解城市每 ...
- 高性能MySQL--MySQL数据类型介绍和最优数据类型选择
MySQL支持的数据类型很多,那么选择合适的数据类型对于获得高性能就至关重要.那么就先了解各种类型的优缺点! 一.类型介绍 1.整型类型 整型类型有: TINYINT,SMALLINT,MEDIUMI ...
- mitmproxy 在windows上的使用
mitmproxy 是一个中间件代理, 结合python使用 安装 pip install mitmproxy 在windows上没有mitmproxy 所以只要用mitmdump和mitmwdb ...
- 基于visual studio 2017 以及cubemx 搭建stm32的开发环境(2)
主要解决 vs2017中,printf无法打印数据的问题. 在keil环境下正常使用printf功能,但是以下的重定向代码在vs2017下使用不了: #ifdef __GNUC__ /* With G ...
- Python学习之——Oracle数据库连接
一.安装Oracle客户端 1.下载对应安装文件,官网地址:http://www.oracle.com/technetwork/database/database-technologies/insta ...