【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 ...
随机推荐
- mvc路由设置参数配置类似于url重写
1.新建的mvc项目中Global.asax 2.在另外一个控制器中的视图中 3. 4.
- .net框架
转载:http://www.cnblogs.com/JimmyZhang/archive/2012/11/27/2790759.html 本书是一本讲解.NET技术的书籍,目标读者群也是在.NET框架 ...
- 返回json格式时间,解析时间
传入:Json格式的时间 JS如下: yyyy-M(MM)-d(dd) H(HH):m(mm):s(ss) function timeStamp2String(time) { var data=tim ...
- MongoDB(3):小的细节问题
1.文档 {“greeting”:“hello,world”,“foo”: 3} 文档中的键/值对是有序的,下面的文档与上面的文档是完全不同的两个文档. {“foo”: 3 ,“greeting”:“ ...
- Java----代码优化篇
一.咱们之所以这么干的目的: 1.效率(最重要) 2.可读性,便于后期维护.(同样很重要) 二.代码优化的要求: 1.减小代码的体积. 2.提高代码的运行效率. 三.常用的代码的优化: 1.尽量重用对 ...
- form表单无刷新提交文件(iframe)
先看一段代码(PHP例子) 1.表单代码(form.html): <iframe name="testIframeName" style="display:none ...
- Ajax异步刷新局部页面的小李子
看到下面那幅图没有,我们要的是当点击确定以后,根据条形码搜索出商品信息,并且异步刷新右边页面:
- php static延迟静态绑定
如果你是一个懒惰的程序员,你看到以下代码可能会恼火 abstract class U{ } class u1 extends U{ public static function create(){ r ...
- Json 调用 天气API 实例
百度了一下,找了点别人的方法改进了一下. 获取天气网址:http://www.weather.com.cn/html/weather/101210701.shtml这里是温州的,当然其他城市自己搜索一 ...
- delphi 更改不了窗体的标题
delphi定义变量名千万要注意,不能和关键字同名,今天我无意间定义了一个caption的变量 导致我怎么都不能修改窗的标题.