题目:

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. Firefox Developer Edition - Mozilla

    冰狐浏览器开发者工具:https://www.mozilla.org/en-US/firefox/developer/ 冰狐浏览器开发者工具:https://www.mozilla.org/en-US ...

  2. poj 3304 Segments(计算直线与线段之间的关系)

    Segments Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10921   Accepted: 3422 Descrip ...

  3. css 始终让图片占满自适应盒子(图片不失真)

    要去上班了,时间比较紧,先把代码粘出来,原理慢慢讲 我来了,今天是农历七月八日,昨天是七夕,不知道为什么,突然通知放假半天(嘎嘎),好吧,没什么!!!走到半路的我看到通知,立马撤了.正好回来把这个原理 ...

  4. 基础架构:一条SQL查询语句是如何执行的?

    https://time.geekbang.org/column/article/68319?code=pEYaxHnjO23LQHW4CJgL706EXlpAJnbOOiT2y42cWwU%3D 这 ...

  5. 2019.9.16 csp-s模拟测试44 反思总结

    虽然说好像没有什么写这个的价值OAO 来了来了来写总结了,不能怨任何东西,就是自己垃圾x 开题顺序又和主流背道而驰,先一头扎进了公认最迷的T2,瞎搞两个小时头铁出来,然后T1和T3爆炸.基础很差,全靠 ...

  6. 2018-8-10-如何移动-nuget-缓存文件夹

    title author date CreateTime categories 如何移动 nuget 缓存文件夹 lindexi 2018-08-10 19:16:51 +0800 2018-2-13 ...

  7. 【CODEVS】倒水问题

    题目描述: 有两个无刻度标志的水壶,分别可装 x 升和 y 升 ( x,y 为整数且均不大于 100 )的水.设另有一水 缸,可用来向水壶灌水或接从水壶中倒出的水, 两水壶间,水也可以相互倾倒.已知 ...

  8. js实现使用递归来计算1~任意数字的和

    function getSum(n){ if(n==1){ return 1; } return n+getSum(n-1); } var res = getSum(100); console.log ...

  9. Maple拥有优秀的符号计算和数值计算能力

    https://www.maplesoft.com/products/maple/ Maple高级应用和经典实例: https://wenku.baidu.com/view/f246962107221 ...

  10. LeetCode141 Linked List Cycle. LeetCode142 Linked List Cycle II

    链表相关题 141. Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can y ...