题目:

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].

代码:

class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int> > ret;
vector<bool> used(nums.size(), false);
vector<int> tmp;
Solution::perpermuteUnique(ret, nums, tmp, used);
return ret;
}
static void perpermuteUnique(
vector<vector<int> >& ret,
vector<int>& nums,
vector<int>& tmp,
vector<bool>& used)
{
if ( tmp.size()==nums.size() )
{
ret.push_back(tmp);
return;
}
map<int, bool> valueUsed;
for ( int i = ; i < nums.size(); ++i )
{
if ( used[i] || ( valueUsed.find(nums[i])!=valueUsed.end() && valueUsed[nums[i]] ) ) continue;
tmp.push_back(nums[i]);
used[i] = true;
valueUsed[nums[i]] = true;
Solution::perpermuteUnique(ret, nums, tmp, used);
tmp.pop_back();
used[i] = false;
}
}
};

tips:

采用dfs的解法,答题思路与Permutations相同(http://www.cnblogs.com/xbf9xbf/p/4519100.html

这里的去重的思路就是:重复的值可以出现在排列的不同位置,但是相同的位置上不能出现重复的值。

具体去重的做法是:每一层维护一个map<int, bool>记录在该层,某个值是否被使用了。

=======================================

第二次过这道题,沿用dfs的思路。

class Solution {
public:
vector<vector<int> > permuteUnique(vector<int>& nums)
{
vector<vector<int> > ret;
vector<int> tmp;
vector<bool> used(nums.size(), false);
Solution::dfs(ret, nums, used, tmp);
return ret;
}
static void dfs(
vector<vector<int> >& ret,
vector<int>& nums,
vector<bool>& used,
vector<int>& tmp)
{
if ( tmp.size()==nums.size() )
{
ret.push_back(tmp);
return;
}
map<int, bool> valueUsed;
for ( int i=; i<nums.size(); ++i )
{
if ( used[i] || (valueUsed.find(nums[i])!=valueUsed.end() && valueUsed[nums[i]]) )
continue;
tmp.push_back(nums[i]);
used[i] = !used[i];
Solution::dfs(ret, nums, used, tmp);
tmp.pop_back();
used[i] = !used[i];
valueUsed[nums[i]] = true;
}
}
};

【Permutations II】cpp的更多相关文章

  1. 【N-Quens II】cpp

    题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...

  2. 【Subsets II】cpp

    题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...

  3. LeetCode 【47. Permutations II】

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

  4. 【Word Break II】cpp

    题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...

  5. 【Unique Paths II】cpp

    题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...

  6. 【Path Sum II】cpp

    题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...

  7. 【Unique Binary Search Trees II】cpp

    题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...

  8. 【Populating Next Right Pointers in Each Node II】cpp

    题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given ...

  9. 【Binary Tree Level Order Traversal II 】cpp

    题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...

随机推荐

  1. jQuery实现的Div窗口震动效果实例

    本文实例讲述了jQuery实现的Div窗口震动效果.分享给大家供大家参考.具体如下: 这是一款jQuery窗口震动效果代码,在Div边框内点击一下鼠标,它就开始震动了,适用浏览器:IE8.360.Fi ...

  2. 为什么使用 Redis及其产品定位 (转载自http://www.infoq.com/cn/articles/tq-why-choose-redis)

    传统MySQL+Memcached架构遇到的问题 实际MySQL 是适合进行海量存储的,通过Memcached将热点数据加载到cache,加速访问,很多公司都曾经使用过这样的架构,但随着业务数据量的不 ...

  3. MarkDown写作技巧

    前言 年轻的我们往往苦恼于没有充实的社会经历.社会阅历,很难有较强的个人能力.个人魅力!就个人能力而言,本 人主要把它分为两种:“吸收能力”和“输出能力”.吸收能力主要体现了个人对外界知识的摄入能力, ...

  4. 共享内存shared pool (4):Library cache 转储文件

    上一篇blog只是从概念上理解Library cache,本篇则是将Library cache从内存中dump出来,看看其结构. 基本命令 ALTER SESSION SET EVENTS 'imme ...

  5. Python学习教程(learning Python)--3.2 if-else分支语句

    if-else分支语句结构的特点是当conditon条件满足时,执行if下的语句块,当condition条件不满足时执行else下的语句块,也就是说根据条件来控制让某些语句执行,某些语句不被执行. i ...

  6. WPF学习03:Element Binding

    元素绑定是数据绑定的一个子集,关于数据绑定是什么,园子里有很多文章都说得非常好,在此不予详细说明. WPF实现了完善的数据绑定机制,使得元素绑定有简易的实现步骤. 本文的元素指的是WPF中的可视控件. ...

  7. springMVC+jpa配置之简单案例

    搭建springMVC+jpa的亲身经历,看着网上的博客,自己摸索着搭建框架结果错误一大堆.现在把流程走一遍,方便以后查看. 其中我遇到这样的一个问题:直接启动tomcat运行保存实体能通过,但是通过 ...

  8. hdu 1113 Word Amalgamation

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1113 字符串简单题: stl水过 如下: #include<algorithm> #inc ...

  9. minihttp http://www.acme.com/software/mini_httpd/

    1.安装mini_httpd 1.1把下载的mini_httpd-1.19.tar.gz拷贝到根目录   1.2 解压tar -xvfzmini_httpd-1.19.tar.gz ,会在根目录产生一 ...

  10. 数组链表下标指针map list

    1.时间复杂度 (1)时间频度 一个算法执行所耗费的时间,从理论上是不能算出来的,必须上机运行测试才能知道.但我们不可能也没有必要对每个算法都上机测试,只需知道哪个算法花费的时间多,哪个算法花费的时间 ...