[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 ...
随机推荐
- mongodb - 查看数据库状态
> use test switched to db test > db.stats() { "db" : "test", #数据库名 "c ...
- Heap 3214 LIS题解
依据问题转换成最长不降子序列问题. 10^9的输入数据计算起来还是挺花时间的.由于这里仅仅能使用O(nlgn)时间复杂度了. 只是证明是能够算出10^9个数据的. 由于时间限制是5s. #includ ...
- unity, StopAllCoroutines导致bug的解决办法
StopAllCoroutines有时候不用不行. 但只要一用,就可能导致无穷无尽的bug. 原因是StopAllCoroutines会将当前脚本中所有coroutines都停掉,而没法做到只停掉我们 ...
- PCIe to AXI Translation——PCIe 内存空间到AXI内存空间的转换
PCIe to AXI Translation——PCIe 内存空间到AXI内存空间的转换 UltraScale系列芯片包含PCIe的Gen3 Integrated Block IP核在内的多种不同功 ...
- PHP系统学习3 正则
正则 ^shop 标示匹配与shop开头的字符串 shop$用来匹配与shop结尾的字符串 ^shop$只匹配shop [a-z]匹配所有小写字母 [A-Z]匹配所有大写字母 [a-zA-Z]匹配所有 ...
- Haskell示例
i :: Int i = --add, sub :: Int -> Int -> Int add, sub :: (Num a) => a -> a -> a add a ...
- CSU 1335: 高桥和低桥 (二分查找,树状数组)
Description 有个脑筋急转弯是这样的:有距离很近的一高一低两座桥,两次洪水之后高桥被淹了两次,低桥却只被淹了一次,为什么?答案是:因为低桥太低了,第一次洪水退去之后水位依然在低桥之上,所以不 ...
- dedecms使用
使用到了dedecms内容管理系统,遇到两个问题: 1.点击登录后,没有登录进去也没有任何提示,一片空白 解决办法:我是把网站从云主机拷贝下来的,但是忘了云主机上的数据库密码和自己本地的数据库密码不一 ...
- python 练习题1--打印三位不重复数字
题目:有四个数字:1.2.3.4,能组成多少个互不相同且无重复数字的三位数?各是多少? 程序分析:可填在百位.十位.个位的数字都是1.2.3.4.组成所有的排列后再去 掉不满足条件的排列. 程序源代码 ...
- azkaban入门中遇到的问题
执行时报错,后来发现他的配置文件中写了相对路径!!所以必须在他的根目录下执行,命令为 nohup bin/azkaban-web-start.sh 1>/tmp/azstd.out 2&g ...