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].

解法一:递归

class Solution {
public:
vector<vector<int> > permute(vector<int> &num) {
vector<vector<int> > ret;
Helper(ret, num, );
return ret;
}
void Helper(vector<vector<int> >& ret, vector<int> num, int pos)
{
if(pos == num.size()-)
ret.push_back(num);
else
{
for(int i = pos; i < num.size(); i ++)
{//swap all the ints to the current position
swap(num[pos], num[i]);
Helper(ret, num, pos+);
swap(num[pos], num[i]);
}
}
}
};

解法二:just a joke

别忘了先排序,因为next_permutation是升序返回的。

class Solution
{
public:
vector<vector<int> > permute(vector<int> &num)
{
vector<vector<int> > result;
//sort first
//note that next_permutation is in ascending order
sort(num.begin(), num.end());
result.push_back(num);
while(next_permutation(num.begin(), num.end()))
{
result.push_back(num);
}
return result;
}
};

【LeetCode】46. Permutations (2 solutions)的更多相关文章

  1. 【LeetCode】46. Permutations 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:库函数 方法二:递归 方法三:回溯法 日期 题目地址:h ...

  2. 【LeetCode】47. Permutations II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...

  3. 【一天一道LeetCode】#46. Permutations

    一天一道LeetCode系列 (一)题目 Given a collection of distinct numbers, return all possible permutations. For e ...

  4. 【LeetCode】47. Permutations II

    Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...

  5. 【LeetCode】047. Permutations II

    题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...

  6. 【LeetCode】046. Permutations

    题目: Given a collection of distinct numbers, return all possible permutations. For example,[1,2,3] ha ...

  7. 【LeetCode】18. 4Sum (2 solutions)

    4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d  ...

  8. 【LeetCode】49. Anagrams (2 solutions)

    Anagrams Given an array of strings, return all groups of strings that are anagrams. Note: All inputs ...

  9. 【LeetCode】120. Triangle (3 solutions)

    Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...

随机推荐

  1. Mysql/MariaDB的多主集群实现:Galera Cluster

    Galera Cluster是Codership公司开发的一套免费开源的高可用方案,属于multi-master的集群架构,如图所示: 三个实例,组成了一个集群,而这三个节点与普通的主从架构不同,它们 ...

  2. Configuring HDFS High Availability

    Configuring HDFS High Availability 原文请訪问 http://blog.csdn.net/ashic/article/details/47024617,突袭新闻小灵儿 ...

  3. @JVM垃圾回收机制的一些概念

    数据类型 Java虚拟机中,数据类型可以分为两类:基本数据类型和引用数据类型 .基本类型的变量保存的值就是数值本身:而引用类型的变量保存引用值."引用值"代表了某个对象的引用,而不 ...

  4. 网页IE轻松调用VLC播放器实现监控(组件+方法大全)【转】

    公司突发奇想,要把刚买回来的网络监控机用自己内部网站在线监控. 作为网站的开发员,我接下了这个任务. 网络上有很多资料参与,但是都不全都不尽人意,最后经过多次的不同关键字的查找和测试,总算让我成功了. ...

  5. 超级简单的jquery操作表格(添加/删除行、添加/删除列)

    利用jquery给指定的table添加一行.删除一行 <script language="javascript" src="./jquery.js"> ...

  6. windows server 2008 远程桌面(授权、普通用户登录)~ .

    大家好,因公司上ERP系统,用户端需要远程到服务器,但大家都知道微软默认只有2个,所以没有办法达到我公司的要求. 在网上找了很久也没有找到合适的文章,要不就这里说一点,那里说一点,没有一个全的,还有很 ...

  7. RUP

    RUP随想 [摘要] 本文主要阐述一下我对RUP软件工程思想的看法以及一些感想.我认为软件工程既然是工程,那么纯粹的空谈理论是没有意义的,软件工程需要实干.我认为软件工程的思想实际上和兵法理论是一样的 ...

  8. Jmeter测试报告可视化(Excel, html以及jenkins集成)

    做性能测试通常在none GUI的命令行模式下运行Jmeter. 例如: jmeter -n -t /opt/las/JMeter/TestPlan/test.jmx -l /opt/las/JMet ...

  9. 空格在Html中的表示方法(&nbsp含义)

    转自:http://www.cnblogs.com/hailexuexi/archive/2010/07/25/1784611.html 在web开发经常会遇到如:   这样的字符.它其实是Html将 ...

  10. 如何为Android上的产品设计一款合适的图标

    如 果你已经完成了你的app,你一定会马上向其它人宣布这件事情.但是你需要注意一个很重要的问题,那就是app的图标.你的图标可能在项目启动之 前就已经设计好了,但我不喜欢这样,如果app没有完成实际上 ...