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

【题意】

给定一个候选数集合,候选集中可能存在重复数,返回所有的排列

【思路】

思路和Permutations是一样的,使用递归、类DFS的方法求解

关键是要注意去重。

【代码】

class Solution {
public:
    void permute(vector<vector<int> >&result, vector<int>combination, vector<int>candidates, int index2remove){
        //combination——当前已生成的组合
        //candidates——候选数字集合
        //index2remove——添加到combination的下一个数字在candidates中的索引位
        combination.push_back(candidates[index2remove]);
        candidates.erase(candidates.begin()+index2remove);
        if(candidates.empty()){
            result.push_back(combination);
        }
        else{
            for(int i=0; i<candidates.size(); i++){
                if(i!=0 && candidates[i]==candidates[i-1])continue; //去重
                permute(result, combination, candidates, i);
            }
        }
    }

    vector<vector<int> > permuteUnique(vector<int> &num) {
        vector<vector<int> >result;
        int size=num.size();
        if(size==0)return result;
        sort(num.begin(), num.end()); //先排序,便于排重
        vector<int>combination;
        for(int i=0; i<size; i++){
            if(i!=0 && num[i]==num[i-1])continue;   //去重
            permute(result, combination, num, i);
        }
        return result;
    }
};

LeetCode: Permutations II [046],布布扣,bubuko.com

时间: 2024-10-16 13:21:47

LeetCode: Permutations II [046]的相关文章

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排序,然

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]. 难度:80+15(怎样跳过重复的case来节省时间).我一开始的想法很简单,就是在permutation这道题的

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]. 原题链接:https://oj.leetcode.com/problems/permutations-ii/ 题

[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]. Show Tags Backtracking 这个就是输入一个数组,可能有重复,输出全部的排列. 这题调用stl 就

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