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 ...
随机推荐
- 机器学习:Jupyter Notebook中numpy的使用
一.Jupyter Notebook的魔法命令 # 模块/方法 + ?或者help(模块/方法):查看模块/方法的解释文档: 1)%run # 机械学习中主要应用两个魔法命令:%run.%timeit ...
- T-SQL 高级编程
在Sql Server 中访问数据库一般有2种方式: 1.一种是使用应用程序编程接口API 2.数据库语句 变量:局部变量:以@为前缀,如@Age:全局变量以@@为前缀:(Ps:全局变量以系统定义和维 ...
- Postman(调试工具)
Postman Postman用法简介-Http请求模拟工具 时间 2015-09-26 23:52:00 博客园-原创精华区 原文 http://www.cnblogs.com/codingbl ...
- CodeForces 492E Vanya and Field (思维题)
E. Vanya and Field time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 23. Bypass ngx_lua_waf SQL注入防御(多姿势)
0x00 前言 ngx_lua_waf是一款基于ngx_lua的web应用防火墙,使用简单,高性能.轻量级.默认防御规则在wafconf目录中,摘录几条核心的SQL注入防御规则: select.+(f ...
- c++中字符串的截取:
c++中字符串的截取: string 类提供字符串处理函数,利用这些函数,程序员可以在字符串内查找字符,提取连续字符序列(称为子串),以及在字符串中删除和添加.我们将介绍一些主要函数. 1.函数fin ...
- hdu1072
#include <iostream> #include <cstdio> #include <cstring> #include <queue> us ...
- Citrix 未注册解决办法
Citrix 经常出现未注册的问题 是因为DNS的解析 问题 ping DDC 的全名你会发现ping 不通 解决方案如下 首先 在 192.168.1.145(图站)上饭解析一下DDC(控制中心19 ...
- pyMongo 一些基本操作
1. find() 函数, 可以在函数体内直接指定 filter, sort, projection(限制field), 语法如下: datas = col.find( filter = {" ...
- tcp/ip四次挥手
四次分手: 由于TCP连接是全双工的,因此每个方向都必须单独进行关闭.这个原则是当一方完成它的数据发送任务后就能发送一个FIN来终止这个方向的连接.收到一个 FIN只意味着这一方向上没有数据流动,一个 ...