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 ...
随机推荐
- shell脚本之文件测试表达式
1.文件测试表达式的用法 我们在编程时处理一个对象时,需要对对象进行测试,只有符合要求的才采取操作处理:这样做的好处是避免程序出错以及无所畏的消耗系统资源,这个测试的对象可以是文件.字符串.数字等. ...
- git 出现 error: bad signature fatal: index file corrupt
一次大改版,提交了很多代码,但再次提交提交不了,也拉不下来仓库的代码 提示error bad signature fatal: index file corrupt 在项目有.git这同级打开Git ...
- Git撤销文件修改
在旧版本中,git的撤销工作区的文件修改是用git checkout -- <file>命令,由于容易漏了--导致和切换分支混肴,所以新版本中: - 使用git restore (--wo ...
- Java_Math类和Random类
Math类 java.lang.Math提供了一系列静态方法用于科学计算, 其方法的参数和返回值类型一般都为double型, 如果需要更加强大的数学运算能力计算高等数学中的相关内容, 可使用apach ...
- dhcp、tftp、httpd、pxe安装CentOS6.9
虚拟机网络设置 要xshell连接虚拟机注意设置VMware Network Adapter VMnet2在同一网段 1.利用光盘配置本地yum源 [root@ZYB ~]# mount -r /de ...
- Android NurReaderView 阅读器 (字符串-.txt文件)
有些地方还没配置好.2/3天后在更新.... 功能 支持字符串和<.txt>文件 文字自动分各个页面 支持从右到左-(从右边开始的语言.比如维吾尔语哈扎克语...外国的阿拉伯语等) 支持自 ...
- MySQL全面瓦解10:分组查询和聚合函数
概述 相信我们经常会遇到这样的场景:想要了解双十一天猫购买化妆品的人员中平均消费额度是多少(这可能有利于对商品价格区间的定位):或者不同年龄段的化妆品消费占比是多少(这可能有助于对商品备货量的预估). ...
- 我发现了一个特别Man的Linux工具!!!
Linux命令不用我多说吧,谁还不会几个?但是一个命令可能有几十种用法,就拿最简单也是最常用的ls来举例,它就有将近20种options用法 比如 ls -a :现实所有文件及其隐藏文件 ls -t ...
- Kubernetes笔记(六):了解控制器 —— Deployment
Pod(容器组)是 Kubernetes 中最小的调度单元,可以通过 yaml 定义文件直接创建一个 Pod.但 Pod 本身并不具备自我恢复(self-healing)功能.如果一个 Pod 所在的 ...
- zabbix自动发现的python方式数据生成
前言 zabbix里面有个功能是自动发现,比如文件系统和网卡的获取的时候,因为预先无法知道这个网卡的名称,所以就有了这个自动发现的功能,这里我是因为要用到存储池的自动发现,所以需要对数据进行生成 实现 ...