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. Please umount the filesystem and rectify the problem(s)

    1.输入命令:ls -l /dev/mapper 2.再输入:xfs_repair /dev/dm-0 3.输入命令:xfs_repair -L /dev/dm-0 4.最后进行重启:init 6 等 ...

  2. 项目Beta冲刺(团队) --1/7

    课程名称:软件工程1916|W(福州大学) 作业要求:项目Beta冲刺) 团队名称:葫芦娃队 作业目标:尽力完成 团队博客 队员学号 队员昵称 博客地址 041602421 der himmel ht ...

  3. 深度学习Keras框架笔记之Dense类(标准的一维全连接层)

    深度学习Keras框架笔记之Dense类(标准的一维全连接层) 例: keras.layers.core.Dense(output_dim,init='glorot_uniform', activat ...

  4. idea常用设置汇总

    https://www.cnblogs.com/wangmingshun/p/6427088.html

  5. Python的私有变量与装饰器@property的用法

    Python的私有变量是在变量前面加上双横杠(例如:__test)来标识, Python私有变量只能在类内部使用,不被外部调用,且当变量被标记为私有后,调用时需再变量的前端插入类名,在类名前添加一个下 ...

  6. C语言 define实现的宏函数汇总

    最大值,最小值 #define MAX( x, y ) ( (x) > (y) ? (x) : (y) )#define MIN( x, y ) ( (x) < (y) ? (x) : ( ...

  7. repo源

    [sdata-base] name=Base baseurl=http://xx.xx.xx.xx:4507/repo/$releasever/$basearch/base enabled=1 gpg ...

  8. 开源项目 07 AutoMapper

    using AutoMapper; using Newtonsoft.Json; using System; using System.Collections.Generic; using Syste ...

  9. 1-移远GSM/GPRS M26 模块 Mini板 开发板(使用说明)

    板子预览 引脚说明 供电 关于串口电压匹配引脚: 上面一版朋友测试反应的问题 (上面的内容不删除,因为已经出售了1套) 1,源码开发完以后,烧录完成 PWRKEY按键不能使用了,需要断电上电,那么就需 ...

  10. 农场派对(party)(信息学奥赛一本通 1497)

    [题目描述] N(1≤N≤1000)头牛要去参加一场在编号为 x(1≤x≤N) 的牛的农场举行的派对.有 M(1≤M≤100000) 条有向道路,每条路长 Ti(1≤Ti≤100):每头牛都必须参加完 ...