【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 ...
随机推荐
- Jquery入门之---------基本事件------------
Javascript有一个非常重要的功能,就是事件驱动. 当页面完成加载后,用户通过鼠标或键盘触发页面中绑定事件的元素即可触发.Jquery为开发者更有效率的编写事件行为,封装了大量有益的事件方法供我 ...
- Linux Hugetlbfs内核源码简析-----(二)Hugetlbfs挂载
本文只讨论执行"mount none /mnt/huge -t hugetlbfs"命令后,mount系统调用的执行过程(基于Linux-3.4.51),不涉及进程相关的细节. m ...
- 遇到困难 jsp代码onclick="javascript:return(checklogin());"报错
<script language="javascript"> function checklogin() { if (document.getElementById(& ...
- 《第一行代码--Android》阅读笔记之Activity
1.BaseActivity里面可以干什么 定义一个Context定义一个TAG 记录当前的Activity名字getClass().getSimpleName(); 2.Activity里面的几个重 ...
- php中empty(), is_null(), isset()函数区别
empty(), is_null(), isset()真值表(区别) 我们先来看看这3个函数的功能描述 www.111cn.net isset 判断变量是否已存在,如果变量存在则返回 TRUE,否则返 ...
- 通过Messenger与后台连接(单向操作,activity向service发送数据)
xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:t ...
- How to setup and process Intercompany accounting [AX2012]
In this post, I will take you through a very simple functionality called the intercompany accountin ...
- 第十五章 调试及安全性(In .net4.5) 之 管理程序集
1. 概述 本章将介绍 什么是程序集.如何强命名程序集.如何把程序集放入GAC.程序集版本 以及 WinMD程序集. 2. 主要内容 2.1 什么是程序集 程序集(Assembly)概念的出现,是为了 ...
- 【C#】 装箱 (boxing) 和拆箱 (unboxing)
目录: 1. 装箱和拆箱 2. 深入理解装箱和拆箱 3. int[] to object[],值类型数组到对象数组的转化 4. 使用泛型减少装箱和拆箱 1. 装箱和拆箱 装箱 就是把“值类型”转换成 ...
- NOJ1018-深度遍历二叉树
题目要求很简单,前中后序遍历一棵二叉树.坑爹的是这道题的输入数据和测试数据压根不一样,找了好久原因,去讨论区看见有别人发的测试样例,修改了一下就AC了 测试样例是这个:DEH##FJ##G#CK### ...