标签:
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>> result; public: vector<vector<int> > permute(vector<int> &num) { int length=num.size(); perm(num,0,length-1); return result; } void perm(vector<int> &num,int k,int m){ if(k==m) result.push_back(num); for(int i=k;i<=m;i++){ swap(num[k],num[i]); perm(num,k+1,m); swap(num[k],num[i]); } } void swap(int &a,int &b){ int temp=a; a=b; b=temp; } };
标签:
原文地址:http://blog.csdn.net/sxhlovehmm/article/details/45175427