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

Hide Tags

Backtracking

class Solution {
private:
    vector<vector<int> > ret;
public:
    void perm(vector<int> &num,int i,int len){
        if(i==len){
            ret.push_back(num);
            return;
        }
        for(int j=i;j<len;++j){
            if(!isSwap(num,i,j))
                continue;
            swap(num[i],num[j]);
            perm(num,i+1,len);
            swap(num[j],num[i]);
        }
    }
    bool isSwap(vector<int>& num, int i, int j) {
        while (num[i] != num[j] && i < j) i++;
        if (i == j) return true;
        else return false;
    }
    vector<vector<int> > permuteUnique(vector<int> &num) {
        int len=num.size();
        ret.clear();
        sort(num.begin(), num.end());
        perm(num,0,len);
        return ret;
    }
};

参考http://blog.csdn.net/morewindows/article/details/7370155

时间: 2024-12-18 05:41:27

Permutations II 去掉重复的全排列的相关文章

LeetCode 47. 全排列 II(Permutations II)

题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] 解题思路 类似于LeetCode46.全排列,只不过对于每个起始位置维护一个之前遍历过的数集,在交换时判断当前数是否在数集中出现,若出现过则不能交换此数,否则与起始位置交换并将此数加入到数集中. 代码 1 class Solution { 2 public: 3 vector<vector<int>> permuteU

[LeetCode][JavaScript]Permutations II

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]. https://leetcode.com/problems/permutations

leetcode 刷题之路 77 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]. Permutations 的升级版,依旧是全排列问题,但是序列中可能会出现重复数字. 思路:采用字典序的非递归方

【LeetCode】Permutations II

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]. 首先分析一下与Permutations有何差异. 记当前位置为start,当前排列数

leetCode 47.Permutations II (排列组合II) 解题思路和方法

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]. 思路:这题相比于上一题,是去除了反复项. 代码上与上题略有区别.详细代码例如以下

【leetcode刷题笔记】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]. 题解:跟Permutation差不多,只是这次会有重复的元素,如下图所示,如果只用DFS的话就会产生重复的排列: 上

LeetCode: Permutations II 解题报告

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]. SOLUTION 1: 还是经典的递归模板.需要处理的情况是:我们先把Num排序,然

[C++]LeetCode: 120 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]. 思路:这道题和Permutations的区别就是,输入的数字数组中包含重复的元素.如果我们对重复的元素不

LeetCode46,47 Permutations, Permutations II

题目: LeetCode46 I Given a collection of distinct numbers, return all possible permutations. (Medium) For example,[1,2,3] have the following permutations: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ] LeetCode47 II Given a collection of numb