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的更多相关文章

  1. LeetCode: Sort Colors 解题报告

    Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...

  2. LeetCode(75) Sort Colors

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

  3. [LeetCode] Sort Colors 颜色排序

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

  4. [leetcode]Sort Colors @ Python

    原题地址:https://oj.leetcode.com/problems/sort-colors/ 题意: Given an array with n objects colored red, wh ...

  5. [Leetcode] Sort Colors (C++)

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

  6. [LeetCode] Sort Colors 对于元素取值有限的数组,只遍历一遍的排序方法

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

  7. [LeetCode] Sort Colors 只有3个类型的排序

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

  8. LeetCode Sort Colors (技巧)

    题意: 一个数组只可能含有3种数据,分别为1,2,3,请将数组排序(只能扫一遍). 思路: 如果扫两遍的话,用个桶记录一下,再赋值上去就行了. class Solution { public: voi ...

  9. 【LeetCode】Sort Colors 数组排序

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

随机推荐

  1. 登录SQLServer报错:无法连接到DESKTOP-LDJHQGN

    解决方法:下↓↓↓

  2. Java 常用IO流操作详解

    1.基本概念 IO:Java对数据的操作是通过流的方式,IO流用来处理设备之间的数据传输,上传文件和下载文件,Java用于操作流的对象都在IO包中. 2.IO流的分类 图示:(主要IO流) 3.字节流 ...

  3. mpvue微信小程序开发随笔

    mpvue上手很快,学习成本低,目前是开源的,适合技术实力不是很强的公司采用. spring boot 做后台,开发效率杠杠的.建议会java的开发尽量使用spring boot 开发,省事. 最近用 ...

  4. Ajax缓存

    一.Ajax缓存的好处 这种设计使客户端对一些静态页面内容的请求,比如图片,css文件,js脚本等,变得更加快捷,提高了页面的响应速度,也节省了网络通信资源. 二.Ajax缓存的不足 Ajax缓存虽然 ...

  5. python if-elif-else 结构判断输入值处于何种年龄段

    输入变量 age 的值,再编写一个 if-elif-else 结构,根据 age的值判断处于人生的哪个阶段.如果一个人的年龄小于 2岁,就打印一条消息,指出他是婴儿.如果一个人的年龄为 2(含)-4岁 ...

  6. shell习题第10题:打印每个单词的字数

    [题目要求] 用shell打印下面这句话中字母数小于6的单词. Bash also interprets a number of multi-character options. [核心要点] for ...

  7. centos7添加新网卡实现双IP双网关

     问题背景: 业务需要,针对业务需要不同地域的机构访问,所以需要在同一台机器上配置不同IP并配置不同网关,实现不用机构可以访问同一台服务器办理业务. 系统环境: centos linux7 网络环境: ...

  8. Windows 安装Redis程序

    一.系统环境 1.硬件系统:Windows7 64位 2.软件环境: Redis 64位 3.2.100.Redis Desktop Manager. 二.Redis安装 下载地址:https://g ...

  9. echarts 地图 免费离线js,json包分享

    最近,项目中需要用到地图,由于项目的特殊性,只能使用内网获取数据. 然而,echarts官网上的离线地图包(http://echarts.baidu.com/download-map.html)早在一 ...

  10. python学习之网络编程基础

    引入场景:客户与银行关系 银行职员负责给客户提供取钱服务,客户通过账户密码跟银行职员建立合作关系.此时银行职员就可以作为服务器,当用户A取完钱后他需要等待下一个用户的接入,用户的账号密码就是建立合作关 ...