75. Sort Colors (Array)
Given an array with n objects colored red, white or blue, sort them 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.
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 an one-pass algorithm using only constant space?
思路: 使用两个指针,分别指向最后一个红色的后一个位置,以及第一个蓝色蓝色的前一个位置。如果当前扫描到的元素是红色,那么与第一个指针互换,若为蓝色,与蓝色头部指针互换,若为白色,不做任何操作。
class Solution {
public:
void sortColors(vector<int>& nums) {
int n = nums.size();
int redTail = ;
int blueHead = n-;
int tmp; for(int i = ; i <= blueHead;){
if(nums[i]==){//当前是红色
if(nums[redTail]==){//红色尾指针指向红色(说明没有白色)
redTail++;
i++; //不处理当前元素
continue;
} tmp = nums[redTail];
nums[redTail] = nums[i];
redTail++;
if(tmp==){ //红色尾指针指向白色
nums[i] = tmp; //交换红色和白色
i++;
}
else{ //红色尾指针指向蓝色(说明目前没有白色)
nums[i] = nums[blueHead]; //把blueHead处的元素放到i
nums[blueHead]=tmp; //把蓝色放到blueHead处
blueHead--;
}
}
else if(nums[i]==){//如果当前是白色,不处理当前元素
i++;
}
else{ //如果当前是蓝色
tmp = nums[blueHead];
nums[blueHead] = nums[i];//把蓝色放到blueHead处
nums[i] = tmp;//把blueHead处的元素放到i
blueHead--;
}
}
}
};
75. Sort Colors (Array)的更多相关文章
- LeetCode 75. Sort Colors (颜色分类):三路快排
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- 75. Sort Colors(颜色排序) from LeetCode
75. Sort Colors 给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和2分别表示红色, ...
- 刷题75. Sort Colors
一.题目说明 题目75. Sort Colors,给定n个整数的列表(0代表red,1代表white,2代表blue),排序实现相同颜色在一起.难度是Medium. 二.我的解答 这个是一个排序,还是 ...
- 75. Sort Colors - LeetCode
Question 75. Sort Colors Solution 题目大意: 给一个数组排序,这个数组只有0,1,2三个元素,要求只遍历一遍 思路: 记两个索引,lowIdx初始值为0,highId ...
- 【LeetCode】75. Sort Colors (3 solutions)
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- [LeetCode] 75. Sort Colors 颜色排序
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- Leetcode 75. Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- 75. Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- LeetCode OJ 75. Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
随机推荐
- gradle asciidoc 使用
备注: 次文档参考github 例子 1.环境准备 node npm (yarn) java KindleGen 备注: 具体的安装可以参考网上相关文档,KindleGen 下载地址:htt ...
- 随着tomcat一起启动一个线程
package test; import javax.servlet.*; public class MyCode implements ServletContextListener { //当Tom ...
- ASP.NET常用标准配置web.config
在我们的项目开发过程中,我们经常要配置wei.config文件,而大多数的时候配置差不多,下面的是一个简单的配置,其他的配置可以在这个基础上在添加 <?xml version="1.0 ...
- Linux之 手动释放内存
我们在进程中要怎样去描述一个文件呢?我们用目录项(dentry)和索引节点(inode).它们的定义如下: 所谓"文件", 就是按一定的形式存储在介质上的信息,所以一个文件其实包含 ...
- java代码=====实现修改while()
总结: package com.mmm; public class cse { public static void main(String[] args) { // int count=0;你妹,我 ...
- 模块初识2-模块的默认保存路径Python36-32\\lib\\site-packages,Python36-32\,标准库和第三方库
import 可以直接导入当前目录的其他脚本 如果你把login.py移动到new_dir的目录下,那么就会提示找不到模块: 要解决这个问题,有两个方法: 1.把login.py复制到C:\\User ...
- JAVA for循环语句的循环变量类型问题
class HalfDollars { public static void main(String [] arguments) { int[] denver = {1_900_000,1_700_0 ...
- supervisor+uwsgi+django遇到writing to a closed pipe/socket/fd解决
原因: 最近开发的一个项目,由于有个更新job需要消耗的时间非常长,一度以为更新出现了错误. 经过: 于是打开debug模式测试, 异常开启,调试发现system返回了 writing to a cl ...
- Monkey测试工具介绍
---------------------------------------------------------------------------------------------------- ...
- linux系统构架 - LB集群之LVS的NAT
1.环境说明 三台服务器,一台叫dir,两台叫rs1和rs2 (director 和 real server) dir外网ip:192.168.192.129 内网ip:192.168.1.114 ...