zoukankan      html  css  js  c++  java
  • [LintCode] 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], and [2,1,1].

    class Solution {
    public:
        vector<vector<int>> permuteUnique(vector<int>& nums) {
            vector<vector<int>> paths;
            if (nums.empty()) {
                return paths;
            }
            
            vector<int> index;
            vector<int> path;
            permuteUniqueHelper(nums, index, path, paths);
            return paths;
            
        }
        
    private:
        void permuteUniqueHelper(const vector<int> &nums,
                                 vector<int> &index,
                                 vector<int> &path,
                                 vector<vector<int>> &paths) {
            if (path.size() == nums.size()) {
                paths.push_back(path);
                return;
            }
            
            // 保证相同的数不在同一位置出现两次以上
            unordered_set<int> hashset;
            for (int ix = 0; ix < nums.size(); ix++) {
                if (find(index.begin(), index.end(), ix) == index.end() && hashset.count(nums[ix]) == 0) {
                    hashset.insert(nums[ix]);
                    
                    index.push_back(ix);
                    path.push_back(nums[ix]);
                    permuteUniqueHelper(nums, index, path, paths);
                    index.pop_back();
                    path.pop_back();
                }
            } 
        }
    };
    能否对空间复杂度做进一步的优化?
    class Solution {
    public:
        /**
         * @param nums: A list of integers.
         * @return: A list of unique permutations.
         */
        vector<vector<int> > permuteUnique(vector<int> &nums) {
            // write your code here
            vector<vector<int>> paths;
            if (nums.empty()) {
                return paths;
            }
            
            sort(nums.begin(), nums.end());
            bool *visited = new bool[nums.size()]();
            vector<int> path;
            permuteUniqueHelper(nums, visited, path, paths);
            return paths;
        }
        
    private:
        void permuteUniqueHelper(const vector<int> &nums,
                                 bool visited[],
                                 vector<int> &path,
                                 vector<vector<int>> &paths) {
            if (path.size() == nums.size()) {
                paths.push_back(path);
                return;
            } 
            
            for (int ix = 0; ix < nums.size(); ix++) {
                if (visited[ix] == true || ix > 0 && nums[ix - 1] == nums[ix] && visited[ix - 1] == false) {
                    continue;    
                }
                
                visited[ix] = true;
                path.push_back(nums[ix]);
                permuteUniqueHelper(nums, visited, path, paths);
                visited[ix] = false;
                path.pop_back();
            }
        }
    };
  • 相关阅读:
    简单两行,实现无线WiFi共享上网,手机抓包再也不用愁了
    Windows下Python 3.6 安装BeautifulSoup库
    RSA加密算法破解及原理
    干货,Wireshark使用技巧-过滤规则
    干货:Wireshark使用技巧-显示规则
    干货!链家二手房数据抓取及内容解析要点
    Wireshark分析实战:某达速递登录帐号密码提取
    协议分析中的TCP/IP网络协议
    Wireshark使用教程:不同报文颜色的含义
    VMware kali虚拟机环境配置
  • 原文地址:https://www.cnblogs.com/jianxinzhou/p/4525222.html
Copyright © 2011-2022 走看看