엔지니어 게시판
LeetCode 솔루션 분류

[5/12] 47. Permutations II

컨텐츠 정보

본문

[LeetCode 시즌 3] 2022년 5월 12일 문제입니다.

https://leetcode.com/problems/permutations-ii/


47. Permutations II
Medium
492593Add to ListShare

Given 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.
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;        
    }
};
전체 396 / 8 페이지
번호
제목
이름

최근글


인기글


새댓글


Stats


  • 현재 접속자 184 명
  • 오늘 방문자 5,683 명
  • 어제 방문자 5,332 명
  • 최대 방문자 11,134 명
  • 전체 회원수 1,048 명
알림 0