翻译

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

在这里,我们将使用数字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. 洛谷——P1152 欢乐的跳

    P1152 欢乐的跳 题目描述 一个n个元素的整数数组,如果数组两个连续元素之间差的绝对值包括了[1,n-1]之间的所有整数,则称之符合“欢乐的跳”,如数组1 4 2 3符合“欢乐的跳”,因为差的绝对 ...

  2. POJ 2975 Nim(博弈论)

    [题目链接] http://poj.org/problem?id=2975 [题目大意] 问在传统的nim游戏中先手必胜策略的数量 [题解] 设sg=a1^a1^a3^a4^………^an,当sg为0时 ...

  3. 【枚举】【贪心】 Codeforces Round #398 (Div. 2) B. The Queue

    卡题意……妈的智障 一个人的服务时间完整包含在整个工作时间以内. 显然,如果有空档的时间,并且能再下班之前完结,那么直接输出即可,显然取最左侧的空档最优. 如果没有的话,就要考虑“挤掉”某个人,就是在 ...

  4. JQurey中getJSON方法错误回调方法

    1.使用try...catch实现 2.换$.ajax 3.JQuery 1.5+可以这样使用: $.getJSON("example.json", function() { al ...

  5. Word里如何打出带有上下横杠的大写字母i

    换成新罗马就行了.

  6. 微信开发之自定义菜单--weixin-java-tools

    一.前言 平时在开发微信的过程中,肯定会设计到微信的相关菜单的使用,这次就和大家介绍下如何使用weixin-java-tools来管理菜单 二.自定义菜单分类 1.click:点击推事件用户点击cli ...

  7. weblogic下同域不同端口下的跨域问题解决

      环境:同一台服务器,同一个Weblogic应用程序,分别建两个域,两个域IP一样,端口不同.一个域里放Web应用A,一个放Web应用B. 操作:用户访问A程序的时候,A程序会返回一个链接,让用户去 ...

  8. nodeJs+socket.io

    1.先安装npm和node 2.安装socket.io npm install socket.io 3.html <!DOCTYPE html> <html lang="e ...

  9. 部署web Service到tomcat

    建立项目 打开jdeveloper 12c,然后新建一个java项目,点击java,生成web services. package simple; import javax.jws.WebMethod ...

  10. android应用开发-从设计到实现 2-8 组件与经常使用模式

    组件与经常使用模式 前面已经比較全面的介绍了Material Design相关的设计哲学.设计原理和方法论. 这一章開始,我们将看看这些原理是怎样在安卓系统其中得到实践的. 一个应用并非全然从什么都没 ...