码迷,mamicode.com
首页 > 其他好文 > 详细

【LeetCode】047. Permutations II

时间:2017-05-07 21:57:30      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:dfs   blog   ++   sharp   题目   color   class   arp   for   

题目:

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;
    }
};

 

【LeetCode】047. Permutations II

标签:dfs   blog   ++   sharp   题目   color   class   arp   for   

原文地址:http://www.cnblogs.com/Atanisi/p/6789182.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!