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. 新概念英语(1-101)A Card From Jimmy

    Lesson 101 A card from Jimmy 吉米的明信片 Listen to the tape then answer this question. Does Grandmother s ...

  2. 【原生js实现一键回到顶部】

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  3. POJ-1556 The Doors---线段相交+最短路

    题目链接: https://vjudge.net/problem/POJ-1556 题目大意: 给一个10*10的正方形房间中间用墙隔开每个墙上有两个门,给出门的两个端点坐标求从左边中点走到右边中点所 ...

  4. [论文阅读] ImageNet Classification with Deep Convolutional Neural Networks(传说中的AlexNet)

    这篇文章使用的AlexNet网络,在2012年的ImageNet(ILSVRC-2012)竞赛中获得第一名,top-5的测试误差为15.3%,相比于第二名26.2%的误差降低了不少. 本文的创新点: ...

  5. 分析ajax请求抓取今日头条关键字美图

    # 目标:抓取今日头条关键字美图 # 思路: # 一.分析目标站点 # 二.构造ajax请求,用requests请求到索引页的内容,正则+BeautifulSoup得到索引url # 三.对索引url ...

  6. tkinter打招呼

    import tkinter as tk #导入tkinter模块声明为tk class App:#创建一个类名称为App def __init__(self,master):#传入的参数顶层窗口在这 ...

  7. [LeetCode] Prefix and Suffix Search 前后缀搜索

    Given many words, words[i] has weight i. Design a class WordFilter that supports one function, WordF ...

  8. [LeetCode] Delete Operation for Two Strings 两个字符串的删除操作

    Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 t ...

  9. 【温故而知新】HTTP 概述

    什么是 HTTP 官方解释是 "因特网的多媒体信使",通俗点说,就是个送信的.电话机出来之前,人与人(有一定距离)之间的沟通基本靠写信,然后由快递员送发.如果把 web 服务器和客 ...

  10. 机器学习基石:16 Three Learning Principles

    三个理论上界: 三个线性模型: 三个关键工具: 三条学习规则: 1.奥卡姆剃刀定律 先从简单模型开始, 训练后出现欠拟合, 再尝试复杂点模型. 2.采样误差 训练.验证.测试数据尽量同分布. 3.数据 ...