翻译

给定一个包括红色、白色、蓝色这三个颜色对象的数组。对它们进行排序以使同样的颜色变成相邻的,其顺序是红色、白色、蓝色。

在这里,我们将使用数字0、1和2分别来代表红色、白色和蓝色。

原文

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.

分析

看到这道题。立刻就想到了range-for。可能这样的方法有些投机吧。只是确实非常easy就实现了。

void sortColors(vector<int>& nums) {
vector<int> v0, v1, v2;
for (auto n : nums) {
switch (n)
{
case 0:
v0.push_back(n);
break;
case 1:
v1.push_back(1);
break;
case 2:
v2.push_back(2);
break;
default:
break;
}
}
nums.erase(nums.begin(), nums.end());
for (auto a0 : v0) {
nums.push_back(a0);
}
for (auto a1 : v1) {
nums.push_back(a1);
}
for (auto a2 : v2) {
nums.push_back(a2);
}
}

LeetCode 75 Sort Colors(颜色排序)的更多相关文章

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

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

  2. leetCode 75.Sort Colors (颜色排序) 解题思路和方法

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

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

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

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

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

  5. leetcode 75 Sort Colors 计数排序,三路快排

    解法一:计数排序:统计0,1,2 的个数 时间复杂度:O(n) 空间复杂度:O(k)    k为元素的取值范围, 此题为O(1) class Solution { public: void sortC ...

  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] Sort Colors 颜色排序

    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三色排序

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

  9. Leetcode 75. Sort Colors

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

随机推荐

  1. Struts2遇到Caused by Action class not found这类问题怎么解决

    刚才碰到个小坑,显示的错误是struts.xml找不到Action,明显不会有错的因为之前还是正常跑起来的,然后百度google搜了一通没解决,maven clean用了没反应还是照样报错. 最后在百 ...

  2. [BZOJ2753]滑雪与时间胶囊

    第一问直接把可以走的边连起来bfs一遍即可 第二问可以用类似kruskal的方法,只不过排序的依据应该变为第一关键字为终点高度(从大到小),第二关键字为边权(从小到大),只排序可以走的边 因为同样高度 ...

  3. python3开发进阶-Django框架学习前的小项目(一个简单的学员管理系统)

    ''' 自己独立写一个学员管理系统 表结构: 班级表: -id -grade_name 学生表: -id -student_name -grade 关联外键班级表 老师表: -id -teacher_ ...

  4. 杂谈PID控制算法——第一篇:三个量

    电赛训练了大半个暑假,人渐渐开始进入到了疲倦期.既然这样那就好好休息下自己也好吧. 休息也不能光休息,乘机把平常写过的一些东西好好整理也好. 从第一次训练开始我们就接触到了一个新的名词——PID控制理 ...

  5. Plplot中line width 问题

    Plplot延续了Pgplot的设计风格,线的宽度函数plwid(int width)只能用整型变量,不能精确控制线宽.用pscairo画出的曲线总是太粗,这是plplot一个很老的问题(issue) ...

  6. 删除cnpm

    cnpm是npm的中国镜像:这是一个完整 npmjs.org 镜像,你可以用此代替官方版本(只读),同步频率目前为 10分钟 一次以保证尽量与官方服务同步. 安装:npm install -g cnp ...

  7. solr6.6 导入 文本(txt/json/xml/csv)文件

    参照:solr6.6 导入 pdf文件 重点就是三个配置文件 1.建立的data-config.xml 内容如下: <dataConfig> <dataSource name=&qu ...

  8. FTP服务器需要开几个端口

    原文: https://blog.csdn.net/houbin0912/article/details/72578688 -------------------------------------- ...

  9. PHP关闭notice级别报错信息

    1.在php.ini文件中改动error_reporting改为:error_reporting=E_ALL & ~E_NOTICE2.如果你不能操作php.ini文件,你可以使用如下方法在你 ...

  10. Kinect 1.8 体感开发,手势,姿态(Pose) 捕捉判断方法以及一些辅方法

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...