C++ Simple Solution


  • 0
    class Solution {
    public:
        void helper(vector<vector<int>>& res, vector<int>& vec, unordered_map<int, int>& mp, int cnt) {
            if (vec.size() == cnt) {
                res.push_back(vec);
                return;
            }
            for (auto& i : mp) {
                if (i.second < 1)
                    continue;
                vec.push_back(i.first);
                i.second--;
                helper(res, vec, mp, cnt);
                i.second++;
                vec.pop_back();
            }
        }
        vector<vector<int>> permuteUnique(vector<int>& nums) {
            unordered_map<int, int> mp;
            for (auto num : nums)
                mp[num]++;
            vector<vector<int>> res;
            vector<int> vec;
            helper(res, vec, mp, nums.size());
            return res;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.