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. [Android UI] ActionBar 自定义属性

    actionbar 默认放在顶部, 如果在application或者activity中加入 android:uiOptions="splitActionBarWhenNarrow" ...

  2. python pandas.Series&&DataFrame&& set_index&reset_index

    参考CookBook :http://pandas.pydata.org/pandas-docs/stable/cookbook.html Pandas set_index&reset_ind ...

  3. Web页面切图和CSS注意事项

    一.Asp.net中的线程池设置 在Asp.net的服务处理中,每当服务器收到一个请求,HttpRuntime将从HttpApplication池中获取一个HttpApplication对象处理此请求 ...

  4. 五个瓶颈影响你的Asp.Net程序(网站)性能

    在今天的手机设备世界里,生活的节奏继续加快,因此访问你的网站的用户的耐心也在渐渐失去.同时,我提供了非常多的特性,为了防止你的网站变得过时或者廉价,你必须跟上竞争对手.你想赢得访问者的喝彩,但访问者没 ...

  5. jQuery图片上传前先在本地预览

    js代码: /* *名称:图片上传本地预览插件 v1.1 *作者:周祥 *时间:2013年11月26日 *介绍:基于JQUERY扩展,图片上传预览插件 目前兼容浏览器(IE 谷歌 火狐) 不支持saf ...

  6. idea 创建 简单的scala maven项目

    1.创建maven scala项目 2.maven配置 <properties> <scala.version>2.11.8</scala.version> < ...

  7. 正则 js截取时间

    项目中要把时间截取,只要年月日,不要时分秒,于是 /\d{4}-\d{1,2}-\d{1,2}/g.exec("2012-6-18 00:00:00") 或者另一种 var dat ...

  8. (LeetCode 41)First Missing Positive

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...

  9. com.esotericsoftware.kryo.kryoexception java.util.ConcurentModificationException

    近期 有网友看我的"整合Kafka到Spark Streaming--代码演示样例和挑战"文章, 讲 kafka对象 放到 pool 并通过broadcast广播出去: 然后 在开 ...

  10. URLRewriter.dll的使用

    http://www.cnblogs.com/Jaylong/archive/2011/10/17/url.html 微软URLRewriter.dll的url重写的简单使用 先添加引用URLRewr ...