• 【LeetCode】047. Permutations II


    题目:

    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]
    ]
    

      

    题解:

    Solution 1 (TLE)

    class Solution {
    public:
        void dfs(vector<int> nums, vector<vector<int>>& vv, vector<int>& v, vector<int>& visited, int level) {
            if(level >= nums.size()) {
                if(find(vv.begin(), vv.end(), v) == vv.end())
                    vv.push_back(v);
                return;
            }
            for(int i=0; i<nums.size(); ++i) {
                if(visited[i] != 0) continue;
                v.push_back(nums[i]);
                visited[i] = 1;
                dfs(nums, vv, v, visited, level+1);
                v.pop_back();
                visited[i] = 0;
            }
        }
        vector<vector<int>> permuteUnique(vector<int>& nums) {
            vector<vector<int>> vv;
            vector<int> v, visited(nums.size(),0);
            dfs(nums, vv, v, visited, 0);
            return vv;        
        }
    };

     Solution 2 ()

    class Solution {
    public:
        void dfs(vector<int> nums, vector<vector<int>>& vv, vector<int>& v, vector<int>& visited, int level) {
            if(level >= nums.size()) {
                vv.push_back(v);
                return;
            }
            for(int i=0; i<nums.size(); ++i) {
                if(visited[i] != 0) continue;
                if(i>0 && nums[i] == nums[i-1] && visited[i-1] == 0) continue;
                v.push_back(nums[i]);
                visited[i] = 1;
                dfs(nums, vv, v, visited, level+1);
                v.pop_back();
                visited[i] = 0;
            }
        }
        vector<vector<int>> permuteUnique(vector<int>& nums) {
            vector<vector<int>> vv;
            vector<int> v, visited(nums.size(),0);
            sort(nums.begin(), nums.end());
            dfs(nums, vv, v, visited, 0);
            return vv;        
        }
    };

    Solution 3 ()

    class Solution {
    public:
        void dfs(vector<int> nums, set<vector<int>>& sv, int level) {
            if(level >= nums.size()) {
                sv.insert(nums);
                return;
            }
            for(int i=level; i<nums.size(); ++i) {
                if(i != level && nums[i] == nums[level]) continue;
                swap(nums[i], nums[level]);
                dfs(nums, sv, level+1);
                swap(nums[i], nums[level]);
            }
        }
        vector<vector<int>> permuteUnique(vector<int>& nums) {
            set<vector<int>> sv;
            vector<int> v, visited(nums.size(),0);
            sort(nums.begin(), nums.end());
            dfs(nums, sv, 0);
            return vector<vector<int>> (sv.begin(), sv.end());        
        }
    };

    Solution 4 ()

    class Solution {
    public:
        vector<vector<int>> permuteUnique(vector<int>& nums) {
            vector<vector<int>> ans;
            vector<int> aux(nums.begin(), nums.end());
            sort(aux.begin(), aux.end());
            do {
                ans.emplace_back(aux.begin(), aux.end());
            } while(next_permutation(aux.begin(), aux.end()));
            return ans;
        }
    };
  • 相关阅读:
    C# Firefox Session Manager 文件的导出与管理
    安徒生的童话《冰雪皇后》原本是这样的
    许多人不知道的生活小秘方
    洗衣服窍门大全
    小窍门解决大问题(绝对值得收藏)
    日常生活小技巧
    谷歌浏览器应用商店打不开,下载不了扩展程序的解决办法
    食品安全如何让百姓放心
    把 WORD 里的换行符(向下的箭头)换成回车符(常用回车符)
    充满创意的生活小妙招 --爱生活爱创意!
  • 原文地址:https://www.cnblogs.com/Atanisi/p/6789182.html
Copyright © 2020-2023  润新知