翻译

给定一个包括红色、白色、蓝色这三个颜色对象的数组。对它们进行排序以使同样的颜色变成相邻的,其顺序是红色、白色、蓝色。在这里,我们将使用数字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

    LeetCode 75. Sort Colors Solution1: 参考自:<leetcode-cpp 答案> 由于0,1,2非常紧凑,首先想到计数排序(counting sort), ...

  2. LeetCode 75. Sort Colors (python一次遍历,模拟三路快排)

    LeetCode 75. Sort Colors (python一次遍历,模拟三路快排) 题目分析: 本题需要实现数字只包含0,1,2的排序,并且要求一次遍历. 由于只用把数字隔离开,很容易想到快排的 ...

  3. [LeetCode]75.Sort Colors

    [题目连接] 75.Sort Colors [题目] Given an array with n objects colored red, white or blue, sort them so th ...

  4. leetcode 75. Sort Colors | 75. 颜色分类(荷兰国旗问题,快速排序)

    题目 https://leetcode.com/problems/sort-colors/ 题解 快速排序3.0(随机快排+荷兰国旗技巧优化) 在arr[L-R]范围上,进行快速排序的过程: 1)在这 ...

  5. LeetCode 75. Sort Colors

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

  6. 个人记录-LeetCode 75. Sort Colors

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

  7. 75. Sort Colors - LeetCode

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

  8. Leetcode 075 Sort Colors

    题目连接:Leetcode 075 Sort Colors 解题思路:从左向右遍历,同时维护两个指针r和b,0~r位置上的元素为0,b~n-1位置上的为2.在遍历过程中,碰到0,放到r的位置:碰到2, ...

  9. LeetCode 75. Sort Colors--Python解法

    题目地址:Sort Colors - LeetCode Given an array with n objects colored red, white or blue, sort them in-p ...

最新文章

  1. 高效的机器学习研究者,应该具备这 6 个习惯
  2. 关于产品版本英语缩写
  3. Java数据库连接池实现原理
  4. import android.view.window;,尝试在空对象引用上调用虚拟方法‘android.view.Window$回调...
  5. centos6.5安装vnc-server
  6. js调用浏览器的下载框
  7. 用于web网页的html文件属于,南开15春学期《Web页面设计》在线作业满分答案
  8. Bootstrap 表格行的样式
  9. 【I】ZF2安装 和 创建一个新项目
  10. 【渝粤教育】电大中专电商运营实操 (12)作业 题库
  11. Google 中国招聘啦
  12. flash+xml 制作电子相册教程
  13. Greensock平台
  14. ASP.NET Core 多线程 异步编程
  15. 在 vue 中基于 tinymce 封装的一个富文本编辑器组件
  16. 2. shenyu(神禹)网关调用链及组装过程
  17. Leetcode| 216. 组合总和III、17. 电话号码的字母组合 Day25
  18. php实用教程第3版郑阿奇课后答案_PHP实用教程(第3版)
  19. Netty实现多人在线游戏后台程序
  20. 实验八、Windows本地破解用户口令

热门文章

  1. The Genymotion virtual device could not obtain an IP address
  2. android 看AP 国家代码,Android WiFi 获取国家码
  3. spark 不同模式用途_Spark中那些常用的特征处理操作
  4. MATLAB-向量相关计算
  5. 方法传递java_Java 程序将方法作为参数传递给其他方法
  6. android 让item满屏,Android的全屏活动?
  7. 【深度学习】CUDA 和 TensorRT 博客搜集
  8. 【数据结构与算法】5. C++中 list、deque、vector对比
  9. Linux(Ubuntu14.04)下安装Anaconda和Spyder
  10. 如何活着:欲望、外界、标签、天才、时间、人生目标和经历