vector<vector<int> > permute(vector<int> &num) {
vector<vector<int>> permutations;
if (num.size() <= 1)
{
permutations.push_back(num);
return permutations;
}
int lastNum = num.back();
num.pop_back();
vector<vector<int>> permutationsWithoutLastNum = permute(num);
for (int i = 0; i < permutationsWithoutLastNum.size(); i++)
{
vector<int> permutationWithoutLastNum = permutationsWithoutLastNum[i];
for (int j = 0; j <= permutationWithoutLastNum.size(); j++)
{
vector<int> permutation = permutationWithoutLastNum;
permutation.insert(permutation.begin()+j, lastNum);
permutations.push_back(permutation);
}
}
return permutations;
}
[LeetCode] Permutation
最新推荐文章于 2019-06-07 23:36:09 发布