[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的思想。
首先需要遍历输入数组,获取一共有多少种不同的数字,每个数字有多少个。 最简单的方法,排序后统计。
然后就是对应位置填数字的游戏了,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的更多相关文章
- LeetCode题解 Permutations II 和 Permutations I ——回溯算法
这个算法感觉还是很陌生的.算法导论里没有讲这个算法,而数据结构与算法分析只用了一节来阐述.我居然跳过去了..尴尬. 笨方法解决的: 第一题: 给定一个元素不重复的数组,枚举出他们的全排列. 方法1:递 ...
- 【leetcode】Permutations II
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- Java for LeetCode 047 Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- leetCode 47.Permutations II (排列组合II) 解题思路和方法
Permutations II Given a collection of numbers that might contain duplicates, return all possible un ...
- [LeetCode] 47. Permutations II 全排列 II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- LeetCode 047 Permutations II
题目要求:Permutations II Given a collection of numbers that might contain duplicates, return all possibl ...
- [LeetCode 题解]: Permutations
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- [LeetCode] 47. Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [leetcode] 47. Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
随机推荐
- BIO模型
基本模型 代码: 客户端 package bhz.bio; import java.io.BufferedReader; import java.io.IOException; import java ...
- Shiro的校验Session是否过期处理的过程
首先开启定时扫描活跃的session进行校验 <!-- shiro会话管理 --> <!-- 即用户登录后就是一次会话,在没有退出之前,它的所有信息都在会话中:会话可以是普通 Jav ...
- a.call(b); call 方法
a.call(b); a.apply(b,[]) function class1() { this.name = function(){ alert("class1的方法name()&quo ...
- mybatis 用法分享
主题 这篇文章主要是记录这2个月我对mybatis的学习以后的一些感触和一些如果我是架构师会怎么在项目里使用mybatis的一些大胆的想法. 感想 1.首先根据之前的学习我已经知道了mybatis g ...
- shell编程——日志输出的同时显屏
在执行脚本的时候我们常常需要将执行过程全部输出到日志里,以备出现报错时可以跟踪分析,开始我用的是exec: exec 1>info.log #把全部执行过程输出到info日志中 exec 2&g ...
- VUE+WebPack游戏开发:神庙逃亡的游戏设计
- Nginx源码完全注释(4)ngx_queue.h / ngx_queue.c
队列头文件ngx_queue.h #include <ngx_config.h> #include <ngx_core.h> #ifndef _NGX_QUEUE_H_INCL ...
- 71. Simplify Path (Stack)
Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", ...
- cocos2d-js反射
如何在android平台上使用js直接调用Java方法 在cocos2d-js 3.0beta中加入了一个新特性,在android平台上我们可以通过反射直接在js中调用java的静态方法.它的使用方法 ...
- style多次设置行内样式
语法 style="font-size:32px;background-color:#aaa"