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]
.
Notes: recursive, in place.
class Solution {
public:
vector<vector<int> > permute(vector<int> &num) {
int size = num.size();
vector<vector<int> > result;
if(size == )
return result;
if(size == ) {
vector<int> per;
per.push_back(num[]);
result.push_back(per);
return result;
} for(int i = ; i < size; i ++) {
int current = num[i];
num.erase(num.begin() + i);
vector<vector<int> > ret = permute(num);
num.insert(num.begin() + i, current);
for(auto item: ret) {
item.insert(item.begin(), current);
result.push_back(item);
}
}
return result;
}
};