LeetCode Permutations II

class Solution {
    public:
        vector<vector<int> > permuteUnique(vector<int> &num) {
            vector<vector<int> > result;
            if (num.size() < 1) return result;
            vector<int> path;
            dfs(num, 0, path, result);
        }

        void dfs(vector<int>& dict, int pos, vector<int>& path, vector<vector<int> > &result) {
            int len = dict.size();
            if (pos >= len) {
                result.push_back(path);
                return;
            }
            unordered_set<int> uniques;
            for (int i=pos; i<len; i++) {
                if (i != pos && uniques.count(dict[i]) > 0) continue;
                uniques.insert(dict[i]);
                path.push_back(dict[i]);
                
                int tmp = dict[i];
                dict[i] = dict[pos];
                dict[pos] = tmp;
                
                dfs(dict, pos + 1, path, result);
                path.pop_back();

                tmp = dict[i];
                dict[i] = dict[pos];
                dict[pos] = tmp;
            }
        }
};

 也可以用next_permutation

class Solution {
    public:
    vector<vector<int> > permuteUnique(vector<int> &num) {
            vector<vector<int> > res;
            sort(num.begin(), num.end());
            do {
                res.push_back(num);
            } while (next_permutation(num.begin(), num.end()));
            return res;
        }
        
};

 

写一个不用hash表的

class Solution {
private:
    vector<vector<int>> res;

public:
    vector<vector<int>> permuteUnique(vector<int>& nums) {
        res.clear();
        sort(nums.begin(), nums.end());
        
        int len = nums.size();

        vector<int> sub;
        vector<bool> used(len);
        
        dfs(nums, used, sub);
        
        return res;
    }
    
    void dfs(vector<int>& nums, vector<bool>& used, vector<int>& sub) {
        int len = nums.size();
        
        if (sub.size() >= len) {
            res.push_back(sub);
            return;
        }
        
        for (int i=0; i<len; i++) {
            if (used[i] || (i>0 && !used[i-1] && nums[i] == nums[i-1])) {
                continue;
            }
            sub.push_back(nums[i]);
            used[i] = true;
            dfs(nums, used, sub);
            used[i] = false;
            sub.pop_back();
        }
    } 
};

 

还是这种方法比较简洁:

class Solution {
private:
    vector<vector<int>> res;
public:
    vector<vector<int>> permuteUnique(vector<int>& nums) {
        res.clear();
        dfs(nums, 0);
        return res;
    }
    
    void dfs(vector<int>& nums, int pos) {
        int len = nums.size();
        if (pos >= len) {
            res.push_back(nums);
            return;
        }
        
        unordered_set<int> st;
        for (int i=pos; i<len; i++) {
            if (st.count(nums[i]) > 0) {
                continue;
            }
            st.insert(nums[i]);
            swap(nums[i], nums[pos]);
            dfs(nums, pos + 1);
            swap(nums[i], nums[pos]);
        }
    }
};

 

posted @ 2014-05-27 19:00  卖程序的小歪  阅读(166)  评论(0编辑  收藏  举报