[LintCode] 全排列
递归实现:
class Solution {
public:
/**
* @param nums: A list of integers.
* @return: A list of permutations.
*/
vector<vector<int> > permute(vector<int> nums) {
// write your code here
vector<vector<int> > permutations;
if (nums.empty()) return permutations;
permutate(nums, , permutations);
return permutations;
}
private:
void permutate(vector<int> nums, int start, vector<vector<int> >& permutations) {
if (start == nums.size()) {
permutations.push_back(nums);
return;
}
for (int i = start; i < (int)nums.size(); i++) {
swap(nums[start], nums[i]);
permutate(nums, start + , permutations);
}
}
};
非递归实现(基于nextPermutation):
class Solution {
public:
/**
* @param nums: A list of integers.
* @return: A list of permutations.
*/
vector<vector<int> > permute(vector<int> nums) {
// write your code here
vector<vector<int> > permutations;
if (nums.empty()) return permutations;
vector<int> copy(nums.begin(), nums.end());
nextPermutation(nums);
permutations.push_back(nums);
while (nums != copy) {
nextPermutation(nums);
permutations.push_back(nums);
}
return permutations;
}
private:
void nextPermutation(vector<int>& nums) {
int k = -, n = nums.size();
for (int i = n - ; i >= ; i--) {
if (nums[i] < nums[i + ]) {
k = i;
break;
}
}
if (k == -) {
reverse(nums.begin(), nums.end());
return;
}
int l;
for (int i = n - ; i > k; i--) {
if (nums[i] > nums[k]) {
l = i;
break;
}
}
swap(nums[l], nums[k]);
reverse(nums.begin() + k + , nums.end());
}
};
[LintCode] 全排列的更多相关文章
- LintCode——全排列
描述:给定一个数字列表,返回其所有可能的排列. 样例:给出一个列表[1,2,3],其全排列为:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] 说明: ...
- lintcode 中等题:permutations 全排列
题目 全排列 给定一个数字列表,返回其所有可能的排列. 您在真实的面试中是否遇到过这个题? Yes 样例 给出一个列表[1,2,3],其全排列为: [ [1,2,3], [1,3,2], [2,1,3 ...
- lintcode 中等题:permutations II 重复数据的全排列
题目 带重复元素的排列 给出一个具有重复数字的列表,找出列表所有不同的排列. 样例 给出列表 [1,2,2],不同的排列有: [ [1,2,2], [2,1,2], [2,2,1] ] 挑战 使用递归 ...
- (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...
- lintcode Permutation Index
题目:http://www.lintcode.com/zh-cn/problem/permutation-index/ 排列序号 给出一个不含重复数字的排列,求这些数字的所有排列按字典序排序后该排列的 ...
- leetcode & lintcode for bug-free
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...
- leetcode & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
- LintCode 190: Next Permutation
LintCode 190: Next Permutation 题目描述 给定一个若干整数的排列,给出按正数大小进行字典序从小到大排序后的下一个排列. 如果没有下一个排列,则输出字典序最小的序列. 样例 ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
随机推荐
- spring中添加redis缓存
1.单机版的添加 spring里面配置 <bean id="redisClient" class="redis.clients.jedis.JedisPool&qu ...
- Mac /Ubuntu/Windows 下安装nodejs
Mac If you're using the excellent homebrew package manager, you can install node with one command: b ...
- 1 bootstrap table null默认显示为 - 要查源码 2 记一个很无语的bug
本来返回的json 3个true 7个false的 结果显示10个true 因为本来是好的 结果判断的问题 给全部赋值true了
- [elk]logstash grok原理
logstash语法 http://www.ttlsa.com/elk/elk-logstash-configuration-syntax/ https://www.elastic.co/guide/ ...
- love2d教程33--window模块
--love的window模块比较简单,直接贴代码了function love.load() io.stdout:setvbuf("no") -- 设置io为无缓存模式 --获取显 ...
- 通过PDO 连接SQL Server
下载PDO_DBLIB库 PDO的各种库都可以在PECL中找到,例如,MySQL库:PDO_MYSQL.Oracle库:PDO_OCI. 作为SQL Server 的连接库,通过下面命令下载PDO_D ...
- poj3041 Asteroids 匈牙利算法 最小点集覆盖问题=二分图最大匹配
/** 题目:poj3041 Asteroids 链接:http://poj.org/problem?id=3041 题意:给定n*n的矩阵,'X'表示障碍物,'.'表示空格;你有一把枪,每一发子弹可 ...
- 大数据(9) - Flume的安装与使用
Flume简介 --(实时抽取数据的工具) 1) Flume提供一个分布式的,可靠的,对大数据量的日志进行高效收集.聚集.移动的服务,Flume只能在Unix环境下运行. 2) Flume基于流式架构 ...
- R语言安装sqldb包报错解决办法
我使用Rtudio环境,安装sqldb几次出错.网上没有好的教程. 经过自己试验之后,这样处理.我写出来以后,供大家参考. > install.packages("sqldf" ...
- Memcached集群:Magent缓存代理使用
小结: 先启动memcached 然后启动magent memcached -d -p 11211 -u memcached -m 64 -c 5120 memcached -d -p 11212 - ...