Given a collection of numbers that might contain duplicates, return all possible unique permutations.
For example,
[1,1,2] have the following unique permutations:
[
[1,1,2],
[1,2,1],
[2,1,1]
]
题目大意:返回一个数字集合构成的数字(可能有重复数字)的所有可能的唯一的全排列~
分析:先对nums排序,然后将所有全排列加入result中~
1 2 3 4 5 6 7 8 9 10 11 |
class Solution { public: vector<vector<int>> permuteUnique(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 柳婼