题目:

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. 容斥原理学习(Hdu 4135,Hdu 1796)

    题目链接Hdu4135 Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  2. springmvc 传参Required String parameter 'xxxx' is not present

    报错 请求因该是已经被分配了,但是参数补全,无法被执行 加上这个参数就好了,表示请求参数,可以为空 这样的好处是,可以进入controller之后再去判断,比较好定位错误

  3. 如何获取SpringBoot项目的applicationContext对象

    ApplicationContext对象是Spring开源框架的上下文对象实例,在项目运行时自动装载Handler内的所有信息到内存.传统的获取方式有很多种,不过随着Spring版本的不断迭代,官方也 ...

  4. 将Factory-boy生成的复杂对象转成dict的方法

    最近在做接口测试,使用Factory-boy来生成接口对象实例,接着将对象转成dict,最后通过requests发送请求. 对象转成dict,目前知道的方法就是object.__dict__ .这个方 ...

  5. javascript中uber实现子类访问父类成员

    function Animal(){} Animal.prototype={ name:"animal", toString:function(){ console.log(thi ...

  6. Codeforces Round #192 (Div. 2) A. Cakeminator【二维字符数组/吃掉cake,并且是一行或者一列下去,但是该行/列必须没有草莓的存在】

    A. Cakeminator time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  7. vue-waterfall2 基于Vue.js 瀑布流组件

    vue-waterfall2 1.宽度自适应,数据绑定特效(适用于上拉加载更多) 2.自定义程度高 3.使用极为简便,适用于PC/移动端 4.提供resize(强制刷新布局-适用于下拉刷新)/mix( ...

  8. Leetcode4.Median of Two Sorted Arrays两个排序数组的中位数

    给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 你可以假设 nums1 和 nums2 不同 ...

  9. 在liferay 7中如何删除service builder已经生成的数据库table

    在Liferay 7中,加了数据库保护机制,你改了service.xml的结构后,重新运行service builder,并不会帮你生成新的数据库表.然后你发现你在数据库中自己手动删除了表后,重新部署 ...

  10. JavaScript-- 函数既是函数又是对象

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...