深度优先搜索算法(depth first search),是一个典型的图论算法。所遵循的搜索策略是尽可能“深”地去搜索一个图。

算法思想是:

对于新发现的顶点v,如果它有以点v为起点的未探测的边,则沿此边继续探测下去。当顶点v的所有边都已被探寻结束,则回溯到到达点v的先辈节点。以相同方法一直回溯到源节点为止。如果图中还有未被发现的顶点,则选择其中一个作为源顶点,重复以上的过程。最后的结果是一些不相交的深度优先树。

leetCode中的应用:

Given a collection of integers that might contain duplicates, S, return all possible subsets.

Note:

  • Elements in a subset must be in non-descending order.
  • The solution set must not contain duplicate subsets.

For example,
If S = [1,2,2], a solution is:

[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
可以使用DFS来解答,通过构造二叉树,然后得到所有子集为二叉树的所有叶子节点,然后使用DFS算法,将所有叶子节点输出。
构造二叉树如下:

实现代码如下(参考网上):

 public List<List<Integer>> subsetsWithDup(int[] num) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
if(num == null ||num.length == 0){
return res;
}
int len = num.length;
Arrays.sort(num);//对数组里的数排序
for(int i=1; i<len+1; i++){
ArrayList<Integer> numList = new ArrayList<Integer>();
dfs(res,numList,num,0,i);
}
res.add(new ArrayList<Integer>());
return res;
}
private void dfs(List<List<Integer>> res, ArrayList<Integer> numList,
int[] num, int start, int k) {
if(numList.size() == k){
res.add(new ArrayList<Integer>(numList));
return;
}
ArrayList<Integer> allList = new ArrayList<Integer>();
for(int i=start; i<num.length; i++){
if(allList.contains(num[i])) continue;
allList.add(num[i]);
numList.add(num[i]);
dfs(res, numList, num, i+1, k);
numList.remove(numList.size()-1);
}
}

深度优先搜索算法(DFS)以及leetCode的subsets II的更多相关文章

  1. 图的深度优先搜索算法DFS

    1.问题描写叙述与理解 深度优先搜索(Depth First Search.DFS)所遵循的策略.如同其名称所云.是在图中尽可能"更深"地进行搜索. 在深度优先搜索中,对最新发现的 ...

  2. [LeetCode] 90.Subsets II tag: backtracking

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  3. [leetcode]90. Subsets II数组子集(有重)

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  4. [Leetcode Week8]Subsets II

    Subsets II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/subsets-ii/description/ Description Given ...

  5. Java for LeetCode 090 Subsets II

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  6. 【leetcode】Subsets II

    Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...

  7. [LeetCode] 90. Subsets II 子集合 II

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  8. 【leetcode】Subsets II (middle) ☆

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  9. Leetcode#90 Subsets II

    原题地址 跟Subsets(参见这篇文章)类似. 但因为有重复元素,所以要考虑去重问题. 什么情况下会出现重复呢?比如S = {5, 5, 5},如果要选1个5,一共有C(3,1)=3种选法,即100 ...

随机推荐

  1. python 学习笔记 9 -- Python强大的自省简析

    1. 什么是自省? 自省就是自我评价.自我反省.自我批评.自我调控和自我教育,是孔子提出的一种自我道德修养的方法.他说:“见贤思齐焉,见不贤而内自省也.”(<论语·里仁>)当然,我们今天不 ...

  2. 手机触屏的js事件

    处理Touch事件能让你跟踪用户的每一根手指的位置.你可以绑定以下四种Touch事件:     1.touchstart:  // 手指放到屏幕上的时候触发      2.touchmove:  // ...

  3. C#学习日志 day10 -------------- problem statement

    Revision History Date Issue Description Author 15/May/2015 1.0 Finish most of the designed function. ...

  4. codeforces 8C. Looking for Order 状压dp

    题目链接 给n个物品的坐标, 和一个包裹的位置, 包裹不能移动. 每次最多可以拿两个物品, 然后将它们放到包里, 求将所有物品放到包里所需走的最小路程. 直接状压dp就好了. #include < ...

  5. jni note

    2016-1-15 javah 使用javah可以自动从java文件生成jni头文件, 用法:javah [选项] <类> 其中 [选项] 包括:         -help        ...

  6. “Options模式”下的配置是如何绑定为Options对象

    “Options模式”下的配置是如何绑定为Options对象 配置的原子结构就是单纯的键值对,并且键和值都是字符串,但是在真正的项目开发中我们一般不会单纯地以键值对的形式来使用配置.值得推荐的做法就是 ...

  7. rsyslog start with

    startswith Checks if the value is found exactly at the beginning of the property value. For example, ...

  8. CentOS 七 vs CentOS 6的不同

    CentOS 七 vs CentOS 6的不同   CentOS 7 vs CentOS 6的不同(1)桌面系统[CentOS6] GNOME 2.x[CentOS7] GNOME 3.x(GNOME ...

  9. iOS6 旋转

    iOS 6的rotation改变了很多.先来看看官方的描述  http://www.bgr.com/2012/08/06/ios-6-beta-4-change-log-now-available/ ...

  10. COCOS2d-x简易安装步骤

    准备工作:1.    下载 cocos2d-x  下载地址:http://cdn.cocos2d-x.org/cocos2d-x-2.2.zip2.    下载 python 2.7.3 下载地址:h ...