深度优先搜索算法(DFS)以及leetCode的subsets II
深度优先搜索算法(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的更多相关文章
- 图的深度优先搜索算法DFS
1.问题描写叙述与理解 深度优先搜索(Depth First Search.DFS)所遵循的策略.如同其名称所云.是在图中尽可能"更深"地进行搜索. 在深度优先搜索中,对最新发现的 ...
- [LeetCode] 90.Subsets II tag: backtracking
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- [leetcode]90. Subsets II数组子集(有重)
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- [Leetcode Week8]Subsets II
Subsets II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/subsets-ii/description/ Description Given ...
- Java for LeetCode 090 Subsets II
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- 【leetcode】Subsets II
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
- [LeetCode] 90. Subsets II 子集合 II
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- 【leetcode】Subsets II (middle) ☆
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- Leetcode#90 Subsets II
原题地址 跟Subsets(参见这篇文章)类似. 但因为有重复元素,所以要考虑去重问题. 什么情况下会出现重复呢?比如S = {5, 5, 5},如果要选1个5,一共有C(3,1)=3种选法,即100 ...
随机推荐
- droppable的详细参数讲解
jQuery-Draggable参数介绍 默认设置值: $.extend($.ui.draggable, { version: “1.7.1″, eventPrefix: “drag”, de ...
- ajax防止重复提交请求1
ajax防止重复提交请求 A. 独占型提交 只允许同时存在一次提交操作,并且直到本次提交完成才能进行下一次提交. module.submit = function() { if (this.pro ...
- PHP程序输出日历
以下代码只是简单实现日历的效果和逻辑思路,没有使用类封装,权当抛砖引玉,有兴趣的朋友可以封装起来,方便调用. <?php /** * PHP利用时间函数输出日历 * Rain.zen $ int ...
- TCP 监控工具 TCPMonitor
1, membrane monitor Download: http://www.membrane-soa.org/downloads/archive/monitor-archive.htm 2, a ...
- codeforces 3D . Least Cost Bracket Sequence 贪心
题目链接 给一个字符串, 由( ) 以及? 组成, 将?换成( 或者 ) 组成合法的括号序列, 每一个?换成( 或者 ) 的代价都不相同, 问你最小代价是多少, 如果不能满足输出-1. 弄一个变量nu ...
- 8. java.lang.ArithmeticException
java.lang.ArithmeticException 数学运算异常 当算术运算中出现了除以零这样的运算就会出这样的异常.
- ThinkPHP中 按条件查询后列表显示
最近在项目中遇到了需要根据下拉框的条件筛选出符合条件的数据,然后进行列表显示的问题. 在ThinkPHP中进行列表显示的传统过程:通过在后台控制器中查询出数据,然后通过$this->assign ...
- windows 8 metro 开发学习资源链接
原文 http://www.cnblogs.com/icuit/archive/2012/05/30/2525979.html windows8 metro开发资源目前还是以MSDN为主,做了一个li ...
- [原]基于CAS实现单点登录(SSO):登录成功后,cas client如何返回更多用户信息
从cas server登录成功后,默认只能从casclient得到用户名.但程序中也可能遇到需要得到更多如姓名,手机号,email等更多用户信息的情况. cas client拿到用户名后再到数据库中查 ...
- c语言libcurl 使用实例get/post方法+c语言字符串处理
#include <stdio.h> #include <curl/curl.h> #include <string.h> #include <ctype.h ...