递归实现:

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] 全排列的更多相关文章

  1. LintCode——全排列

    描述:给定一个数字列表,返回其所有可能的排列. 样例:给出一个列表[1,2,3],其全排列为:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] 说明: ...

  2. lintcode 中等题:permutations 全排列

    题目 全排列 给定一个数字列表,返回其所有可能的排列. 您在真实的面试中是否遇到过这个题? Yes 样例 给出一个列表[1,2,3],其全排列为: [ [1,2,3], [1,3,2], [2,1,3 ...

  3. lintcode 中等题:permutations II 重复数据的全排列

    题目 带重复元素的排列 给出一个具有重复数字的列表,找出列表所有不同的排列. 样例 给出列表 [1,2,2],不同的排列有: [ [1,2,2], [2,1,2], [2,2,1] ] 挑战 使用递归 ...

  4. (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)

    --------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...

  5. lintcode Permutation Index

    题目:http://www.lintcode.com/zh-cn/problem/permutation-index/ 排列序号 给出一个不含重复数字的排列,求这些数字的所有排列按字典序排序后该排列的 ...

  6. leetcode & lintcode for bug-free

    刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...

  7. leetcode & lintcode 题解

    刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...

  8. LintCode 190: Next Permutation

    LintCode 190: Next Permutation 题目描述 给定一个若干整数的排列,给出按正数大小进行字典序从小到大排序后的下一个排列. 如果没有下一个排列,则输出字典序最小的序列. 样例 ...

  9. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

随机推荐

  1. eclipse项目 乱码

  2. Win7下更改iTunes备份路径最便捷的方法

    ① 先删除C:\Users\你的用户名\AppData\Roaming\Apple Computer里的 MobileSync文件夹(首次安装iTunes要先运行一次itunes,才有这个文件夹,如果 ...

  3. PyCharm设置python新建文件指定编码为utf-8

    PyCharm新建文件时可以在模板中添加编码字符集为utf-8,新建文件可自动添加了

  4. 저장소system.runtime.remoting.messaging.callcontext

    https://msdn.microsoft.com/ko-kr/library/system.runtime.remoting.messaging.callcontext(v=vs.110).asp ...

  5. android 建数据库 SQLite 存储sd 卡或者内存

    android 创建数据库调用SQLiteOpenHelper,一般不直接操作SQLiteDatabase . 是通过SQLiteOpenHelper来获取 public class DBOpenHe ...

  6. C# ZPL II 命令打印标签

    public class BarPrinter { public static byte[] ZPLPrinter(string p_title = "", string p_sp ...

  7. 使用 xlue 实现简单 listbox 控件

    基于 XLUE 实现的 listbox 控件 1. 提供增删查接口,将 obj 作为子控件添加到列表: 2. 提供 Attach/Detach 方法,可以将子控件的事件转发出来: 3. 支持滚动条: ...

  8. SQLServer 与 MySQL

    SQL Server         MySQL1.getdate()            NOW()        2.top 100                               ...

  9. java -jar Incompatible argument to function

    原因分析:jar包版本问题 解决方法:到工程中查看代码引用的jar包版本是多少,然后升级jar包,就可以了!

  10. spring quartz定时任务 配置

    cronExpression表达式: 字段 允许值 允许的特殊字符秒 0-59 , - * /分 0-59 , - * /小时 0-23 , - * /日期 1-31 , - * ? / L W C月 ...