[LeetCode] 75. 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?
这道题的本质还是一道排序的题,题目中给出提示说可以用计数排序,需要遍历数组两遍,那么先来看这种方法,因为数组中只有三个不同的元素,所以实现起来很容易。
- 首先遍历一遍原数组,分别记录 0,1,2 的个数。
- 然后更新原数组,按个数分别赋上 0,1,2。
解法一:
class Solution {
public:
void sortColors(vector<int>& nums) {
vector<int> colors();
for (int num : nums) ++colors[num];
for (int i = , cur = ; i < ; ++i) {
for (int j = ; j < colors[i]; ++j) {
nums[cur++] = i;
}
}
}
};
题目中还要让只遍历一次数组来求解,那么就需要用双指针来做,分别从原数组的首尾往中心移动。
- 定义 red 指针指向开头位置,blue 指针指向末尾位置。
- 从头开始遍历原数组,如果遇到0,则交换该值和 red 指针指向的值,并将 red 指针后移一位。若遇到2,则交换该值和 blue 指针指向的值,并将 blue 指针前移一位。若遇到1,则继续遍历。
解法二:
class Solution {
public:
void sortColors(vector<int>& nums) {
int red = , blue = (int)nums.size() - ;
for (int i = ; i <= blue; ++i) {
if (nums[i] == ) {
swap(nums[i], nums[red++]);
} else if (nums[i] == ) {
swap(nums[i--], nums[blue--]);
}
}
}
};
当然我们也可以使用 while 循环的方式来写,那么就需要一个变量 cur 来记录当前遍历到的位置,参见代码如下:
解法三:
class Solution {
public:
void sortColors(vector<int>& nums) {
int left = , right = (int)nums.size() - , cur = ;
while (cur <= right) {
if (nums[cur] == ) {
swap(nums[cur++], nums[left++]);
} else if (nums[cur] == ) {
swap(nums[cur], nums[right--]);
} else {
++cur;
}
}
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/75
类似题目:
参考资料:
https://leetcode.com/problems/sort-colors/
https://leetcode.com/problems/sort-colors/discuss/26500/Four-different-solutions
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 75. Sort Colors 颜色排序的更多相关文章
- 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(颜色排序) from LeetCode
75. Sort Colors 给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和2分别表示红色, ...
- 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 计数排序,三路快排
解法一:计数排序:统计0,1,2 的个数 时间复杂度:O(n) 空间复杂度:O(k) k为元素的取值范围, 此题为O(1) class Solution { public: void sortC ...
- 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] 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
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
随机推荐
- 未初始化内存检测(MSan)
https://github.com/google/sanitizers/wiki https://github.com/google/sanitizers/wiki/MemorySanitizer ...
- ubuntu删除文件和文件夹的rm命令
在Ubuntu中好多文件或文件夹是不能使用右键删除的,因此知道删除文件或文件夹的rm命令显得尤为重要. rm命令的语法 rm [选项] 文件名或文件夹名 rm命令的一些选项 -f.--force 强力 ...
- Type Erasure with Pokemon---swift的类型擦除
我感觉这个是swift的设计缺陷. 类型擦除:解决泛型类型作为公用类型的问题 是抽象的公用机制的一种实现方式. 1)类型擦除并不能解决类型不一致的兼容问题,只能解决类似继承一致性的兼容问题. 2)擦除 ...
- C# NPOI Excel
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- 对象数组自定义排序--System.Collections.ArrayList.Sort()
使用System.Collections.ArrayList.Sort()对象数组自定义排序 其核心为比较器的实现,比较器为一个类,继承了IComparer接口并实现int IComparer.Com ...
- C#中将long浮点数格式化为{H:min:s.ms}格式的字符串的方法
场景 表示时间的数据格式为浮点数,如下: 需要将其格式化为{H:min:s.ms}格式的字符串,效果如下: 注: 博客主页:https://blog.csdn.net/badao_liumang_qi ...
- 元素增删事件DOMNodeInserted和DOMNodeRemoved
监听元素变化的三种方法: 对于表单类型的控件,使用onchange事件最好. 使用DOMNodeInserted和DOMNodeRemoved事件 使用定时器定时检测(下策) 有时需要给一个class ...
- WebService 创建、发布、调用
环境Win7+VS2017 启用IIS 查看iis是否启用 新建 ASP.NET Web 应用程序 项目,项目中添加Web 服务 在 asmx 文件中添加需要的方法 运行结果 发布 创建新的文件夹, ...
- Google Analytics 学习笔记一 —— GA简介
GA的原理 网页页面添加GA跟踪代码,以"一像素"传递信息给服务器 hit(交互) --> sessions(会话) --> user(用户) 竞品对比 Firebas ...
- Innodb整体架构
如下图展示了Innodb内存中和磁盘的结构: 内存中结构主要有如下几种: buffer pool change buffer adaptive hash index (自适应的hash索引) Log ...