Sort Colors

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?

思路: 1. 类似快排,走两遍(v=1, 分出0;v = 2,分出1)。

void partition(int A[], int n, int v) {
int start = 0, end = n-1;
while(start < end) {
while(start < end && A[start] < v) ++start;
while(start < end && A[end] >= v) --end;
int tem = A[start];
A[start++] = A[end];
A[end--] = tem;
}
}
class Solution {
public:
void sortColors(int A[], int n) {
partition(A, n, 1);
partition(A, n, 2);
}
};

2. 计数排序。计数与重写。

class Solution {
public:
void sortColors(int A[], int n) {
int count[3] = {0};
for(int i = 0; i < n; ++i) count[A[i]]++;
int id = 0;
for(int i = 0; i < 3; ++i)
for(int j = 0; j < count[i]; ++j)
A[id++] = i;
}
};

Combinations

Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.

For example, If n = 4 and k = 2, a solution is:

[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
] 思路:递归,每层从前往后逐步取元素。
void combination(int k, int num, int begin, int end, vector<int> & vec2, vector<vector<int> > &vec) {
if(num == k) {
vec.push_back(vec2);
return;
}
for(int i = begin; i <= end; ++i) {
vec2.push_back(i);
combination(k, num+1, i+1, end, vec2, vec);
vec2.pop_back();
}
} class Solution {
public:
vector<vector<int> > combine(int n, int k) {
vector<int> vec2;
vector<vector<int> > vec;
combination(k, 0, 1, n, vec2, vec);
return vec;
}
};

52. Sort Colors && Combinations的更多相关文章

  1. 【LeetCode】Sort Colors

    Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...

  2. Lintcode: Sort Colors II

    Given an array of n objects with k different colors (numbered from 1 to k), sort them so that object ...

  3. Lintcode: Sort Colors II 解题报告

    Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects ...

  4. 75. Sort Colors(颜色排序) from LeetCode

      75. Sort Colors   给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和2分别表示红色, ...

  5. Sort Colors I & II

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

  6. 【LeetCode】75. Sort Colors (3 solutions)

    Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...

  7. LeetCode: Sort Colors 解题报告

    Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...

  8. LeetCode解题报告—— Rotate List & Set Matrix Zeroes & Sort Colors

    1. Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. Exam ...

  9. [Leetcode Week2]Sort Colors

    Sort Colors题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/sort-colors/description/ Description Give ...

随机推荐

  1. oracle 11g ORA-12541: TNS: 无监听程序 (DBD ERROR: OCIServerAttach)

    From :http://www.cnblogs.com/wangyt223/archive/2012/12/11/2812931.html em无法浏览,同时监听起不来.同时他的监听服务还是正常的, ...

  2. MySql指令集

    http://blog.csdn.net/cl05300629/article/details/9464007

  3. ubuntu auto install update

    sudo apt-get update sudo apt-get dist-upgrade 32bit mode sudo dpkg --add-architecture i386

  4. flask开发遇到 Must provide secret_key to use csrf解决办法

    开发flask的时候,遇到了 Must provide secret_key to use csrf错误提醒.原来是没有设置secret_key .在代码中加上 app.config['SECRET_ ...

  5. crackme1.exe解密过程

    那今天呢     在西普的做题过程中,发现这么一款.exe,我们来破解一下(当然不是简单的强制爆破,不是简单的打补丁) 我们先用PE  看看   它是用什么写的  有没有加壳什么的 很好   是VC6 ...

  6. Android快捷便利但不常被使用的原生工具类

    Android快捷便利但不常被使用的原生工具类 Android SDK原生 API中,有一些常用的工具类,运用得当可以省事省力省时,何况还是Android官方提供的,现在收集整理一些出来.DateUt ...

  7. 从零开始学习Node.js例子六 EventEmitter发送和接收事件

    pulser.js /* EventEmitter发送和接收事件 HTTPServer和HTTPClient类,它们都继承自EventEmitter EventEmitter被定义在Node的事件(e ...

  8. IOS UIImagePickerController 保存图片到 相册

    // 异步下载图片 dispatch_queue_t queue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0); disp ...

  9. redis windows

    下载 redis windows版本 redis-server  redis.windows.conf  //cmd 命令 启动成功 E:\redis\Redis-x64-3.0.500>red ...

  10. 【转载】WebDriver常用的鼠标/键盘操作

    注:driver为一个WebDriver的实例,xpath为一个元素的xpath字符串,在本文中一律采用xpath的方式定位元素 1.鼠标右键点击操作:Actions action = new Act ...