LeetCode 046 Permutations 全排列
Given a collection of distinct 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],
[3,2,1]
]
方法一:
class Solution {
public:
vector<vector<int> > permute(vector<int> &num) {
vector<vector<int> > res;
vector<int> out;
int size=num.size();
if(size==||num.empty())
return res;
helper(num, ,size, out, res);
return res;
}
void helper(vector<int> &num,int start,int size,vector<int> &out,vector<vector<int>> &res)
{
if(start==size-)
{
for(int i=;i<size;++i)
out.push_back(num[i]);
res.push_back(out);
out.clear();
}
else
{
for(int i=start;i<size;++i)
{
swap(num,start,i);
helper(num,start+,size,out,res);
swap(num,start,i);
}
}
}
void swap(vector<int> &num,int i,int j)
{
int tmp=num[i];
num[i]=num[j];
num[j]=tmp;
}
};
方法二:
class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> res;
helper(nums,,res);
return res;
}
void helper(vector<int> num,int start,vector<vector<int>> &res)
{
if(start==num.size())
{
res.push_back(num);
return;
}
for(int k=start;k<num.size();++k)
{
swap(num[start],num[k]);
helper(num,start+,res);
}
}
};
LeetCode 046 Permutations 全排列的更多相关文章
- LeetCode 046 Permutations
题目要求:Permutations(全排列) Given a collection of numbers, return all possible permutations. For example, ...
- 046 Permutations 全排列
给定一个含有不同数字的集合,返回所有可能的全排列.比如,[1,2,3] 具有如下排列:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2 ...
- [LeetCode] 46. Permutations 全排列
Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...
- [leetcode]46. Permutations全排列(给定序列无重复元素)
Given a collection of distinct integers, return all possible permutations. Input: [1,2,3] Output: [ ...
- [leetcode]47. Permutations全排列(给定序列有重复元素)
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 【LeetCode】Permutations(全排列)
这道题是LeetCode里的第46道题. 题目要求: 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3 ...
- Java for LeetCode 046 Permutations
Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the follo ...
- [LeetCode] 47. Permutations II 全排列 II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- Java for LeetCode 047 Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
随机推荐
- 洛谷【P1080】国王游戏
我对贪心的理解:https://www.cnblogs.com/AKMer/p/9776293.html 题目传送门:https://www.luogu.org/problemnew/show/P10 ...
- hdu 5909 Tree Cutting —— 点分治
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5909 点分治,每次的 rt 是必选的点: 考虑必须选根的一个连通块,可以DP,决策就是在每个子树中决定选不 ...
- 使用Rancher搭建K8S测试环境
使用Rancher搭建K8S测试环境 http://blog.csdn.net/csdn_duomaomao/article/details/75316926 环境准备(4台主机,Ubuntu16.0 ...
- Dockerfile创建MySQL容器
本文目的是创建一个MySQL的image,并且在新创建出来的容器里自动启动mysql服务接受外部连接 步骤: 1. 首先创建一个目录并在目录下创建一个Dockerfile,文件内容如下 FROM ce ...
- Linux CentOS安装Azure Cli工具
vim /etc/yum.repos.d/epel.repo [epel] name=epel baseurl=http://mirrors.sohu.com/fedora-epel/6/$basea ...
- flume入门之一:flume 安装及测试
http://flume.apache.org/ flume下载:http://mirror.bit.edu.cn/apache/flume/1.7.0/apache-flume-1.7.0-bin. ...
- [FATAL_ERROR] Uncaught PDOException: There is already an active transaction
[FATAL_ERROR] Uncaught PDOException: There is already an active transaction ... $mysql->beginTran ...
- day01_虚拟机与主机之间ip配置
虚拟机1: centos_ node1 虚拟机2:centos_node2 宿主主机虚拟机ip配置: vmnet1 来自为知笔记(Wiz)
- 高级查询子条件查询filter
Filter Context 在查询过程中,只判断该文档是否满足条件,只有Yes或者No { "query":{ "bool":{ //布尔关键词 " ...
- Flask13 面试要能吹 、安装虚拟机、虚拟机全局设置、导入虚拟机文件、虚拟机局部设置
1 web开发工作的三个能力 1.1 开发思想 易维护:开发成本远低于维护成本 可扩展:随着访问量的增加会自动使用多个数据库 高可用:程序就像小强一样,开发的系统能够经得住狂风暴雨的吹残(例如:一台主 ...