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. jquery 表单元素选择器

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content ...

  2. 项目笔记---WPF之Metro风格UI(转)

    写在前面 作为新年开篇的文章,当然要选择比较“Cool”的东西来分享,这自然落到了WPF身上,WPF技术自身可塑性非常强,其强大的绘图技术以及XAML技术比WinForm而言有本质的飞跃. 切入正题, ...

  3. 10. vue-router命名路由

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

  4. JavaScript——判断页面是否加载完成

    前言 接上文,既然你是做一个loading的效果,你总不能一直loading,当页面完成加载的时候你总要结束吧 步骤 先说下原生的方法,再讲jquery的方法,原理是一样的 JavaScript // ...

  5. 讨论---MySQL数据库中null、''、' '区别

    今天在写代码的时候,遇到了一个关于MySQL数据库的问题,发现在表中插入‘’数据的时候插入不进去.发现这张表中的数据没有数据的时候,默认显示的null,当时自己又重复的额试了几次,但是始终没有成功,从 ...

  6. keras中to_categorical()函数解析

    from keras.utils.np_utils import * # 类别向量定义 b = [0, 1, 2, 3, 4, 5, 6, 7, 8] # 调用to_categorical将b按照9个 ...

  7. UVa10828 Back to Kernighan-Ritchie——概率转移&&高斯消元法

    题意 给出一个程序控制流图,从每个结点出发到每个后继接结点的概率均相等.当执行完一个没有后继的结点后,整个程序终止.程序总是从编号为1的结点开始.你的任务是对于若干个查询结点,求出每个结点的期望执行次 ...

  8. [Algorithm] BFS vs DFS

    //If you know a solution is not far from the root of the tree: BFS, because it is faster to get clos ...

  9. LOJ P10015 扩散 题解

    每日一题 day49 打卡 Analysis 用dis数组记录每两个点之间的时间,再用一个传递闭包来维护最小的时间就好了 #include<iostream> #include<cs ...

  10. 洛谷 题解 P2721 【摄像头】

    这是我见过最水的蓝题 这不就是拓扑排序板子题吗 题目大意:松鼠砸烂摄像头不被抓住 摄像头一个可以监视到另一个可以看做有向边,用邻接链表储存就好了,我也不知道邻接矩阵到底能不能过保险起见还是用邻接链表. ...