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 全排列的更多相关文章

  1. LeetCode 046 Permutations

    题目要求:Permutations(全排列) Given a collection of numbers, return all possible permutations. For example, ...

  2. 046 Permutations 全排列

    给定一个含有不同数字的集合,返回所有可能的全排列.比如,[1,2,3] 具有如下排列:[  [1,2,3],  [1,3,2],  [2,1,3],  [2,3,1],  [3,1,2],  [3,2 ...

  3. [LeetCode] 46. Permutations 全排列

    Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...

  4. [leetcode]46. Permutations全排列(给定序列无重复元素)

    Given a collection of distinct integers, return all possible permutations. Input: [1,2,3] Output: [ ...

  5. [leetcode]47. Permutations全排列(给定序列有重复元素)

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  6. 【LeetCode】Permutations(全排列)

    这道题是LeetCode里的第46道题. 题目要求: 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3 ...

  7. Java for LeetCode 046 Permutations

    Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the follo ...

  8. [LeetCode] 47. Permutations II 全排列 II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  9. Java for LeetCode 047 Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

随机推荐

  1. w3c上的SQL 教程---基本语法 语句学习

    SQL 教程路径:http://www.w3school.com.cn/sql/index.asp

  2. Sublime Text2中Evernote 插件的使用

    Sublime Text2是个强大的编辑器, 有好多插件供我们使用, 其中有个插件SublimeEvernote, 可以把代码发送到Evernote里. 但是没找见使用说明, 今天看了下Sublime ...

  3. poj 1519 Digital Roots (计算根数字)

    一.Description The digital root of a positive integer is found by summing the digits of the integer. ...

  4. 【转】 Pro Android学习笔记(六十):Preferences(4):MultiSelect List Preference

    目录(?)[-] XML文件 在设备中保存 读出信息 ListPreference提供单选列表,我们可以通过CheckBoxPreference提供多选列表.此外,Android在3.0后提供Mult ...

  5. Linq to Object之非延迟标准查询操作符

    非延时标准查询操作符是指不具备延时查询特性的标准查询操作符,这些操作符一般用于辅助延时标准查询操作符使用. 1.ToArray操作符 ToArray操作符用于将一个输入序列转换成一个数组. 方法原型: ...

  6. 在vue项目中使用md5加密

    MD5:信息-摘要算法,是让大容量信息在用数字签名软件签署私人密匙前被"压缩"成一种保密的格式 一般我们把登录和注册信息的密码进行加密 1.安装模块 cnpm install js ...

  7. centos6.5安装tomcat7.0教程(二)

    阅读之前对基本命不熟悉的话, 可以先安装另一文章: http://www.cnblogs.com/duenboa/articles/6665159.html把基本的命令记一下.后面的文章就不重复演示了 ...

  8. python+selenium简单实现拖动元素实例

    from  selenium  import  webdriver#引入ActionChains类from  selenium.webdriver.common.action_chains  impo ...

  9. IOS网络同步请求

    //1.目标地址 NSString *url_string = @"http://b33.photo.store.qq.com/psu?/05ded9dc-1001-4be2-b975-13 ...

  10. 在PCL中如何实现点云压缩(2)

    博客转载自:http://www.pclcn.org/study/shownews.php?lang=cn&id=125 压缩配置文件: 压缩配置文件为PCL点云编码器定义了参数集.并针对压缩 ...