[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 ...
随机推荐
- 两周赚100万 在Google Play做对了什么
http://tech.it168.com/a2013/0301/1457/000001457913_all.shtml
- 前端JS脚本将网页表格导出为Excel
话不多说,上代码! <!DOCTYPE> <html> <head> <title>Excel Test</title> </head ...
- 【征文】Hadoop十周年特别策划——我与Hadoop不得不说的故事
2016年是Hadoop的十周年生日,在今年,CSDN将以技术和实战为主题与大家共同为Hadoop庆生.其主要内容包含Hadoop专业词典.系列视频技术解析.Hadoop行业实践.线上问答.线下沙龙. ...
- 通过使用JanusGraph索引提高性能
翻译整理:纪玉奇 Extending JanusGraph Server JanusGraph支持两种类型的索引:graph index和vertex-centric index.graph inde ...
- 64位Windows系统如何配置32位ODBC数据源
在64位Windows系统中,默认“数据源(ODBC)”是64位的,包括“控制面板->管理工具->数据源 ”或在“运行”中直接运行“ODBCAD32”程序.如果客户端是32位应用程序,仍然 ...
- Laravel之路(事务)mysql事务
其实关于mysql的事务(原声mysql语句),我在我的博客里面有提到(mysql的文章分类下) 今天看下基于laravel框架ORM的处理 准备: 表必须是InnoDB引擎 DB::beginTra ...
- Linux 系统 pptpd+radius+mysql 安装攻略
分类: 原文地址:Linux 系统 pptpd+radius+mysql 安装攻略 作者:wfeng .你所需要的软件 内核最好能升级到2.6 如果你是centos的用户,可以通过yum update ...
- 查看linux内核和版本信息
一.查看Linux内核版本命令(2种方法): 1.cat /proc/version Linux version 3.10.0_1-0-0-8 (root@xxx) (gcc version 4.8. ...
- linux 查看可执行文件动态链接库相关信息(转)
转自 http://blog.sina.com.cn/s/blog_67eb1f2f0100mgd8.html ldd <可执行文件名> 查看可执行文件链接了哪些 系统动态链 ...
- c# 多线程里面创建byte数组发生内存溢出异常求解
在多线程里面读取一个400多M的Xml文件,首先将其读入FileStream里面,然后,在执行 byte [] bts = new byte[fs.Length]; 这句代码时,出现内存溢出的异常,求 ...