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 ...
随机推荐
- ABAP遇到的问题——1
在创建ABAP对象的时候抛出“测试对象不能被创建在外来命名空间”的错误 原因:程序的名字不是以Z或者Y开头的.
- Documentum常见问题11-xplore全文检索时找不到相关内容
最近帮助同事处理了一个关于全文检索的问题,随手记录下来供以后参考. 问题一 某些Cabinet下的文件可以全文检索到,但某些Cabinet下的数据全文检索不成功. 新建了一个Docbase-AADCT ...
- Rhel6-moosefs分布式存储配置文档
MFS 特性: 1. Free(GPL) 2. 通用文件系统,不需要修改上层应用就可以使用 3. 可以在线扩容,体系架构可伸缩性极强. 4. 部署简单. 5. 高可用,可设置任意的文件冗余程度(提供比 ...
- 收藏的牛人的Backbone分享教程
http://yujianshenbing.iteye.com/category/256978 感谢御剑神兵,目前正在看,为源码分析做准备. 今天是2015年4月13号,看了前两篇,
- 个推推送iOS版 常见问题详解
原文:http://www.oschina.net/question/1782938_234760 1.提交了.p12文件后多久可以测试? 提交后10分钟左右才可以测试,并不是立即生效的. 2 ...
- CSipSimple结构浅析
最近做一个VOIP的项目,调研了CSipSimple.都说CSipSimple结构清晰,但是代码下下来看了一下,还是一头雾水,不知从何看起.于是想到从最简单的打电话开始,借助网上一篇博文"C ...
- bootstrap学习<三>打开模态窗体
可以切换模态框(Modal)插件的隐藏内容: 通过 data 属性:在控制器元素(比如按钮或者链接)上设置属性 data-toggle="modal",同时设置 data-targ ...
- jquery替换URL参数值
由于经常会用到替换URL参数值,而网上写的方法代码都太长了,所以在这里写了一个简单的方法,供大家使用. 说明: reLoad(参数名,参数值) function reLoad(p, v) { var ...
- DES跨(C# Android IOS)三个平台通用的加解密方法
#region 跨平台加解密(c# 安卓 IOS) // public static string sKey = "12345678"; ...
- Jmeter—1 安装
1 Jmeter运行需要java环境.首先需要安装JDK. 图标是这样的: 2 下载apache-jmeter包. jmeter官网:http://jmeter.apache.org/ 3 解压 ...