[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 ...
随机推荐
- linuxubuntu升级.net core版本到2.0
直接看这里 https://www.microsoft.com/net/core#linuxubuntu
- iOS 9应用开发教程之ios9中实现button的响应
iOS 9应用开发教程之ios9中实现button的响应 IOS9实现button的响应 button主要是实现用户交互的.即实现响应.button实现响应的方式能够依据加入button的不同分为两种 ...
- 服务发现与负载均衡 dubbo zk原理
服务发现与负载均衡 拓展阅读 : dubbo 原理概念图 2016-03-03 杜亦舒 性能与架构 性能与架构 性能与架构 微信号 yogoup 功能介绍 网站性能提升与架构设计 内容整理自文章“实施 ...
- unity5, UGUI刺穿问题解法
我希望在touch屏幕时player起跳,于是在playerControl.cs的Update函数中添加如下touch代码: if (Input.GetMouseButtonDown (0)) {/ ...
- [MySQL] Innodb參数优化
innodb_buffer_pool_size innodb_buffer_pool_size 參数用来设置Innodb 最基本的Buffer(Innodb_Buffer_Pool)的大小,也就是缓存 ...
- Atitit.词法分析的理论原理 part2
Atitit.词法分析的理论原理 part2 1. 转换图1 1.1. 转换图是由程序流程图改进而成的.同样,转换图也可以等价地转换为程序流程图3 1.2. 2.2.3 构造词法分析器(2)流程程 ...
- layui当点击增加的时候,将form中的值获取的添加到table行中代码
layui.use(['table','layer'],function(){ var $=layui.$, table=layui.table, layer=layui.layer; functio ...
- springmvc配置AOP的两种方式
spingmvc配置AOP有两种方式,一种是利用注解的方式配置,另一种是XML配置实现. 应用注解的方式配置: 先在maven中引入AOP用到的依赖 <dependency> <gr ...
- phoneGap 3.5 eclipise 模拟器调试
最近想搞phoneGap开发,可是一看 http://www.phonegapcn.com/ phoneGap中文网 FUCK .phoneGap 还在1.0.0 里混呢.现在phoneGap 3.5 ...
- 阿里云经典网络和专有 专有自己设置网络和私网IP
阿里云网络系列之经典网络和专有网络 驻云科技 2016-07-29 13:43:44 浏览45005 评论9 云栖社区 nginx 安全与风控 系统软件 编程语言 数据存储与数据库 系统研发与运维 ...