标签:
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],
and [3,2,1].
Subscribe to see which companies asked this question
//思路首先:算了,还是用stl来做吧
//再次来看nextPermutation的原理:
//但是比较明显的规律是:
//1,升序为最小组合数,降序为最大组合数
//2,某一个数的下一个数,就是比他大的最小组合数,比如123,下一个比他大的最小组合就是132,所以必须从低位处理
//3,找到首个升序序列将其交换,然后逆置后面的数。比如:1234311
//为1243311(首个升序已经交换),显然此数不是最小,并且后面的数逆置后才是最小的,即1243311为1243113。so,done!。
//参考我的另一篇文章:http://blog.csdn.net/ebowtang/article/details/50450861
class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
int col=nums.size();
int row=1;
for(int i=1;i<=col;i++)//求阶乘
row*=i;
vector< vector<int> > result(row);
for(int i=0;i < row ;i++)
result[i].resize(col);//设置数组的大小row行,col列
if(nums.empty())
return result;
vector<int> tmpnums=nums;
sort(tmpnums.begin(),tmpnums.end());//先排序
result[0]=tmpnums;
for(int i=1;i<row;i++)
{
next_permutation(tmpnums.begin(), tmpnums.end());
result[i]=tmpnums;
}
return result;
}
};
注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!
原文地址:http://blog.csdn.net/ebowtang/article/details/50488439
原作者博客:http://blog.csdn.net/ebowtang
<LeetCode OJ> 46. Permutations
标签:
原文地址:http://blog.csdn.net/ebowtang/article/details/50488439