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. Django-2的路由层(URLconf)

    URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表:你就是以这种方式告诉Django,对于客户端发来的某个URL调用哪一段逻辑代码 ...

  2. Halcon算子之shape_trans,用于变换区域的形状

    函数原型:shape_trans(Region : RegionTrans : Type : ) *shape_trans*仍然是区域,smallest_rectangle1可以获得四个角的坐标 函数 ...

  3. 转载----开发运维资产管理系统cmdb一些观点扯淡

    在新公司负责全网的自动化运维平台及给各个业务线提供接口数据.这工作和以前做的很类似,也算是比较顺手的工作,这段时候遇见一些问题,导致开发的前进速度的放慢了,具体有哪些的不完善,我这里就先不摆出了,但是 ...

  4. 解决编译错误 implicit declaration of function 'strptime'

    根据man手册,在文件中加上以下定义,应该可以去处该warning        #define _XOPEN_SOURCE /* glibc2 needs this */        #inclu ...

  5. android学习-Eclipse中修改Android项目图标

    参考原文:http://blog.csdn.net/wpwbb510582246/article/details/52556753 方法一:替换res文件夹下的ic_launcher-web.png图 ...

  6. Linux 入门知识一(附上如何解决Ubuntu的root密码问题)

    .centos有拥有七个控制台,其中第一到第六个是字符界面,第七个是图形界面 切换的快捷键是ctrl+shift+fn(n为自然数)   输入tty的话,可以检查当前处于哪个控制台   如何在cent ...

  7. tnsping命令解析

    tnsping命令格式: tnsping <service_name> n n的意义是可以让tnsping ping多次 例: c:\Documents and Settings\Tony ...

  8. 前端开发之CSS入门篇

    一.CSS介绍和语法 二.CSS引入方式 三.基本选择器 四.高级选择器 五.伪类选择器 六.伪元素选择器 1️⃣  CSS介绍和语法 1. CSS的介绍 (1)为什么需要CSS? 使用css的目的就 ...

  9. ADB Not Responding - Android Studio

    问题描述: 最近安装了Android Studio v1.0,运行的时候老是这个错误 解决方案: 网上有人说是已经有adb的进程在运行,可是打开任务管理器,找不到对应的adb 进程. 无奈之下,想到a ...

  10. 117 FP页面无法查看 此错误是JDK8.0.0.0版本的一个BUG,会导致工单重复回写,

    用户表示117 FP页面无法查看,提示如下错误: 跟进: 1.进入FP服务器可看到以下错误 这个错误的框就表示FP的一个进程报错,自动断掉了,需要重新跑一次EXIT,INIT,PLAN,EXPORT, ...