题目:

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

    本文主要给大家简单介绍一下如何动态的在一个元素添加和删除div,希望能够得到举一反三之效. 代码实例如下: <!DOCTYPE html> <html> <head> ...

  2. 快速清理Visual Studio起始页最近打开项目

    清除vs2008起始页最近打开项目 第一种:最简单的方式: 把以下内容保存为.bat批处理文件 @echo off@REG Delete HKCU\Software\Microsoft\VisualS ...

  3. vs C#数据库导入EXCLE

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  4. IE中console的正确使用方法

    本文出处原文链接 转载请注明出处 http://www.cnblogs.com/havedream/p/4519538.html 问题来源:最近在学习easyui,观看的视频教程是孙宇的<EAS ...

  5. 封装js千分位加逗号和删除逗号

    //封装js千分位加逗号和删除逗号 alert( format(2545678754.020001) ) //2,545,678,754.03 alert( format(-2545678754.02 ...

  6. PHP 实现下载文件到本地

    只需要在php文件中设置请求头就可以了,创建download.php文件,代码如下: $fileName = $_GET['filename']; //得到文件名 header( "Cont ...

  7. input元素的padding border margin的区别

    padding内(不包含padding)的部分才是可输入部分,也是width和height标明的区域.padding的部分加上width和height部分是background的部分.padding的 ...

  8. APK反编译之一

    初步接触APK反编译.刚刚使用android-apktool软件反编译了一下QQ.apk,目的只是想看看QQ这个应用软件是内部是如何设计的,希望可以在某些方面借鉴一下.下面就如何反编译做一个简单的记录 ...

  9. linux内核设计与实现学习笔记-模块

    模块 1.概念:  如果让LINUX Kernel单独运行在一个保护区域,那么LINUX Kernel就成为了“单内核”.    LINUX Kernel是组件模式的,所谓组件模式是指:LINUX K ...

  10. 通过Maven搭建Mybatis项目

    学习通过maven工程搭建Mybatis工程开启对M ybaits的学习总结之旅. 1.首先创建Maven工程. 2.在pom.xml文件中加入依赖的jar <!-- mybatis核心包 -- ...