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