leetcode || 47、 Permutations II

problem:

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

题意:给定一个带重复数字的序列,输出其所有的排列

thinking:

(1)采用DFS,对已经访问过的元素标识

(2)考虑去重:对于重复的数字,规定一个访问顺序即可

先对数组进行排序,这样在DFS的时候,可以先判断前面的一个数是否和自己相等,相等的时候则前面的数必须使用了,自己才能使用,这样就不会产生重复的排列了。

code:

class Solution {
private:
    bool canUse[100];
    int a[100];
    vector<vector<int> > ret;
public:
    void dfs(int dep, int maxDep, vector<int> &num)
    {
        if (dep == maxDep)
        {
            vector<int> ans;
            for(int i = 0; i < maxDep; i++)
                ans.push_back(a[i]);
            ret.push_back(ans);
            return;
        }

        for(int i = 0; i < maxDep; i++)
            if (canUse[i])
            {
                if (i != 0 && num[i] == num[i-1] && canUse[i-1])
                    continue;

                canUse[i] = false;
                a[dep] = num[i];
                dfs(dep + 1, maxDep, num);
                canUse[i] = true;
            }
    }

    vector<vector<int> > permuteUnique(vector<int> &num) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        sort(num.begin(), num.end());
        memset(canUse, true, sizeof(canUse));
        ret.clear();
        dfs(0, num.size(), num);
        return ret;
    }
};
时间: 2024-08-30 07:07:16

leetcode || 47、 Permutations II的相关文章

【LeetCode】047. 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], [2,1,1] ] 题解: Solution 1 (TLE) class Solution { public: void dfs

leetcode || 46、Permutations

problem: Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the following permutations: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. Hide Tags Backtracking 题意:给定一个序列,输出其所有的排列组合 thinking: (1)之前写过求一个

leetcode || 90、Subsets II

problem: Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,2], a sol

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

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

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