【Permutations II】cpp
题目:
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的更多相关文章
- 【N-Quens II】cpp
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
- 【Subsets II】cpp
题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
- LeetCode 【47. Permutations II】
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 【Word Break II】cpp
题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...
- 【Unique Paths II】cpp
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- 【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 ...
- 【Unique Binary Search Trees II】cpp
题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...
- 【Populating Next Right Pointers in Each Node II】cpp
题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given ...
- 【Binary Tree Level Order Traversal II 】cpp
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
随机推荐
- Android 中断线程的处理
我现在对一个用户注册的功能1.用ProgressDialog将当前页面设成不可操作(保留返回键 退出ProgressDialog)2.用一个线程clientThread执行数据的提交和返回 问题:考虑 ...
- js对文章内容进行分页示例代码
这篇文章主要介绍了使用js对文章内容进行分页的具体实现,需要的朋友可以参考下 Thinkphp中文章显示代码: 代码如下: <div id="showContent"> ...
- ASP.NET中@Page指令中的AutoEventWireup
AutoEventWireup:指示控件的事件是否自动匹配 (Autowire).如果启用事件自动匹配,则为 true:否则为 false.默认值为 true.如果设为false,则事件不可用.有关更 ...
- PHP中数据库的连接
<?php //1.链接MySQL服务器 $conn = mysql_connect("localhost", "root" , 199452); //2 ...
- JSON,JSONP
http://blog.csdn.net/huaishuming/article/details/40046729 说明: 在做2个系统间传值时出现: 已阻止交叉源请求:同源策略不允许读取 http: ...
- HBase分布式安装
安装HBase之前需要先安装Hadoop,因为HBase是运行在Hadoop集群上的.安装Hadoop可以参照http://www.cnblogs.com/stGeekpower/p/3307289. ...
- Android中的显示单位
px (pixels)像素 一般HVGA代表320x480像素,这个用的比较多. dip或dp (device independent pixels)设备独立像素 这个和设备硬件有关,一般为了支持WV ...
- iPhone图标及启动画面大小 xcode5
启动画面 文件名 大小px Default.png 320*480 Default@2x.png 640*960 Default-568h@2x.png 640*1136 图标 文件名 大小px ...
- Python学习教程(learning Python)--1.2.2 Python格式化输出基础
本节讨论为何要格式化输出数据? 先看一段代码吧,本程序的功能是计算月支付金额. amount_due = 5000.0 #年支付金额 monthly_payment = amount_due / 12 ...
- 分布式缓存Memcached
分布式缓存服务器,既然用到数据缓存很明显就是想高效性的获取数据,大容量的存储数据.为了可以缓存大量的数据以及可以高效获取数据,那么分布式缓存数据库就要解决数据可以水平线性扩展,这样可以扩大数据容 ...