一. 题目描写叙述

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.

Follow up:

A rather straight forward solution is a two-pass algorithm using counting sort.

First, iterate the array counting number of 0’s, 1’s, and 2’s, then overwrite array with total number of 0’s,

then 1’s and followed by 2’s.

Could you come up with an one-pass algorithm using only constant space?

二. 题目分析

题目一開始说到一组对象,包括红白蓝三种颜色。然后要对他们进行排序,说白了就是对一个仅仅含有0, 1, 2三个数字的数组从小到大排序。

题目要求:come up with an one-pass algorithm using only constant space,因此仅仅能扫描一次。这里採取的方法是,统计0, 1, 2三个数字分别出现的次数。再将数组nums又一次构建为从0到2排列的数组,这样的方法没有使用数组元素间的交换,仅仅需扫描一次nums。

三. 演示样例代码

#include <iostream>
#include <vector> using namespace std; class Solution {
public:
void sortColors(vector<int>& nums)
{
int SIZE = nums.size();
int count[3] = {0, 0, 0};
for (int i = 0; i < SIZE; ++i)
++count[nums[i]]; for (int i = 0, index = 0; i < 3; ++i)
for (int j = 0; j < count[i]; ++j)
nums[index++] = i;
}
};

一个測试结果:

四. 小结

本题的解法还是挺多的,可多參考网上的其它解法。

leetcode笔记:Sort Colors的更多相关文章

  1. [Leetcode Week2]Sort Colors

    Sort Colors题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/sort-colors/description/ Description Give ...

  2. 【LeetCode】Sort Colors 数组排序

    题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组.包括0,1,2 ...

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

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

  4. 【LeetCode】Sort Colors

    Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...

  5. LeetCode 75. Sort Colors(排序颜色)

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

  6. [LeetCode题解]: Sort Colors

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given an a ...

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

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

  8. Leetcode 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】Sort Colors(middle)☆

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

  10. Java for LeetCode 075 Sort Colors

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

随机推荐

  1. C内存管理一 概述

    我们写了这么多年的程序猿.可能理论方面还比不上大学生.有人 "嘘"我了,假设有能回答下面几个问题的同学请举手: 1.面试常常遇到:同学请说说堆栈的差别? 2.同学请说说一个函数在堆 ...

  2. numeric and int in sql server

    类型映射 https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql-server-data-type-mappings C#关 ...

  3. centos 服务器配置注意项

    Mysql 出现Table‘xxx’is read only问题 Mysql数据库在由Mssql数据库导入数据文件后出现“ERROR 1036 (HY000): Table 'xxxx' is rea ...

  4. The in operator

    The operators we have seen so far are all special characters like + and *, but there are a few opera ...

  5. Codeforces 708D 费用流 (呃我们的考试题)

    NB的题目背景 输入输出一样 考试的时候貌似只有gzz一个人搞出来了 %gzz 思路: 分情况讨论 add(x,y,C,E) C是费用 E是流量 1. f>c add(x,y,2,inf),ad ...

  6. PostgreSQL Replication之第四章 设置异步复制(8)

    4.8 处理时间线 时间线是一个您必须要知道的一个重要的概念,尤其是当您规划一个大型的设置的时候. 那么,什么是时间线呢?事实上,它是XLOG的一个分支.正常情况下,刚设置的一个数据库实例使用的时间线 ...

  7. codeforces 400 D Dima and Bacteria【并查集 Floyd】

    题意:给出n个点,分别属于k个集合,判断每个集合里面的点的距离都为0,为0的话输出yes,并输出任意两个集合之间的最短路 这道题目有两个地方不会处理, 先是n个点,分别属于k个集合,该怎么记录下来这里 ...

  8. jQuery的效果函数

    jQuery的效果函数有很多,下面让我们一起看看jQuery的效果函数吧: jQuery的效果函数列表: animate():对被选元素应用“自定义”的动画. clearQueue():对被选元素移除 ...

  9. HDU-1225 Football Score 模拟问题(水题)

    题目链接:https://cn.vjudge.net/problem/HDU-1225 水题 代码 #include <algorithm> #include <string> ...

  10. python note #1

    To record my process of studying python and to practice my English meanwhile, I'd like to start writ ...