记得第一遍做这题的时候其实是没什么思路的,但是第二次的时候,我已经有"结果空间树"的概念了。这时候再看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 再分析的更多相关文章

  1. LeetCode:Permutations, Permutations II(求全排列)

    Permutations Given a collection of numbers, return all possible permutations. For example, [1,2,3] h ...

  2. LeetCode46,47 Permutations, Permutations II

    题目: LeetCode46 I Given a collection of distinct numbers, return all possible permutations. (Medium) ...

  3. 【LeetCode】47. Permutations II

    Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...

  4. LeetCode: Permutations II 解题报告

    Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...

  5. LeetCode解题报告—— Permutations & Permutations II & Rotate Image

    1. Permutations Given a collection of distinct numbers, return all possible permutations. For exampl ...

  6. leetCode 47.Permutations II (排列组合II) 解题思路和方法

    Permutations II  Given a collection of numbers that might contain duplicates, return all possible un ...

  7. Leetcode之回溯法专题-47. 全排列 II(Permutations II)

    Leetcode之回溯法专题-47. 全排列 II(Permutations II) 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2] ...

  8. 回溯---Permutations II

    47.Permutations II (Medium)](https://leetcode.com/problems/permutations-ii/description/) [1,1,2] hav ...

  9. 【leetcode】Permutations II

    Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...

随机推荐

  1. Photoshop投影和CSS box-shadow转换

    "混合模式":Photoshop提供了各式各样的混合模式,但是CSS3阴影只支持正常模式(normal). "颜色(color)":阴影颜色.对应于CSS3阴影 ...

  2. ASP.NET 状态服务 及 session丢失问题解决方案总结

    ASP.NET2.0系统时,在程序中做删除或创建文件操作时,出现session丢失问题.采用了如下方法:1.asp.net Session的实现:asp.net的Session是基于HttpModul ...

  3. ajax提交表单+前端验证小示例

    <script src="http://css.jj.cn/js/jquery.js" type="text/javascript"></sc ...

  4. 【Java】Annotation_学习笔记

    Annotation 1.APT: 访问和处理Annotation的工具统称,即Annotation Process Tool. 2.java.lang下提供的五种基本Annotation: @Ove ...

  5. 后台启动weblogic成功后,在web浏览器上无法访问

    后台启动weblogic成功后,在web浏览器上无法访问,可尝试重启服务器.

  6. startActivity跳转失败而且没有异常信息

    startActivity跳转不能显示目标activity的布局(显示空白页),而且没有异常信息 onCreate()方法重写错误 应该重写的是onCreate(Bundle savedInstanc ...

  7. CSS3中的伪类选择器详解

      类选择器和伪类选择器区别 类选择器我们可以随意起名,而伪类选择器是CSS中已经定义好的选择器,不可以随意起名. 伪类选择器以及伪元素 我们把它放到这里 p.aaas{ text-align: le ...

  8. cordova iOS blank iframe iphone iframe 白屏 ios iframe 白屏

    (1)解决方案 http://stackoverflow.com/questions/36572537/cordova-ios-blank-iframe/36587026 在 index.html中配 ...

  9. link和@import导入css文件的区别

    (二者的区别其实是基础问题,但由于本人经常会忽略掉使用@import导入css文件这种方式,所以记录下来增加印象^^) 首先二者的引入方式: link:<link rel="style ...

  10. 安装linux操作系统

    安装双操作系统; 1 0. 介绍: 1 1 实验环境: 2 2. 实验准备: 2 3.开始安装: 2 1 制作U盘启动工具: 2 2.安装LinuxOS. 3 2.1在windowOS中划分60G空间 ...