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. 关于java中word转html

    http://www.dewen.org/q/5820/java%E4%B8%AD%E6%80%8E%E4%B9%88%E5%B0%86word%E6%96%87%E6%A1%A3%E8%BD%ACh ...

  2. EDAS字体嵌入问题解决方法

    提交IEEE EDAS文章时出现:“The paper PDF file cannot be accepted: Publishers require that PDF fonts are embed ...

  3. 关于jquery.noConflict()的学习记录

    今天无意中看到了jquery.noConfict()的实现方法 代码如下: var // Map over jQuery in case of overwrite _jQuery = window.j ...

  4. NIO编程介绍

    代码: package bhz.nio; import java.io.IOException; import java.net.InetSocketAddress; import java.nio. ...

  5. leetcode703

    class KthLargest { public: KthLargest(int k, vector<int> nums) { size = k; for(auto num:nums){ ...

  6. xmlhttp的OnReadyStateChange事件

    这两天抽空升级了一下自己做的pon资料表,增加了OLT拓扑自动发现以及业务从MSE至OLT的全自动下发 等功能,让人没想到的是在处理xmlhttp回调时死活接收不到反馈信息, 在Excel论坛和微软官 ...

  7. 从邮件原理来看 postfix和docecot

    好早好早以前计算机网络老师就教了说,邮件嘛,就三个协议smtp,imap,pop3. smtp 用来发邮件,imap,pop3用来收邮件.噢?是么.难道没有发现这句话有非常多的漏洞,根本就不能说清楚这 ...

  8. 获取当前UnixTime的零点时间戳

    最近有个需求,开屏广告每天只出一次. 思路为如果出了开屏广告,则记录当前时间,下次来的时候,读取当前时间和上一次出开屏的时间. 算一下是不是在同一天即可. 我们的第一个想法是将上次开屏时间和当前时间归 ...

  9. Windows 安装 Maven 及 Eclipse 安装Maven插件

    环境说明: window 8.1 64bit Eclipse Version: Luna Release (4.4.0) Maven 3.0.5 Windows Maven 安装: 1.确保安装了JD ...

  10. labelme2COCO

    # -*- coding:utf-8 -*-# !/usr/bin/env python import argparseimport jsonimport matplotlib.pyplot as p ...