题目:

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

  1. Leetcode75. Sort Colors颜色分类

    给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 0. 1 和 2 分别表示红色.白色和蓝色. ...

  2. 【LeetCode】Sort Colors

    Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...

  3. 52. Sort Colors && Combinations

    Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...

  4. Lintcode: Sort Colors II

    Given an array of n objects with k different colors (numbered from 1 to k), sort them so that object ...

  5. Lintcode: Sort Colors II 解题报告

    Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects ...

  6. 75. Sort Colors(颜色排序) from LeetCode

      75. Sort Colors   给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和2分别表示红色, ...

  7. Sort Colors I & II

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

  8. 【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 ...

  9. LeetCode: Sort Colors 解题报告

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

随机推荐

  1. 20190807-RP-Explosion

    如题,RP爆发后爆炸了. 话说在七夕这天考试? 那么? 黑暗又来临了? 没有人见过那一幕. 在如漆般胶着的黑暗中, Struggle?Dying? 用鲜血浇灌花朵,用死亡迎接明天. 考试过程: 看看三 ...

  2. mysql sum() 求和函数的用法

    查询在record表中 name=? 的 money 加起来的值使用聚和函数 sum() 求和select sum(money) from record t where t.name = ?另外:co ...

  3. LintCode_415 有效回文串

    给定一个字符串,判断其是否为一个回文串.只包含字母和数字,忽略大小写. 注意事项 你是否考虑过,字符串有可能是空字符串?这是面试过程中,面试官常常会问的问题. 在这个题目中,我们将空字符串判定为有效回 ...

  4. LintCode_41 最大子数组

    题目 给定一个整数数组,找到一个具有最大和的子数组,返回其最大和. 注意事项 子数组最少包含一个数 样例 给出数组[−2,2,−3,4,−1,2,1,−5,3],符合要求的子数组为[4,−1,2,1] ...

  5. MYSQL基础常识

    所有的数据库名.表名.表字段都是区分大小写的.所以在使用mysql命令时需要输入正确的名称 MYSQL命令终止符是分号; 1.MYSQL的连接:mysql -u root -p(\q或exit退出); ...

  6. appium+python 启动一个app步骤

    询问度娘搭好appium和python环境,开启移动app自动化的探索(基于Android),首先来记录下如何启动待测的app吧! 如何启动APP?1.获取包名:2.获取launcherActivit ...

  7. Jquery选择器分类:基本选择器,层次选择器,过滤选择器,表单选择器。

    基本选择器 说明:通过元素id.class和标签名等来查找DOM元素 1.id选择器:$("#test");//选取id为test的元素 2.类选择器:$(".test& ...

  8. LUOGU 1278 单词游戏

    题目描述 Io和Ao在玩一个单词游戏. 他们轮流说出一个仅包含元音字母的单词,并且后一个单词的第一个字母必须与前一个单词的最后一个字母一致. 游戏可以从任何一个单词开始. 任何单词禁止说两遍,游戏中只 ...

  9. web前端学习(二)html学习笔记部分(10)-- HTML5构建应用布局和页面

    1.2.25  HTML5构建应用布局和页面 1.2.25.1  HTML5在移动开发中的准则 1.尽量使用单页面开发 2.慎重选择前端UI框架 3.动画.特效使用准则(60fps) 浏览器消耗最小的 ...

  10. python 单元测试之初次尝试

    python 语言中有很多单元测试框架和工具,而unittest单元测试框架作为标准python语言中的一个模块.是其他框架和工具的基础.想要进行单元测试,我们需要使用到unittest框架中的功能. ...