题目

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.

click to show follow up.

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 大小的元素,将其由小及大排序即可。

题目明确要求不可用标准库中的排序算法,尽可能使得算法高效。

我们常用的排序算法最优情况下也有 O(nlogn) 的时间复杂度,对待这样一个特殊的数组排序,我们应该采取其他更加高效的方法。

follow up 给出一种方法,分别计算元素0 , 1 , 2 的个数,然后对原数组重新赋值,简单高效,下面用该

方法给出程序实现。

AC代码

class Solution {
public:
void sortColors(vector<int>& nums) {
if (nums.empty())
return; //初始化一个count数组,count[0] , count[1] , count[2] 分别记录nums中0 , 1 , 2出现个数
vector<int> count(3, 0); vector<int>::iterator iter = nums.begin(); for (; iter != nums.end(); iter++)
{
if (*iter == 0)
count[0]++;
else if (*iter == 1)
count[1]++;
else if (*iter == 2)
count[2]++;
}//for //对原数组排序
int i = 0;
while (i < count[0])
nums[i++] = 0;
while (i < (count[0] + count[1]))
nums[i++] = 1;
while (i < (count[0] + count[1] + count[2]))
nums[i++] = 2; return; }
};

GitHub测试程序源码

LeetCode(75) Sort Colors的更多相关文章

  1. LeetCode(75):分类颜色

    Medium! 题目描述: 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 0. 1 和 2 ...

  2. Leetcode(1)两数之和

    Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...

  3. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  4. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  5. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  6. LeetCode(122) Best Time to Buy and Sell Stock II

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

  7. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

  8. LeetCode(113) Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

  9. LeetCode(107) Binary Tree Level Order Traversal II

    题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...

随机推荐

  1. 本周学习总结(ng-zorro/MDN索引/读书笔记)

    按钮 <button ng-button nzType="primary">Primary</button> nzType="" pri ...

  2. only-child选择器

    :only-child选择器用于匹配属于某一个父元素的唯一子元素的元素,也就是说,如果某个父元素仅有一个子元素,则使用  :only-chlid选择器  ,可以选择这个子元素

  3. nginx上游模块

    1 概念 The ngx_http_upstream_module is used to define groups of servers that can be referenced by the  ...

  4. Maximal Area Quadrilateral CodeForces - 340B || 三点坐标求三角形面积

    Maximal Area Quadrilateral CodeForces - 340B 三点坐标求三角形面积(可以带正负,表示向量/点的不同相对位置): http://www.cnblogs.com ...

  5. h5-18-文件操作-兼容判断

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

  6. JAVA常用知识总结(六)——Mybatis

    为什么说Mybatis是半自动ORM映射工具?它与全自动的区别在哪里? Hibernate属于全自动ORM映射工具,使用Hibernate查询关联对象或者关联集合对象时,可以根据对象关系模型直接获取, ...

  7. poj2282The Counting Problem(组合)

    链接 计算0-9每一个数字出现的次数 逐位进行处理 对于每一位取几时依次算下组合的情况 注意0的情况需要特殊处理一下 因为0000 00 这样都是等于0的 前面的几位是多余的 #include < ...

  8. Jenkins视图使用--添加删除视图

    job建立的特别多的时候,我们可能不太容易找到自己的某个job,这时,我们就可以在Jenkins中建立视图.job的视图类似于我们电脑上的文件夹.可以通过一些过滤规则,将已经建好的job过滤到视图中, ...

  9. 01认识Python和基础知识

     1.了解Python Python的发展历史,作者Guido, 荷兰人 Python的优缺点 Python在网站的开发,如YouTube,科学计算,数据分析,在游戏后台开发等方面广泛使用  2.编写 ...

  10. Mybatis的Service循环调用错误

    org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'z ...