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 ...
随机推荐
- TFS二次开发系列:六、TFS的版本控制
在TFS中对于版本控制是在WorkSpace工作区来控制的. 首先我们先整理WorkSpace的一些基本使用方法. CheckIn:迁入挂起的操作 CreateMapping:创建一个本地映射地址 D ...
- validate插件深入学习-01 小白从看透一个插件开始
没有编程基础的的我,即使看了一遍jq文档也不知道怎么写程序,一个新的插件看了也不知道怎么用. 总是想做自己会的,自己不会的永远不去触碰,就永远不会. 都说编程这东西,很多都有很像的地方了,一个语言学通 ...
- vim添加未识别文件类型
这里用.c的文件格式来识别.nc文件 $ cd ~/.vim/ftdetect $ vim nc.vim # nc.vim内容 # au BufRead,BufNewFilE *.nc set fil ...
- DOM常用操作总结
一.getElementById() 寻找一个有着给定 id 属性值的元素,返回值是一个有着给定 id 属性值的元素节点.如果不存在,这样的元素,它返回 null. 二.getElementsByNa ...
- angular 路由请求js文件
<script type="text/javascript" src="http://apps.bdimg.com/libs/angular.js/1.3.2/an ...
- /etc/ethers【地址映射】
该文件存放硬件地址和 IP 地址的映射关系. 格式如下: 00-00-00-00-00-00 0:0:0:0 每一行代表一个 IP 地址.
- 软件工程线上课程(C语言实践篇)学习心得总结
林牧 + 原创作品转载请注明出处 + <软件工程(C编码实践篇)>MOOC课程http://mooc.study.163.com/course/USTC-1000002006 软件工程的理 ...
- 里面的div怎么撑开外面的div,让高度自适应
关于容器高度自适应的兼容性问题.1.有些时候,我们希望容器有一个固定高度,但当其中的内容多的时候,又希望高度能够自适应,也即容器在纵向能被撑开,且如果有背景,也能够自适应.在一般情况下,使用min-h ...
- pc端页面在移动端显示问题
1.pc端页面在移动端显示,默认视口宽度是980px(也就是body宽度是980px),可通过meta标签设置为需要的尺寸,比如页面中元素最大宽度是1220px,则如下所示 <meta name ...
- Java_ToolKit用法
转自:http://blog.sina.com.cn/s/blog_9e4556250100z5kv.html 此类是所有 Abstract Window Toolkit 实际实现的抽象超类.Tool ...