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: void perm(vector<int> &num, int i, int N, vector<vector<int>> &res) { if(i==N) res.push_back(num); for (int j=i;j<N;j++) { swap(num[i],num[j]); perm(num,i+1,N,res); swap(num[j],num[i]); } } vector<vector<int> > permute(vector<int> &num) { vector<vector<int> > res; perm(num,0,num.size(),res); return res; } };