深度优先搜索算法(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. getchar()与EOF

    大师级经典的著作,要字斟句酌的去读,去理解.以前在看K&R的The C Programming Language(Second Edition)中第1.5节的字符输入/输出,很迷惑getcha ...

  2. CentOS 7 下yum安装xtrabackup备份工具

    第一步:安装xtrabackup的数据库 yum install https://www.percona.com/redir/downloads/percona-release/redhat/late ...

  3. JSON 解析器。JSON.stringify和JSON.parse

    以前用的是JavaScript  的eval. 现在JSON 提供了JSON.stringify和JSON.parse两个函数. JSON.parse用于从一个字符串中解析出json对象. JSON. ...

  4. poj2390

    #include <stdio.h> #include <stdlib.h> int main() { int r,m,y,i; scanf("%d %d %d&qu ...

  5. OpenGL中glRotatef()函数究竟对矩阵做了什么

    OpenGL中glRotatef()函数究竟对矩阵做了什么 我们知道OpenGL中维持着两套矩阵,一个是模型视图矩阵(model view matrix),另一个是投影矩阵(projection ma ...

  6. 前端web应用组件化(一) 徐飞

    https://github.com/xufei/blog/issues/6 Web应用的组件化(一) 基本思路 1. 为什么要做组件化? 无论前端也好,后端也好,都是整个软件体系的一部分.软件产品也 ...

  7. 在线词典php

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. 借@阿里巴巴 耍了个帅——HTML5 JavaScript实现图片文字识别与提取

    写在前面 8月底的时候,@阿里巴巴 推出了一款名为“拯救斯诺克”的闯关游戏,作为前端校园招聘的热身,做的相当不错,让我非常喜欢.后来又传出了一条消息,阿里推出了A-star(阿里星)计划,入职阿里的技 ...

  9. css3幻灯片换位效果

    <title>css3幻灯片换位效果</title> <style type="text/css">  .flowGallery {width: ...

  10. mysql关联更新

    update tb_sdd_info a,tb_bnm_evian_info b set a.username=b.username where a.username=b.memberno and  ...