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 supposed to use the library's sort function for this problem.

这题就是想把乱序的[2 2 1 0 1 1 0 2]排列成有序的[0 0 1 1 1 2 2 2].

方法一:两趟循环,第一趟数0,1,2的个数,第二趟修改数组A, 思想简单且无编程难度.

\(O(2n)\) time, \(O(1)\) extra space.

// two-pass algorithm
// [ 2 2 1 0 1 1 0 2]
void sortColors(vector<int>& A) {
int c_0 = 0, c_1 = 0; for (int i = 0; i < A.size(); i++)
if (A[i] == 0) c_0++;
else if (A[i] == 1) c_1++; for (int i = 0; i < A.size(); i++)
if (i < c_0) A[i] = 0;
else if (i >= c_0 && i < (c_1 + c_0)) A[i] = 1;
else A[i] = 2;
}

方法二,人家的想法了,就是扫描数组,发现2就送入队尾方向,发现0就送队首方向,最后1自然就在中间,就排好了.这方法编程不容易.

设定 zero = 0, sec = A.size()-1 两个指针.
若 A[i] = 2, swap(A[i], A(sec--));
若 A[i] = 0, swap(A[i], A(zero++));
// one-pass algorithm
// [ 2 2 1 0 1 1 0 2]
void sortColors(vector<int>& A) {
int zero = 0, sec = A.size() - 1;
for (int i = 0; i <= sec; i++) {
while (A[i] == 2 && i < sec) swap(A[i], A[sec--]);
while (A[i] == 0 && i > zero) swap(A[i], A[zero++]);
}
}

75. Sort Colors(中等)的更多相关文章

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

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

  2. LeetCode 75. Sort Colors (颜色分类):三路快排

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  3. 刷题75. Sort Colors

    一.题目说明 题目75. Sort Colors,给定n个整数的列表(0代表red,1代表white,2代表blue),排序实现相同颜色在一起.难度是Medium. 二.我的解答 这个是一个排序,还是 ...

  4. 75. Sort Colors - LeetCode

    Question 75. Sort Colors Solution 题目大意: 给一个数组排序,这个数组只有0,1,2三个元素,要求只遍历一遍 思路: 记两个索引,lowIdx初始值为0,highId ...

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

  6. [LeetCode] 75. Sort Colors 颜色排序

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  7. Leetcode 75. Sort Colors

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

  8. 75. Sort Colors

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

  9. LeetCode OJ 75. Sort Colors

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

随机推荐

  1. j2ee基础(1)servlet的生命周期

    Servlet的生命周期 Servlet 生命周期规定了 Servlet 如何被加载.实例化.初始化. 处理客户端请求,以及何时结束服务. 该生命周期可以通过 javax.servlet.Servle ...

  2. setInterval()使用时易疏忽的点

    举个例子: 一道题目 这两个程序的区别就在于我向setInterval的参数一function写入了参数.这就是导致运行结果不尽如人意的原因. setInterval()方法可以接收三个参数,此参数会 ...

  3. 使用 C#/.NET Core 实现单体设计模式

    本文的概念内容来自深入浅出设计模式一书 由于我在给公司做内培, 所以最近天天写设计模式的文章.... 单体模式 Singleton 单体模式的目标就是只创建一个实例. 实际中有很多种对象我们可能只需要 ...

  4. 算法 排序NB二人组 堆排序 归并排序

    参考博客:基于python的七种经典排序算法     常用排序算法总结(一) 序前传 - 树与二叉树 树是一种很常见的非线性的数据结构,称为树形结构,简称树.所谓数据结构就是一组数据的集合连同它们的储 ...

  5. python基础——面向对象进阶

    python基础--面向对象进阶 1.isinstance(obj,cls)和issubclass(sub,super) isinstance(obj,cls)检查是否obj是否是类 cls 的对象 ...

  6. vue2路由

    我们在前面的学习过程中不管是在学习angular还是vue1,都会遇到二级路由,我们现在先来看一下vue2中的一级路由. 首先要引入的是vue2与路由文件. js代码: <script> ...

  7. Spring(六):Spring&Struts2&Hibernate搭建的blog项目

    经过断断续续的学习.累积,终于基于别人的开源blog项目,变成了自己的第一个相对完整点的blog项目. 计划暂时把这个blog程序暂停------有更多(工作中用到的)东西需要去做,因此学习SSH b ...

  8. Go-GRPC 初体验

    grpc 跟常见的client-server模型相似(doubbo)grpc 编码之前需要准备以下环境: 安装protobuf,grpc的client与server之间消息传递使用的protoc格式消 ...

  9. find()用法

    >>> str = '编程改变世界'>>> str.find('编')0>>> str.find('程')1>>> str.fi ...

  10. 《Java面向对象设计》

    <Java面向对象设计> 第一章 面向对象软件工程与UML p理解为什么需要软件工程 p掌握软件工程的基本概念 p掌握软件生命周期各个阶段的主要任务 p了解流行软件开发过程 p了解软件过程 ...