标签:
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]
.
参考文章:http://blog.csdn.net/tuantuanls/article/details/8717262
全排列问题。
1 class Solution{ 2 public: 3 vector<vector<int>>permute(vector<int> &num){ 4 int N=num.size(); 5 vector<vector<int>> result; 6 7 if(N==1) 8 { 9 result.push_back(num); 10 return result; 11 } 12 13 vector<vector<int>> post; 14 15 vector<int> cur; 16 vector<int> tmp; 17 18 for(int i=0;i<N;i++) 19 { 20 cur=num; 21 cur.erase(cur.begin()+i); 22 post=permute(cur); 23 for(int j=0;j<post.size();j++) 24 { 25 tmp=post[j]; 26 tmp.insert(tmp.begin(),num[i]); 27 result.push_back(tmp); 28 } 29 } 30 31 return result; 32 } 33 };
标签:
原文地址:http://www.cnblogs.com/jawiezhu/p/4463546.html