52. Sort Colors && Combinations
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.
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的更多相关文章
- 【LeetCode】Sort Colors
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- Lintcode: Sort Colors II
Given an array of n objects with k different colors (numbered from 1 to k), sort them so that object ...
- Lintcode: Sort Colors II 解题报告
Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects ...
- 75. Sort Colors(颜色排序) from LeetCode
75. Sort Colors 给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和2分别表示红色, ...
- Sort Colors I & II
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- 【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 ...
- LeetCode: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
- 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 ...
- [Leetcode Week2]Sort Colors
Sort Colors题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/sort-colors/description/ Description Give ...
随机推荐
- [Weekly] 2014.03.01-2014.03.08
这周写过好多东西,虽然还没有完全弄明白线段树,但是progress还是有的! 不过有时候真的很想哭,因为自己的梦想连别人看看韩剧.无所事事还要分量轻,实在不明白政治课的Teamwork意义何在,花两分 ...
- vector 的 push_back[转]
vector是用数组实现的,每次执行push_back操作,相当于底层的数组实现要重新分配大小(即先free掉原存储,后重新malloc):这种实现体现到vector实现就是每当push_back一个 ...
- 关于allow_url_fopen的设置与服务器的安全
allow_url_fopen与安全以及PHP libcurl allow_url_fopen=ON常常会给服务器和管理员带来麻烦,但是经常性(至少我这样认为)的我们需要远程读取某个东西,如果设置al ...
- 用CSS定义每段首行缩进2个字符 转
应该遵循w3c所制定的html/xhtml标准来使用tag和编写网页.如果你对此不太了解,可以到w3c的网站www.w3.org去找相关资料,或者买一本xhtml的书(注意不要买过时的html的书,尽 ...
- js 中混乱this
1.在HTML元素事件属性中inline方式使用this关键字: <div onclick=" // 可以在里面使用this ">division element&l ...
- Python 入门指南
Release: 3.4 Date: March 29, 2014 Python 是一门简单易学且功能强大的编程语言. 它拥有高效的高级数据结构,并且能够用简单而又高效的方式进行面向对象编程. Pyt ...
- 如何改善magento前台图片质量
magento做的网店的 前台产品图片仔细看会发现不够清晰,质量比原图损失较大,这是因为系统在用GD2压缩图片时默认压缩 质量是80%.为了提高产品图片质量,我们可以修改代码来改变压 magento做 ...
- mysql控制台操作
显示表结构 : show create table table_name 复制表:insert into table_name1 select * from table_name2
- webservice可以访问但是不能调用方法
<webServices> <protocols> <add name="HttpSoa ...
- python thread 多线程
thread 模块在python3中改为_thread,网上的各种帖子都说不建议新手使用thread,好吃不好吃总得尝尝看. import _thread def print_num(): for i ...