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].
Solution: dfs(recursion)
1 class Solution { 2 public: 3 vector<vector<int> > res; 4 5 void permute(vector<int>& num, vector<int>& per, vector<bool>& isAvail) 6 { 7 if(per.size() == num.size()) { 8 res.push_back(per); 9 } 10 for(int i = 0; i < num.size(); i++) { 11 if(isAvail[i]) { 12 isAvail[i] = false; 13 per.push_back(num[i]); 14 permute(num, per, isAvail); 15 isAvail[i] = true; 16 per.pop_back(); 17 } 18 } 19 } 20 21 vector<vector<int> > permute(vector<int> &num) { 22 vector<int> per; 23 vector<bool> isAvail(num.size(),true); 24 permute(num, per, isAvail); 25 return res; 26 } 27 };