标签:style blog http io ar color os sp for
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]
.
Backtracking
class Solution { private: vector<vector<int> > ret; public: void perm(vector<int> num,int i){ if(i==num.size()){ ret.push_back(num); return; } for(int j=i;j<num.size();j++){ swap(num[i],num[j]); perm(num,i+1); swap(num[j],num[i]); //复原,进行下一个交换前需复原之前状态 } } vector<vector<int> > permute(vector<int> &num) { perm(num,0); return ret; } };
标签:style blog http io ar color os sp for
原文地址:http://www.cnblogs.com/li303491/p/4114500.html