Permutations II [leetcode]

防止产生重复的排列字串,即在排列时不会重复的交换

第一种思路是在递归函数中用set检查当前交换的数字是否已经换过,

如{0,1,0,2}中start=0时:

交换num[0]和num[0],并产生start=1的子排列

交换num[0]和num[1],并产生start=1的子排列

交换num[0]和num[2],并产生start=1的子排列(产生重复)

交换num[0]和num[3],并产生start=1的子排列

也可以理解为位置start只能取备选集num[start...n-1]中不同的数字

vector<vector<int> > permuteUnique(vector<int> &num) {
        vector<vector<int>> res;
        permute(num, res, 0);
        return res;
    }

    void permute(vector<int> &num, vector<vector<int> > & res, int start)
    {
        if (start == num.size())
        {
            res.push_back(num);
            return;
        }

        set<int> s;
        for (int i = start; i < num.size(); i++)
        {
            if (s.find(num[i]) == s.end())
            {
                s.insert(num[i]);
                swap(num, i, start);
                permute(num, res, start + 1);
                swap(num, i, start);
            }
        }
    }

    void swap(vector<int> &num, int i, int j)
    {
        int temp = num[i];
        num[i] = num[j];
        num[j] = temp;
    }

第二种思路

如果不用set,我们可以用一个数组visited记录当前已经选的index,并用另一个数组out存数字的顺序

此时需要对数组排序,并加上for(int curI = i; i < num.size() && num[i] == num[curI]; i++);跳过相同的数字

vector<vector<int> > permuteUnique(vector<int> &num) {
        vector<vector<int>> res;
        vector<int> out;
        vector<bool> visited(num.size());
        sort(num.begin(), num.end());
        permute(num, res, out, visited);
        return res;
    }

    void permute(vector<int> &num, vector<vector<int> > & res, vector<int>& out, vector<bool>& visited)
    {
        if (out.size() == num.size())
        {
            res.push_back(out);
            return;
        }

        for (int i = 0; i < num.size(); i++)
        {
            if (!visited[i])
            {
                visited[i] = true;
                out.push_back(num[i]);
                permute(num, res, out, visited);
                out.pop_back();
                visited[i] = false;
                for(int curI = i; i < num.size() && num[i] == num[curI]; i++);
                i--;
            }
        }
    }
时间: 2024-12-15 01:52:16

Permutations II [leetcode]的相关文章

Permutations II leetcode java

题目: 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]. 题解: 这道题跟Permutaitons没啥大的区别,就是结果去重. 我之前也有写过去重的两个方法: 一

[Lintcode]16. Permutations II/[Leetcode]47. Permutations II

16. Permutations II/47. Permutations II 本题难度: Medium Topic: Search & Recursion Description Given a list of numbers with duplicate number in it. Find all unique permutations. Example Example 1: Input: [1,1] Output: [ [1,1]] Example 2: Input: [1,2,2] O

LeetCode: Permutations II [046]

[题目] 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]. [题意] 给定一个候选数集合,候选集中可能存在重复数,返回所有的排列 [思路] 思路和Permutat

[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刷题笔记】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 题解

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].题解:依旧使用的是DFS的思想. 首先需要遍历输入数组,获取一共有多少种不同的数字,每个数字有多少个. 最简单的方法,

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 047 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]. 代码如下: class Solution { public: vector

【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,当前排列数