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 ...
随机推荐
- Jenkins+Jmeter+Ant 接口持续集成(转)
来源:https://testerhome.com/topics/5186 为什么要用Jmeter做接口测试 当选择这套方案的时候,很多人会问,为什么选择Jmeter做Case管理?为什么不自己写框架 ...
- IT公司100题-26-左旋转字符串
问题描述: 给定字符串和左旋的字符数,写程序实现字符串的左旋操作.例如对于字符串”12345678″, 左旋转4个字符后,变成”56781234″.要求时间复杂度为O(n),空间复杂度O(1). ...
- Rhel6-vpn配置文档
系统环境: rhel6 x86_64 iptables and selinux disabled 主机: 192.168.122.160 server60.example.com 192.168.12 ...
- c# windows编程控件学习-1
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- [计算机、网络相关历史]unix简史
本文2001年由台湾“网络农夫”所写,其人生平不祥,此文受鸟哥大力推崇,两人应该相识.文章写得很不错,应该是查了很多资料整理而成的,美中不足的是好多语句不通顺,国考语文绝对不及格,哈哈. 0.我的准备 ...
- java中的包以及内部类的介绍
1:形式参数和返回值的问题(理解) (1)形式参数: 类名:需要该类的对象 抽象类名:需要该类的子类对象 接口名:需要该接口的实现类对象 (2)返 ...
- 【C++ STL编程】queue小例子
STL是标准化组件,现在已经是C++的一部分,因此不用额外安装什么. #include <queue> #include <iostream> using namespace ...
- cocos2d/x 自带字体(label)
CCLabelTTF* label1 = CCLabelTTF::labelWithString("1掼蛋as", "AppleGothic", 15); la ...
- PHP操作Excel – PHPExcel 基本用法详解
导出excel属性设置//Include classrequire_once('Classes/PHPExcel.php');require_once('Classes/PHPExcel/Writer ...
- Day19_IO第一天
1.异常 1.概念 程序出现不正常的情况 2.异常体系(掌握) Throwable |-Error ...