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 ...
随机推荐
- webix custom component-九宫格
上篇大致讲了对源码的理解,这篇展示一个初步的九宫格控件.直接上源码: webix.protoUI({ name:"grid", $init:function(config){ co ...
- DeepLearning之路(一)逻辑回归
逻辑回归 1. 总述 逻辑回归来源于回归分析,用来解决分类问题,即预测值变为较少数量的离散值. 2. 基本概念 回归分析(Regression Analysis):存在一堆观测资料,希望获得数据内 ...
- Fedora10下建立linux系统的窗口没有地址栏
Fedora10下建立的linux系统窗口没有地址栏 打开一个文件夹就打开一个窗口,还没有地址栏,这很麻烦也不习惯. 另:打开地址栏可以用组合键 Ctrl+L 如图 解决: edit---perfer ...
- WinRT知识积累1之读xml数据
前述:这个知识是在Windows8.1或WP8.1中运用Linq to xml获取一个xml文件里的数据.(网上也很多类似的知识,可以借鉴参考) 平台:windows8.1 metro 或者WP8.1 ...
- Android Camera进行拍照
Android应用提供了Camera来控制拍照,使用Camera进行拍照的步骤: 1.调用Camera的open()方法打开相机. 2.调用Camera的getParameters()方法获取拍照参数 ...
- AngularJs的UI组件ui-Bootstrap分享(二)——Collapse
Collapse折叠控件使用uib-collapse指令 <!DOCTYPE html> <html ng-app="ui.bootstrap.demo" xml ...
- JVM-字节码指令
Java虚拟机字节码指令 了解了class文件,我觉得就很有必要去了解一下JVM中的字节码指令,那样堆class文件以及JVM运行机制也后很大的帮助. Java虚拟机的指令由一个字节长度的,代表着某种 ...
- hdu 2084
ps:这道题...是DP题..所以我去看了百度一些东西,才知道了什么是状态方程,状态转移方程.. 做的第一个DP题,然后TLE一次.贴上TLE的代码: #include "stdio.h&q ...
- windows核心编程---第六章 线程的调度
每个线程都有一个CONTEXT结构,保存在线程内核对象中.大约每隔20ms windows就会查看所有当前存在的线程内核对象.并在可调度的线程内核对象中选择一个,将其保存在CONTEXT结构的值载入c ...
- iOS-服务器文件断点下载
文件下载基本步骤:1.获取下载链接,创建响应发送请求.(使用异步请求,避免因文件过大下载时间长而阻塞主线程).2.当接到响应时在下载目录中创建文件.创建文件使用NSFileHandle进行文件内部处理 ...