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. 设计模式之单实例模式(Singleton)

    原理:将类的构造函数由pubic变为private或者protect,添加获取对象的public 成员函数,返回指向对象的静态指针. 首先来一段简单的代码实现 代码一 class Singleton ...

  2. win7 安装Oracle 10G,11G

    安装 10G : 安装说明: http://wenku.baidu.com/view/a73d048bd0d233d4b14e69a8.html 按这个安装成功过.   11G R2: 在Win7 6 ...

  3. contentType,pageEncoding,charset,setCharacterEncoding

    contentType,pageEncoding,charset,setCharacterEncoding HTML中的meta标签,以及HTTP Header. -------------字符集.字 ...

  4. 【面试题013】在O(1)时间删除链表结点

    [面试题013]在O(1)时间删除链表结点  我们要删除结点i,我们可以把结点i的下一个结点j的内容复制到结点i,然后呢把结点i的指针指向结点j的下一个结点.然后在删除结点j. 1.如果结点i位于链表 ...

  5. [LeetCode]Link List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  6. oracle的全文索引

    1.查看oracle的字符集 SQL> select userenv('language') from dual; USERENV('LANGUAGE') ------------------- ...

  7. 00 - Oracle体系结构课程内容

    1.导论 自己把本书内容分为几个部分, (1)体系结构,(2)并发控制和事务,(3)物理存储, (4)其他. 嗯,拿到一个新的数据库,我们也要从这几个方面去认识它.   2.体系结构 3.Oracle ...

  8. http://www.mxchip.com/talk/news/jishuwenzhang/2014-09-11/67.html

    http://www.mxchip.com/talk/news/jishuwenzhang/2014-09-11/67.html

  9. [转]Ubuntu 常用快捷键10个

    转自:http://www.linuxeden.com/html/news/20100613/103374.html 1.前一个后一个工作区的切换 如果你经常使用工作区,那你就可以用Ctrl + Al ...

  10. lintcode :数组剔除元素后的乘积

    题目: 数组剔除元素后的乘积 给定一个整数数组A. 定义B[i] = A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1], 计算B的时候请不要使用除法. 样例 给出 ...