Title:

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.

直观的解法是使用计数排序

class Solution {
public:
void sortColors(vector<int>& nums) {
int *c = new int [];
memset(c,,sizeof(int)*);
for (int i = ; i < nums.size(); i++){
c[nums[i]]++;
}
nums.clear();
for (int i = ; i < ; i++){
for (int j = ; j < c[i]; j++){
nums.push_back(i);
}
}
}
};

也可以遍历一次就能得到结果,使用3个下标,分别指向0,1,2对应的下标位置。可以这么理解,最左边是0,最右边是2,中间遇到1不用管

class Solution {
public:
void sortColors(int A[], int n) {
if(n <= ) return;
int start = -, end = n;
int p = ;
while(p < n && start < end){
if(A[p] == ){
if(p > start) swap(A, ++start, p);
}else if(A[p] == ){
if(p < end) swap(A, p, --end);
}else ++p;
}
}
private:
void swap(int A[], int i, int j){
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
};

LeetCode: Sorted Color的更多相关文章

  1. 【Python】列表~深入篇

    列表是一列按特定顺序排列的元素组成. 在Python中,用方括号[]来表示列表 下面是一个语言列表 Language = ['Chinese','English','Franch','Deutsch' ...

  2. [LeetCode] 800. Similar RGB Color 相似的红绿蓝颜色

    In the following, every capital letter represents some hexadecimal digit from 0 to f. The red-green- ...

  3. Leetcode: Convert sorted list to binary search tree (No. 109)

    Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...

  4. [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...

  5. [LeetCode] Two Sum II - Input array is sorted 两数之和之二 - 输入数组有序

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  6. [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二

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

  7. [LeetCode] Find Minimum in Rotated Sorted Array 寻找旋转有序数组的最小值

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  8. [LeetCode] Convert Sorted List to Binary Search Tree 将有序链表转为二叉搜索树

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  9. [LeetCode] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...

随机推荐

  1. 【技术贴】解决bug mantisbt APPLICATION ERROR #1502 没有找到类别

    解决bug mantisAPPLICATION ERROR #1502 没有找到类别 mantisbt出现1502问题解决:引起问题的原因:当提交的问题有分类,此时删除此分类,就会出现下面的情况.问题 ...

  2. 循环队列实现(C++) Ring Buffer

    循环队列:队列有着先入先出的特性.但是对于队列如果删除队头以后剩下的空间将不会被释放,又由于队列只能由队尾插入这就导致被删除部分的空间被浪费.解决这个问题就是循环队列.循环队列顾名思义就是将队列串起来 ...

  3. [设计模式] 20 状态模式 State Pattern

    在GOF的<设计模式:可复用面向对象软件的基础>一书中对状态模式是这样说的:允许一个对象在其内部状态改变时改变它的行为.对象看起来似乎修改了它的类.状态模式的重点在于状态转换,很多时候,对 ...

  4. 查看语句运行时间异常的原因(SQLServer)

    转载:http://www.cnblogs.com/fygh/archive/2012/01/17/2324926.html 查看语句运行时间异常的原因(SQLServer)   经常有开发同事反映如 ...

  5. sqlplus 远程oracle

    sqlplus dbuser/dbpassword@192.168.0.2/mydb sqlplus try/try@302-4 302-4为本地oralce  net manager 配置的网络名

  6. Unity3D与iOS的交互设计<ViewController 的跳转>

    原地址:http://www.aichengxu.com/article/%CF%B5%CD%B3%D3%C5%BB%AF/28797_12.html Unity3D与iOS的交互设计<View ...

  7. django在nginx uwsgi和tornado异步方案在项目中的体验

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://rfyiamcool.blog.51cto.com/1030776/1397495 ...

  8. Corner case

    A corner case (or pathological case) is a problem or situation that occurs only outside of normal op ...

  9. 2013 Multi-University Training Contest 1 Partition

    这题主要是推公式…… ;}

  10. Thread的第四天学习

    线程通信 wait notify synchronized + 同对象 才可 互斥 锁不是放在线程上的,放在共享资源内部的. wait 线程等待 notify 线程唤醒