题目:

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. python基础--socket套接字、粘包问题

    本地回环地址:127.0.0.1 简易版服务端: import socket ​ server = socket.socket() # 就比如买了一个手机 server.bind(("127 ...

  2. torch.backends.cudnn.benchmark = true 使用情形

    设置这个 flag 可以让内置的 cuDNN 的 auto-tuner 自动寻找最适合当前配置的高效算法,来达到优化运行效率的问题. 应该遵循以下准则: 如果网络的输入数据维度或类型上变化不大,设置  ...

  3. LAMP标准化安装

    操作系统说明: 操作系统 版本 linux red hat release 6.4 关键软件包说明: 软件包 版本 目录 运行用户 httpd-2.2.27.tar.gz 2.2.27 /usr/lo ...

  4. Quick BI取数模型深度剖析

    开发图表最关键的点在于选择准确的图表类型展示准确的数据,而准确的数据往往依赖于一个强大的取数模型,因此设计一个好的取数模型不仅可以解决数据安全的问题,更可以帮助每个访问者高效触达自己想要的数据,开发者 ...

  5. 光(mirror room)

    /* 光线只有遇上边界或堵塞的格子才会改变方向,所以改变方向的位置是有限的,光线的方向又最多只有四种,所以光线在循环之前改变方向的次数是O(n+m+k)级别的.我们可以模拟光线的移动.已知光线位置和光 ...

  6. excel怎么制作实线虚线混排的折线图

    excel怎么制作实线虚线混排的折线图 excel怎么制作实线虚线混排的折线图?excel表格中想要设计的图表是实线的,想要让图标同时显示虚线和实线,该怎么操? 通常在在使用折线图描述数据的趋势时,前 ...

  7. 基于docker的php调用基于docker的mysql数据库的方法

    1:建立基于docker的mysql,参考 Mac上将brew安装的MySql改用Docker执行 2:建立基于docker�php image 在当前目录,建立Dockerfile,内容如下 FRO ...

  8. qt获取屏幕

    m_pDesktopWidget = QApplication::desktop(); // 屏体数量,包含扩展屏 int screenCount = m_pDesktopWidget->scr ...

  9. 重温Observer模式--热水器·改

    引言 在 C#中的委托和事件 一文的后半部分,讲述了Observer(观察者)模式,并使用委托和事件实现了这个模式.实际上,不使用委托和事件,一样可以实现Observer模式.在本文中,我将使用GOF ...

  10. 连接池dbcp

    连接池dbcp DBCP:apache组织 使用步骤: 1.导入jar包(commons-dbcp-1.4.jar和commons-pool-1.5.6.jar.commons-logging-1.2 ...