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

46. Permutations 的拓展,这题数组含有重复的元素。解法和46题,主要是多出处理重复的数字。

先对nums排序,使得相同的元素排在一起。新建一个大小与nums相同visited数组,用来标记在本次DFS读取中,位置i的元素是否已经被添加。

如果当前的数与前一个数相等,并且前一个数未被添加到list中,则可跳过这个数。

C++:

class Solution {
public:
vector<vector<int> > permuteUnique(vector<int> &num) {
vector<vector<int> > res;
vector<int> out;
vector<int> visited(num.size(), 0);
sort(num.begin(), num.end());
permuteUniqueDFS(num, 0, visited, out, res);
return res;
}
void permuteUniqueDFS(vector<int> &num, int level, vector<int> &visited, vector<int> &out, vector<vector<int> > &res) {
if (level >= num.size()) res.push_back(out);
else {
for (int i = 0; i < num.size(); ++i) {
if (visited[i] == 0) {
if (i > 0 && num[i] == num[i - 1] && visited[i - 1] == 0) continue;
visited[i] = 1;
out.push_back(num[i]);
permuteUniqueDFS(num, level + 1, visited, out, res);
out.pop_back();
visited[i] = 0;
}
}
}
}
};

  

类似题目:

[LeetCode] 46. Permutations 全排列

[LeetCode] 31. Next Permutation 下一个排列

[LeetCode] 60. Permutation Sequence 序列排序

All LeetCode Questions List 题目汇总

[LeetCode] 47. Permutations II 全排列 II的更多相关文章

  1. [LeetCode] 47. Permutations II 全排列之二

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  2. LeetCode 47 Permutations II(全排列)

    题目链接: https://leetcode.com/problems/permutations-ii/?tab=Description   给出数组,数组中的元素可能有重复,求出所有的全排列   使 ...

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

    Permutations II  Given a collection of numbers that might contain duplicates, return all possible un ...

  4. LeetCode(47):全排列 II

    Medium! 题目描述: 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] 解题思路: 这道 ...

  5. [leetcode] 47. Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  6. [leetcode]47. Permutations全排列(给定序列有重复元素)

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  7. LeetCode 46 Permutations(全排列问题)

    题目链接:https://leetcode.com/problems/permutations/?tab=Description   Problem:给出一个数组(数组中的元素均不相同),求出这个数组 ...

  8. [LeetCode] 267. Palindrome Permutation II 回文全排列 II

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  9. Leetcode之回溯法专题-47. 全排列 II(Permutations II)

    Leetcode之回溯法专题-47. 全排列 II(Permutations II) 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2] ...

随机推荐

  1. 【后缀表达式求解】No.3.栈-evaluate-reverse-polish-notation题解(Java版)

    牛客网的题目链接 题目描述 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid opera ...

  2. 最全 webpak4.0 打包性能优化清单

    最全 webpak4.0 打包性能优化清单 webpack4.0如何进行打包优化? 无非是从两个角度进行优化,其一:优化打包速度,其二:优化打包体积,送你一份打包性能优化清单 1.使用loader的时 ...

  3. Mybatis容易遇到的问题

    1.MyBatis中#和$的区别? 1.使用#的原理是?占位符,而$的原理为直接字符串拼接方式 2.$方式一般使用在写数据库中的固定字段时候才会使用例如表名或者列名(select * from use ...

  4. 《BUG创造队》第四次作业:基于原型的团队项目需求调研与分析

    项目 内容 这个作业属于哪个课程 2016级软件工程 这个作业的要求在哪里 实验八 团队作业4:基于原型的团队项目需求调研与分析 团队名称 BUG创造队 作业学习目标 (1)体验以原型设计为基础的团队 ...

  5. (转)python自动化测试之异常及日志

    为了保持自动化测试用例的健壮性,异常的捕获及处理,日志的记录对掌握自动化测试执行情况尤为重要,这里便详细的介绍下在自动化测试中使用到的异常及日志,并介绍其详细的用法. 一.日志 打印日志是很多程序的重 ...

  6. 10. vue-router命名路由

    命名路由的配置规则 为了更加方便的表示路由的路径,可以给路由规则起一个别名, 即为"命名路由". const router = new VueRouter ({ routes: [ ...

  7. test20190925 老L

    100+0+0=100.概率题套路见的太少了,做题策略不是最优的. 排列 给出 n 个二元组,第 i 个二元组为(ai,bi). 将 n 个二元组按照一定顺序排成一列,可以得到一个排列.显然,这样的排 ...

  8. python 查询文件修改python lib 库文件

    运行code import os, time import sys import re def search(path, name): for root, dirs, files in os.walk ...

  9. junit4的初级用法

    junit4初级用法: 一:各个标签的意思 1.@Test用来标注测试函数 2.@Before用来标注此函数在每次测试函数运行之前运行(每执行一个@Test之前都要运行一遍@Before) 3.@Af ...

  10. 2019.12.09 java for循环

    for(初始化表达式; 循环条件; 操作表达式){     执行语句     ……… } 先走初始化表达式,再走循环条件,如条件满足,走执行语句,然后走操作表达式,再走循环条件,如条件满足,走执行语句 ...