【一天一道LeetCode】#75. Sort Colors
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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?
(二)解题
题目大意:数组包含0,1,2,按照大小排列这些数字。
1、最简单的方法
利用STL的sort一句话就解决了。
class Solution {
public:
void sortColors(vector<int>& nums) {
sort(nums.begin(),nums.end());
}
};
2、两个指针
算法思想很简单,把所有的0交换到队首,把所有的1交换到队尾
class Solution {
public:
void sortColors(vector<int>& nums) {
int start = 0;
int end = nums.size()-1;
for(int i = 0 ; i<nums.size();i++)//把0交换到前面
{
if(nums[i]==0) {
if(i!=start) swap(nums[i],nums[start]);
start++;
}
}
for(int i = nums.size()-1 ; i>=start ;i--)//把2交换到尾部
{
if(nums[i]==2){
if(i!=end) swap(nums[i],nums[end]);
end--;
}
}
}
};
3、两个指针优化版
维持三个指针i、j和k,从0~i表示1,i+1~j表示1,k~nums.size()表示2
代码也比较简单:
class Solution {
public:
void sortColors(vector<int>& nums) {
int i = 0, j = i, k = nums.size() - 1;
while(j <= k){
if(nums[j] == 0) swap(nums[i++], nums[j++]);//0交换到前面
else if(nums[j] == 1) j++;//1保持不动
else swap(nums[k--], nums[j]);//2交换到尾部
}
}
};
【一天一道LeetCode】#75. Sort Colors的更多相关文章
- 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 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 ...
- Leetcode 75. 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 in-place so that objects of the ...
- LeetCode 75 Sort Colors(颜色排序)
翻译 给定一个包括红色.白色.蓝色这三个颜色对象的数组.对它们进行排序以使同样的颜色变成相邻的,其顺序是红色.白色.蓝色. 在这里,我们将使用数字0.1和2分别来代表红色.白色和蓝色. 原文 Give ...
- leetCode 75.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 in-place so that objects of the ...
- leetcode 75 Sort Colors 计数排序,三路快排
解法一:计数排序:统计0,1,2 的个数 时间复杂度:O(n) 空间复杂度:O(k) k为元素的取值范围, 此题为O(1) class Solution { public: void sortC ...
- LeetCode 75. Sort Colors (python一次遍历,模拟三路快排)
LeetCode 75. Sort Colors (python一次遍历,模拟三路快排) 题目分析: 本题需要实现数字只包含0,1,2的排序,并且要求一次遍历. 由于只用把数字隔离开,很容易想到快排的 ...
随机推荐
- java绘图原理------在窗口界面(或面板上)画出一张或多张图片问题解决方法
/** *@author blovedr * 功能: java绘图原理------在窗口界面(或面板上)画出一张或多张图片问题解决方法 * 日期: 2018年4月28日 16:20 * 注释: ...
- 判定程序员等级,HashMap就够了
JDK1.8 HashMap源码分析 用到的符号: ^异运算:两个操作数相同,结果是;两个操作数不同,结果是1. &按位与:两个操作数都是1,结果才是1. 一.HashMap概述 在JDK1 ...
- Node.js 常用工具util
util 是一个Node.js 核心模块,提供常用函数的集合,用于弥补核心JavaScript 的功能 过于精简的不足. util.inherits util.inherits(constructor ...
- OO第一阶段总结
OO第一阶段总结 一.各次作业程序结构 第一次作业 第一次作业由于初用JAVA,还没有深刻理解面向对象的编程方法,故在编程过程中只用了一个类,一个方法,即完成了相应的程序功能.这必然不是本课程的目的, ...
- Linux 下不经过BIOS重启(i386)
前段时间有个项目,要求在Linux下不经过BIOS重启,i386平台. 一.可行性分析 众所周知,BIOS中包含了CPU及其他各种设备的初始化代码,Linux系统运行之后是否能够将各种用到的设备返回到 ...
- Android监听手机网络变化
Android监听手机网络变化 手机网络状态发生变化会发送广播,利用广播接收者,监听手机网络变化 效果图 注册广播接收者 <?xml version="1.0" encodi ...
- Spark Streaming + Kafka整合(Kafka broker版本0.8.2.1+)
这篇博客是基于Spark Streaming整合Kafka-0.8.2.1官方文档. 本文主要讲解了Spark Streaming如何从Kafka接收数据.Spark Streaming从Kafka接 ...
- [Flask]学习杂记--模板
这个学习杂记主要不是分享经验,更多是记录下falsk的体验过程,以后做东西在深入研究,因为django之前用的时间比较长,所以很多概念都是一看而过,做个试验了解下flask的功能. flask中使用是 ...
- 全文检索Lucene (2)
接着全文检索Lucene (1) . 下面我们来深入的研究一下,如何使用Lucene! 从全文检索Lucene (1)中我们可以看出,Lucene就好比一个双向的工作流,一方面是对索引库的维护,另一方 ...
- Struts 1 之<logic>标签库
1. logic:empty logic:empty标签是用来判断是否为空的.如果为空,该标签体中嵌入的内容就会被处理.该标签用于以下情况: 当Java对象为null时 当String对象为" ...