Given a collection of distinct 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],
[3,2,1]
]
题目大意:给一个不同数字的集合,返回它的所有可能的排列~
分析:对nums进行排序,然后将所有全排列结果放入result数组中返回~
1 2 3 4 5 6 7 8 9 10 11 |
class Solution { public: vector<vector<int>> permute(vector<int>& nums) { vector<vector<int>> result; sort(nums.begin(), nums.end()); do { result.push_back(nums); } while (next_permutation(nums.begin(), nums.end())); return result; } }; |
❤ 点击这里 -> 订阅《PAT | 蓝桥 | LeetCode学习路径 & 刷题经验》by 柳婼