Given
a collection of numbers, return all possible permutations.
class Solution {
public:
vector<vector<
int> > permute(vector<
int> &num)
{
bool used[num.size()];
for(
int i=
0;i<num.size();i++)
used[i]=
false;
vector<vector<
int> > result;
vector<
int> possible;
for(
int i=
0;i<num.size();i++)
possible.push_back(
0);
gen(result,num,used,
0,possible);
return result;
}
void gen(vector<vector<
int> >& result,vector<
int>& num,
bool* used,
int step,vector<
int>& possible)
{
if(step==num.size())
{
result.push_back(possible);
return;
}
for(
int i=
0;i<num.size();i++)
if(!used[i])
{
used[i]=
true;
possible[step]=num[i];
gen(result,num,used,step+
1,possible);
used[i]=
false;
}
}
};