记得第一遍做这题的时候其实是没什么思路的,但是第二次的时候,我已经有"结果空间树"的概念了。这时候再看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. ASP.NET MVC bootstrap 3 ie 8兼容问题及错误解决(取消IE禁用IE脚本调试定位js文件错误)

    因要做一个B/S架构的项目,使用MVC框架技术,本人不擅长页面设计美工,只好用bootstrap框架,在chrome内核系列的浏览器和IE 11中显示都没有问题,但是在 IE 8下显示却不正常,表格无 ...

  2. eclipse加载maven工程提示pom.xml无法解析org.apache.maven.plugins:maven-resources-plugin:2.4.3解决方案

    pom文件提示信息: Failure to transfer org.apache.maven.plugins:maven-resources-plugin:pom:2.4.3 from http:/ ...

  3. 别不信!App三年内将被HTML5顶替彻底消失?

    2007年W3C(万维网联盟)立项HTML5,直至2014年10月底,这个长达八年的规范终于正式封稿. 过去这些年,HTML5颠覆了PC互联网的格局,优化了移动互联网的体验,接下来,HTML5将颠覆原 ...

  4. 本周psp(观众页面)

    个人项目PSP(观众页面) 计划 30min 估计要用多长时间 3d 开发 5h 需求分析 1h 生成设计文档 1h 设计复审 50min 代码规范 20min 具体设计 40min 具体编码 3h ...

  5. python实现查看目录下重复的文件

    该python 脚本有以下三个功能: 1. 实现查看目录下重复的文件,输出文件按修改时间升序排列 2. 将按修改时间排列比较旧的.可删除的文件列出来 3. 按目录对重复文件进行统计,比如,目录/tmp ...

  6. windows界面库种类

    访问网址http://www.360doc.com/content/14/0612/20/13826502_386093297.shtml

  7. 删除文件夹工具【fuckwinfsdel】,如 node_modules

    强力删除文件夹. 安装 npm install fuckwinfsdel -g 使用 fuckwinfsdel youdir 例 fuckwinfsdel node_modules 项目地址 http ...

  8. 关于启用 HTTPS 的一些经验分享(二)

    转载: 关于启用 HTTPS 的一些经验分享(二) 几天前,一位朋友问我:都说推荐用 Qualys SSL Labs 这个工具测试 SSL 安全性,为什么有些安全实力很强的大厂家评分也很低?我认为这个 ...

  9. 了解Android的编译器

    了解一下Android的编译器并记录下来: Android在4.4以前是使用Dalvik VM的,通过Just In Time(JIT即时编译)来完成编译工作,在Android4.4提供了一种测试版本 ...

  10. 常见的JavaScript函数

    JavaScript函数一共可分为5类:常规函数.数组函数.日期函数.数学函数和字符串函数. (1)常规函数(9个) alert函数:显示一个警告对话框,包括一个“确定”按钮. confirm函数:显 ...