[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 ...
随机推荐
- Nginx服务优化
1.1Nginx.conf配置文件基本参数优化 1.1.1 隐藏nginx header内版本号信息 一些特定的系统及服务漏洞一般都和特定的软件及版本号有关,我们应尽量隐藏服务器的敏感信息(软件名称 ...
- centos7.3查看时区
[root@iZ2ze3gf6h0zndx5dxyhqiZ ~]# date -R Fri, Apr :: + 即可查看时区 我们国家的东八区(+0800) 查看clock系统配置文件 [root@i ...
- ADDCOMPONENT之后立即(同步)调用AWAKE,但START却是所有AWAKE完成后才调用 的(异步)
addcomponent 等价于 instance了一个组件,完成后立即从该点调用awake(). 等到游戏中所有组件都被awake()之后,start()才会被调用.示例如下: class MyTe ...
- Apache Spark介绍及集群搭建
简介 Spark是一个针对于大规模数据处理的统一分析引擎.其处理速度比MapReduce快很多.其特征有: 1.速度快 spark比mapreduce在内存中快100x,比mapreduce在磁盘中快 ...
- 1.在VC编译器下面为什么每个头文件以及源文件都要包含“stdAfx.h”,那么stdAfx.h中到底存放了什么,用来做什么?
我们知道在windows平台下面很多的文件后缀名中都含有Afx,其实Afx是微软公司的一个技术研发团队名称,vc下的“stdAfx.h”和“stdAfx.cpp”文件就是有他们所研发出来的,为什么要这 ...
- 2018年最新 Java面试通关要点汇总集
基础篇 基本功 面向对象的特征 final, finally, finalize 的区别 int 和 Integer 有什么区别 重载和重写的区别 抽象类和接口有什么区别 说说反射的用途及实现 说说自 ...
- jquery删除记录弹出提示框
来自于<jquery权威指南> ------------------- 点击删除时,弹出提示框,并做相应的删除确定或取消 完整代码如下: <!DOCTYPE html PUBLIC ...
- 提高你的Python能力:理解单元测试
对于程序开发新手来说,一个最常见的困惑是测试的主题.他们隐约觉得“单元测试”是很好的,而且他们也应该做单元测试.但他们却不懂这个词的真正含义.如果这听起来像是在说你,不要怕!在这篇文章中,我将介绍什么 ...
- Header add Access-Control-Allow-Origin: *
允许所有域名跨域 Header add Access-Control-Allow-Origin: *
- 搭建自己的MQTT服务器
搭建自己的MQTT服务器 物联网电子世界 百家号08-2903:04 MQTT协议是广泛应用的物联网协议,使用测试MQTT协议需要MQTT的代理.有两种方法使用MQTT服务,一是租用现成的MQTT服务 ...