Permutations II 再分析
记得第一遍做这题的时候其实是没什么思路的,但是第二次的时候,我已经有"结果空间树"的概念了。这时候再看https://oj.leetcode.com/problems/permutations-ii/,觉得那样理解未免过于繁,因此重新写下新的思路。以前的文章总是花了时间写,删了可惜,且留着吧。(废话太多了)。
怎么理解结果空间树?当我们用dfs去寻找结果的时候,实际上是构造了一棵树。我们通常会有如下的代码结构:
void dfs()
{
if(expression1)
do something
for(int i=0;i<N;i++)
{
if(expression2)
do something
dfs();
}
}
expression1通常是终止条件。expression2通常是剪枝条件。
以本题为例,我最终代码是:
class Solution {
public:
vector<vector<int>> res;
vector<vector<int> > permuteUnique(vector<int> &num) {
sort(num.begin(),num.end());
bool *visits=new bool[num.size()];
memset(visits,0,sizeof(bool)*num.size());
vector<int> intermediate;
dfs(num,intermediate,visits);
return res;
}
void dfs(vector<int>& num,vector<int> &intermediate, bool* visits)
{
if(intermediate.size()==num.size())
res.push_back(intermediate);
for(int i=0;i<num.size();i++)
{
if(visits[i]||(i>0&&num[i]==num[i-1]&&!visits[i-1]))
continue;
visits[i]=true;
intermediate.push_back(num[i]);
dfs(num,intermediate,visits);
intermediate.pop_back();
visits[i]=false;
}
}
};
假设输入如下:[1,1,1,1,2],则树形状如下:

红色叉的部分表示剪枝。从上述图来看,或者称为森林还恰当一些,先不管这个。这里关键要理解一点,如何去重?比如第三层的后面两个1是如何去掉的?关键的代码在于这里:
i>0&&num[i]==num[i-1]&&!visits[i-1]
也就是说,要满足以下两个条件:1. 与前一个相等;2. 前一个没有被访问。这样就能保证相同的元素按顺序每层只访问一个。
说白了,实在没什么高难度的东西。
Permutations II 再分析的更多相关文章
- LeetCode:Permutations, Permutations II(求全排列)
Permutations Given a collection of numbers, return all possible permutations. For example, [1,2,3] h ...
- LeetCode46,47 Permutations, Permutations II
题目: LeetCode46 I Given a collection of distinct numbers, return all possible permutations. (Medium) ...
- 【LeetCode】47. Permutations II
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- LeetCode: Permutations II 解题报告
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- LeetCode解题报告—— Permutations & Permutations II & Rotate Image
1. Permutations Given a collection of distinct numbers, return all possible permutations. For exampl ...
- leetCode 47.Permutations II (排列组合II) 解题思路和方法
Permutations II Given a collection of numbers that might contain duplicates, return all possible un ...
- Leetcode之回溯法专题-47. 全排列 II(Permutations II)
Leetcode之回溯法专题-47. 全排列 II(Permutations II) 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2] ...
- 回溯---Permutations II
47.Permutations II (Medium)](https://leetcode.com/problems/permutations-ii/description/) [1,1,2] hav ...
- 【leetcode】Permutations II
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
随机推荐
- cocos2dx解决苹果正版ipv6的问题
苹果官方出了新的规定,要求新上架的app都必须单独支持ipv6-only的网络. 具体的要求链接:https://developer.apple.com/library/mac/documentati ...
- JQuery_元素属性操作
除了对元素内容进行设置和获取,通过jQuery 也可以对元素本身的属性进行操作,包括获取属性的属性值.设置属性的属性值,并且可以删除掉属性. <script type="text/ja ...
- 【java基础】 合并两个类型相同的list
将两个类型相同的list合并,可以用 addAll(Collection<? extends E> c) import java.util.ArrayList; import java.u ...
- 【java基础】面向对象的三大特征---多态
java的引用变量有两种类型,编译时类型和运行时类型,如果编译时类型和运行时类型不一致,就会出现多态. 多态分为: 运行时多态 java运行时系统根据调用该方法的实例的类型来决定选择调用哪个方法则被称 ...
- TestNG中的数据源DataProvider概述
http://blog.csdn.net/taiyangdao/article/details/52141711 在上一篇文章中,我们知道带参数的测试方法,可以在testng.xml配置文件中为其参数 ...
- Gevent中信号量的使用
greenlet间同步方法:信号量 1.为什么引入信号量: 2.gevent信号量有哪些: 3.编程实现. 为何引入信号量 信号量是一个允许Greenlet相互合作,限制并发访问或运行的低层次的同步原 ...
- jquery TypeError: $(...).live is not a functio,动态添加class的点击事件处理
jq版本更新后无live函数的处理.TypeError: $(...).live is not a function jquery live函数语法 jquery版本更新, 发现一个问题: jq自带的 ...
- 数据库 MySQL安装图解
MySQL安装图解 一.MYSQL的安装 1.打开下载的mysql安装文件,双击运行mysql-5.5.40-win32.msi. 2.选择安装类型,有"Typical(默认)". ...
- 关于如何使用Identity的文献
有几篇文件,深入浅出地讲解了如何一步一步的使用Identity,感觉十分有用,留下链接,备查. 1. Configuring Db Connection and Code-First Migratio ...
- ID3、C4.5、CART、RandomForest的原理
决策树意义: 分类决策树模型是表示基于特征对实例进行分类的树形结构.决策树可以转换为一个if_then规则的集合,也可以看作是定义在特征空间划分上的类的条件概率分布. 它着眼于从一组无次序.无规则的样 ...