LeetCode 솔루션 분류
[5/12] 47. Permutations II
본문
[LeetCode 시즌 3] 2022년 5월 12일 문제입니다.
https://leetcode.com/problems/permutations-ii/
47. Permutations II
Medium
492593Add to ListShareGiven a collection of numbers, nums
, that might contain duplicates, return all possible unique permutations in any order.
Example 1:
Input: nums = [1,1,2] Output: [[1,1,2], [1,2,1], [2,1,1]]
Example 2:
Input: nums = [1,2,3] Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
Constraints:
1 <= nums.length <= 8
-10 <= nums[i] <= 10
관련자료
-
링크
댓글 1
austin님의 댓글
- 익명
- 작성일
Runtime: 0 ms, faster than 100.00% of C++ online submissions for Permutations II.
Memory Usage: 8.5 MB, less than 89.32% of C++ online submissions for Permutations II.
Memory Usage: 8.5 MB, less than 89.32% of C++ online submissions for Permutations II.
class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
unordered_map<int, int> m;
for(auto n : nums) ++m[n];
vector<vector<int>> ret;
vector<int> p;
auto check = [&](auto& self) -> void {
bool last = true;
for(auto &[val, count] : m) {
if (count) {
p.emplace_back(val);
--count;
self(self);
p.pop_back();
++count;
last = false;
}
}
if (last) ret.emplace_back(p);
};
check(check);
return ret;
}
};