LeetCode75 Sort Colors
题目:
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. (Medium)
分析:
最直观的想法就是遍历一遍数个数,再遍历一遍按照0,1,2的个数赋值。但题目的follow-up中也提出了不要这么做。
一次遍历常数空间的算法就是采用双指针,模拟快排的思路移动元素。
p1左侧表示0(如果有),p2右侧表示2(如果有)。
这样一次遍历:
遇到0,则swap(nums[i], nums[p1]) p1++
遇到2,则swap(nums[i], nums[p2])p2--, 并且再判定一次i位置
遇到1则 不操作。
代码:
class Solution {
public:
void sortColors(vector<int>& nums) {
int p1 = , p2 = nums.size() - ;
for (int i = ; i <= p2; ++i) {
if (nums[i] == ) {
swap(nums[i], nums[p1]);
p1++;
}
else if (nums[i] == ) {
swap(nums[i], nums[p2]);
p2--;
i--;
}
}
return;
}
};
LeetCode75 Sort Colors的更多相关文章
- Leetcode75. Sort Colors颜色分类
给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 0. 1 和 2 分别表示红色.白色和蓝色. ...
- 【LeetCode】Sort Colors
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- 52. Sort Colors && Combinations
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- Lintcode: Sort Colors II
Given an array of n objects with k different colors (numbered from 1 to k), sort them so that object ...
- Lintcode: Sort Colors II 解题报告
Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects ...
- 75. Sort Colors(颜色排序) from LeetCode
75. Sort Colors 给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和2分别表示红色, ...
- Sort Colors I & II
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- 【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: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
随机推荐
- 正则中使用ASCII码,取值范围
[^\x00-\xFF] : 表示匹配Ascii码大于255的那些字符 基于浏览器的工具: https://regexr.com/
- id生成器
- 在Spring应用中创建全局获取ApplicationContext对象
在Spring应用中创建全局获取ApplicationContext对象 1.需要创建一个类,实现接口ApplicationContextAware的setApplicationContext方法. ...
- oracle习题练习-表空间-用户-表-约束
题一 1. 创建名字为hy_tablespace的表空间,默认大小为10M;@@ 2. 创建一个用户,用户名以自己名字命名,并指定命名空间为hy_tablespace;@@@@ ...
- centos 安装redis2.8.9
1没有安装gcc yum install gcc-c++ 2. 安装tcl yum install -y tcl 3.安装redis $ wget http://download.redis.io/r ...
- NOIP模拟赛 17.10.10
初次见面(firstmeet)[题目背景]雾之湖边,静得可怕.露米娅出神凝望.黑白连衣裙,像极了绽放的墨黑和洁白的莲.身边的雾之湖,倒映着血色天空.酒红的双眸,映照一切.低声浅笑,双臂伸直,她悄无声息 ...
- 小飞音箱wifi配网流程
音箱出货时,已经内置wifi,如果无法接通,按照如下方案执行: 小飞音箱wifi配网流程 0. 接通音箱电源 通电3分钟后,音箱如果显示红色光圈,表示未联网,则需要手动联网 1. 手机下载小飞在线ap ...
- js 详解setTimeout定时器
setTimeout: 定时器函数 第一个参数是匿名函数,第二个参数是延迟执行时间 setTimeout(function(){},time) 注意: 1.setTimeout函数是Window对象提 ...
- Python3.7.4入门-6/7错误和异常/类
6 错误和异常 while True: try: x = int(input("Please enter a number: ")) break except ValueError ...
- Direct2D 第6篇 绘制多种风格的线条
原文:Direct2D 第6篇 绘制多种风格的线条 上图是使用Direct2D绘制的线条,Direct2D在效率上比GDI/GDI+要快几倍,GDI/GDI+绘图是出了名的"慢", ...