给定一个可包含重复数字的序列,返回所有不重复的全排列。

示例:

输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ]

在全排列1题目的基础上先排序,目的是把相同的元素聚在一起,然后在递归完一个数时,把后面与他相同的数给过滤掉,防止重复

class Solution {
public:
vector<vector<int> >res;
vector<int> visit;
int size;
vector<vector<int> > permuteUnique(vector<int>& nums)
{
int len = nums.size();
if(len == 0)
return res;
sort(nums.begin(), nums.end());
size = len;
visit = vector<int>(len, 0);
vector<int> temp;
DFS(nums, temp, 0);
return res;
} void DFS(vector<int>& nums, vector<int> &temp, int len)
{
if(len == size)
{
res.push_back(temp);
}
for(int i = 0; i < size; i++)
{
if(visit[i] == 1)
continue;
visit[i] = 1;
temp.push_back(nums[i]);
DFS(nums, temp, len + 1);
visit[i] = 0;
temp.pop_back();
for(; i < size - 1; i++)
{
if(nums[i] == nums[i + 1])
continue;
else
break;
}
}
}
};

Leetcode47. Permutations II全排列2的更多相关文章

  1. leetcode47. Permutations II

    leetcode47. Permutations II 题意: 给定可能包含重复的数字的集合,返回所有可能的唯一排列. 思路: 这题全排列两种写法. 用hash表记录已经visit的数字,虽然看起来很 ...

  2. [LeetCode] Permutations II 全排列之二

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

  3. [LeetCode] 47. 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 permutations ...

  5. LeetCode47.Permutations II(剑指offer38-1)

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

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

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

  7. permutations II(全排列 2)

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

  8. 47. Permutations II (全排列有重复的元素)

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

  9. leetcode Permutations II 无重全排列

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...

随机推荐

  1. 转:链表相交有环 经典面试题(三)附答案 算法+数据结构+代码 微软Microsoft、谷歌Google、百度、腾讯

    源地址:http://blog.csdn.net/sj13051180/article/details/6754228 1.判断单链表是否有环,要求空间尽量少(2011年MTK) 如何找出环的连接点在 ...

  2. PAT甲级——A1004 Counting Leaves

    A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...

  3. 07_springmvc校验

    一.概述 项目中,通常使用较多是前端的校验,比如页面中js校验.对于安全要求较高点建议在服务端进行校验. 服务端校验: 控制层conroller:校验页面请求的参数的合法性.在服务端控制层conrol ...

  4. 02_jQuery对象初识(二)筛选器1

    0. HTML对象和jQuery对象的区别: 1.jQuery对象转换成DOM对象,用索引取出具体的标签 2.DOM对象转换成jQuery对象,$(DOM对象) 注意:jQuery对象保存到变量的时候 ...

  5. 10张图带你深入理解Docker容器和镜像-转

    转载:http://dockone.io/article/783 这篇文章希望能够帮助读者深入理解Docker的命令,还有容器(container)和镜像(image)之间的区别,并深入探讨容器和运行 ...

  6. mvvmlight下passwordBox绑定的解决方法

    在做公司的项目中,用到MVVMLight框架,需要做登陆注册的功能模块,但是在wpf中passwordBox没有内置绑定功能,因为出于安全的考虑.但是,博主又非常想要契合mvvm思想,一定要passw ...

  7. jquery刷新局部和全页的方法

    一.全页面刷新方法: window.location.reload()刷新当前页面. parent.location.reload()刷新父亲对象(用于框架) opener.location.relo ...

  8. WPF MVVM模式不用Prism

    上一个例子使用了Prism.这个例子不用Prism.用自己封装的库LiuxhCSDLL,其实也差不多. 一.程序结构 二.界面代码以及界面效果 <Window x:Class="WPF ...

  9. WPF 导出Excel 导出图片

    /// <summary> /// 导出Excel /// </summary> private void ExportExcel(DataTable ExcelDt) { / ...

  10. LUOGU P4113 [HEOI2012]采花

    传送门 解题思路 莫队题卡莫队...莫队只能拿到100分,满分200.正解主席树??发个莫队100分代码. 代码 #include<iostream> #include<cstdio ...