LeetCode 0047 - Permutations II

2021-08-11 10:33:57 浏览数 (1)

Permutations II

Desicription

Given a collection of numbers that might contain duplicates, return all possible unique permutations.

For example,

[1,1,2] have the following unique permutations:

代码语言:javascript复制
[
  [1,1,2],
  [1,2,1],
  [2,1,1]
]

Solution

代码语言:javascript复制
class Solution {
public:
    vector<vector<int>> permuteUnique(vector<int>& nums) {
        vector<vector<int>> res;
        sort(nums.begin(), nums.end());
        do
            res.push_back(nums);
        while(next_permutation(nums.begin(), nums.end()));
        return res;
    }
};

0 人点赞