LeetCode 046 Permutations
题目要求:Permutations(全排列)
Given a collection of numbers, return all possible permutations.
For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].
分析:
参考网址:http://www.cnblogs.com/panda_lin/archive/2013/11/12/permutations.html
可以使用STL的next_permutation函数。
不使用的话,要递归生成全排列。
① 生成[2, 3]的全排列[2, 3]和[3, 2],然后把1加上去生成[1, 2, 3]和[1, 3, 2]。
② 交换1和2的位置,生成[1, 3]的全排列[1, 3]和[3, 1],然后把2加上去生成[2, 1, 3]和[2, 3, 1]。
③在第二步的基础上交换2和3的位置,生成[2, 1]的全排列[2, 1]和[1, 2],然后把3加上去生成[3, 2, 1]和[3, 1, 2]。
① [1, 2, 3] [1, 3, 2]
② [2, 1, 3] [2, 3, 1]
③ [3, 2, 1] [3, 1, 2]
代码如下:
class Solution {
public:
vector<vector<int> > permute(vector<int> &num) {
vector<vector<int>> result;
sort(num.begin(), num.end());
//STL next_permutation
do{
result.push_back(num);
}while(next_permutation(num.begin(), num.end()));
return result;
}
};
class Solution {
public:
void internalPermute(vector<int> &num, int index, vector<int> &perm, vector<vector<int> > &result) {
int size = num.size();
if (size == index) {
result.push_back(perm);
}
else {
for (int i = index; i < size; ++i) {
swap(num[index], num[i]);
perm.push_back(num[index]);
internalPermute(num, index + 1, perm, result);
perm.pop_back();
swap(num[index], num[i]);
}
}
}
vector<vector<int> > permute(vector<int> &num) {
vector<vector<int> > result;
vector<int> perm;
internalPermute(num, 0, perm, result);
return result;
}
};
LeetCode 046 Permutations的更多相关文章
- Java for LeetCode 046 Permutations
Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the follo ...
- LeetCode 046 Permutations 全排列
Given a collection of distinct numbers, return all possible permutations.For example,[1,2,3] have th ...
- Java for LeetCode 047 Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- [LeetCode] 47. Permutations II 全排列 II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 【LeetCode】046. Permutations
题目: Given a collection of distinct numbers, return all possible permutations. For example,[1,2,3] ha ...
- LeetCode 46 Permutations(全排列问题)
题目链接:https://leetcode.com/problems/permutations/?tab=Description Problem:给出一个数组(数组中的元素均不相同),求出这个数组 ...
- 046 Permutations 全排列
给定一个含有不同数字的集合,返回所有可能的全排列.比如,[1,2,3] 具有如下排列:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2 ...
随机推荐
- Reverse for ‘password_reset_complete‘ not found. ‘password_reset_complete‘ is not a valid view funct
关注公众号"轻松学编程"了解更多 原因 在使用xadmin与django 2版本以上修改密码时会报这个错,这是由于django修改密码成功后使用的是success_url参数,而x ...
- python中可迭代对象、迭代器、生成器
可迭代对象 关注公众号"轻松学编程"了解更多. 1.列表生成式 list = [result for x in range(m, n)] g1 = (i for i in rang ...
- Flask常用API
Flask常用API 1.os 拼接路径:pathname = os.path.join(basepath, filename) 获得文件名后缀:suffix = os.path.splitext ...
- Django前后端安全验证
会话技术 关注公众号"轻松学编程"了解更多. 1.Cookie 客户端会话技术(数据存储在客户端) 以key-value的形式进行存储 cookie的操作都是通过Response来 ...
- Git命令之diff
工作区(working tree),暂存区(index /stage),本地仓库(repository) git跟不同的参数,比较不同的区间的版本. git diff:是查看working tree与 ...
- 什么是4G模块 4G模块的工作原理及特点
什么是4G模块 4G模块,也被叫做4G通信模块或4G DTU模块,他是物联网行业具有4G通信功能的一种产品,通过4G模块,我们可以实现工业设备数据通过无线4G网络传输到远端控制中心,并从控制中心通过4 ...
- Dreamweaver是怎么把图片转换成代码 简单五步骤即可解决
Dreamweaver图片转换代码图文介绍 1.打开需要转换的Photoshop作品: 2.保存为web格式,得到一个文件夹和一个html格式文件: 3.在html格式文件上单击右键,选择打开方式为D ...
- [MIT6.006] 5. Binary Search Trees, BST Sort 二分搜索树,BST排序
第5节课主要讲述了二分搜索树概念和BST排序.讲师提出一个关于"跑道预订系统"的问题,假设飞机场只有一个跑道,飞机需要为未来降落时间t进行预订,如果时间集合R中,在t时间前后k分钟 ...
- 二:Redis:(REmote DIctionary Server)远程字典服务器
Redis是完全开源免费的,用C语言编写的,遵循BSD协议,是一个高性能的(key-value)分布式内存数据库,基于内存运行,并支持持久化的NOSQL数据库,是当前最热门的NOSQL数据库之一,也被 ...
- 【鸿蒙应用开发】使用确切位置布局(PositionLayout)实现登录页面
上一节我们了解了PositionLayout(确切位置布局,我更倾向于称为绝对布局),虽然应用场景稀少.维护不方便,但是该有的示例还是不能少. UI图拆解及代码实现 这个界面我们是不是很熟悉,打开浏览 ...