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的思想。

首先需要遍历输入数组,获取一共有多少种不同的数字,每个数字有多少个。 最简单的方法,排序后统计。

然后就是对应位置填数字的游戏了,DFS即可。

 class Solution {
public:
vector<vector<int> > vi;
vector<int> types; // 数字缓存数组
vector<int> counts; // 数字计数器数组
vector<int> seq; // 打印数组 void generatePermute(int len)
{
if(len==)
{
vi.push_back(seq);
return;
}
for(int i=;i<types.size();i++)
{
if((counts[i])>)
{
counts[i]--;
seq[len-]=types[i]; //第len-1 是否存放types[i]
generatePermute(len-);
counts[i]++;
}
}
} vector< vector<int> > permuteUnique(vector<int> &num) { if(num.size()==) return vi;
sort(num.begin(),num.end()); vi.clear(); //结果数组初始化
types.clear(); //数字缓存数组初始化
counts.clear(); //计数器初始化
seq.resize(num.size()); //输出数组大小设定 int j=;
types.push_back(num[]);
counts.push_back(); for(int i=;i<num.size();i++)
{
if(num[i]==types.back())
{
counts.back()++;
}
else
{
types.push_back(num[i]);
counts.push_back();
}
}
generatePermute(num.size());
return vi;
}
};

转载请注明出处: http://www.cnblogs.com/double-win/ 谢谢!

[LeetCode 题解]: Permutations II的更多相关文章

  1. LeetCode题解 Permutations II 和 Permutations I ——回溯算法

    这个算法感觉还是很陌生的.算法导论里没有讲这个算法,而数据结构与算法分析只用了一节来阐述.我居然跳过去了..尴尬. 笨方法解决的: 第一题: 给定一个元素不重复的数组,枚举出他们的全排列. 方法1:递 ...

  2. 【leetcode】Permutations II

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

  3. Java for LeetCode 047 Permutations II

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

  4. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

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

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

  6. [LeetCode] 47. Permutations II 全排列 II

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

  7. LeetCode 047 Permutations II

    题目要求:Permutations II Given a collection of numbers that might contain duplicates, return all possibl ...

  8. [LeetCode 题解]: Permutations

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

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

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

  10. [leetcode] 47. Permutations II

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

随机推荐

  1. 异步编程之Generator(2)——剖析特性

    异步编程系列教程: (翻译)异步编程之Promise(1)--初见魅力 异步编程之Promise(2):探究原理 异步编程之Promise(3):拓展进阶 异步编程之Generator(1)--领略魅 ...

  2. Django xadmin的使用 (二)

    上一篇中我们基本完成了xadmin的配置,但是要进行正式使用还需要更多细致的配置. 1.页面显示中文 settings.py中配置(这个和django自带的admin配置一样) # LANGUAGE_ ...

  3. Mysql Replication 主从同步

    简介: Mysql 的主从同步功能,这种解决方案是企业很常见的一种.常用于备份数据库,当客户端操作主库时,主库会产生binlog日志文件, 从库通过复制主库的binlog日志文件,然后解析成相应的 S ...

  4. windows下Mysql5.7.10免安装版配置

    免安装配置: 在环境变量 Path 中追加 %mysql_home%\bin; 配置mysql目录下的 my-default.ini 文件,在mysql 根目录下新建 data 文件夹 使用管理员权限 ...

  5. 【314】putty 自动登录

    putty是一款好用的远程登录linux服务器软件,但每次输入用户名密码毕竟有些烦人,这里教你免用户名密码登陆. 本教程通过 *.bat 文件进行添加参数,下面为相应的代码: 方法一:(直接将密码/用 ...

  6. unit_2_homework

    随记2018/4/23 # 找元祖中的元素,移除每个元素的空格,并查找以a或A开头,c结尾的所有元素. # 思路:将i取出来,求得li列表中有多少个元素for i in range(len(li)): ...

  7. ssh框架,工具类调用service层方法

    解决方法: @Component//声明为spring组件 public class CopyFileUtil{ @Autowired private DataFileManager dataFile ...

  8. git远程代码库回滚(webstorm下)

    git远程代码库回滚(webstorm下) 1. 场景 添加了一个文件[file-for-test.js]到git的控制下 进行了三次修改,并分别进行了三次commit,最后进行了一次push git ...

  9. ReentrantLock(重入锁)功能详解和应用演示

    目录 1. ReentrantLock简介 2. ReentrantLock和synchronized的相同点 2.1 ReentrantLock是独占锁且可重入的 3. ReentrantLock相 ...

  10. 【LA2957 训练指南】运送超级计算机【二分,最大流】

    题意: 宇宙中有n个星球,你的任务是用最短的时间把k个超级计算机从星球S运送到星球T.每个超级计算机需要一整艘飞船来运输.行星之间有m条双向隧道,每条隧道需要一整天的时间来通过,且不能有两艘飞船同时使 ...