深度优先搜索算法(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);
        }
    }
时间: 2024-10-14 13:35:16

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

LeetCode --- 90. Subsets II

题目链接:Subsets II 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]

Java for LeetCode 090 Subsets II

Given a collection of integers that might contain duplicates, nums, 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 nums = [1,2,2], a soluti

图的深度优先搜索算法DFS

1.问题描述与理解 深度优先搜索(Depth First Search,DFS)所遵循的策略,如同其名称所云,是在图中尽可能"更深"地进行搜索.在深度优先搜索中,对最新发现的顶点v若此顶点尚有未探索过从其出发的边就探索之.当v的所有边都被探索过,搜索"回溯"到从其出发发现顶点v的顶点.此过程继续直至发现所有从源点可达的顶点.若图中还有未发现的顶点,则以其中之一为新的源点重复搜索,直至所有的顶点都被发现.与BFS中源顶点是指定的稍有不同. DFS搜索轨迹Gπ将形成一片

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

Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set). Note: The solution set must not contain duplicate subsets. Input: [1,2,2] Output: [ [2], [1], [1,2,2], [2,2], [1,2], [] ] 题意: 是的[leetcode

[LeetCode] 90.Subsets II tag: backtracking

Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set). Note: The solution set must not contain duplicate subsets. Example: Input: [1,2,2] Output: [ [2], [1], [1,2,2], [2,2], [1,2], [] ] 这个题目思路

【leetcode】Subsets II

Subsets II 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 so

[LeetCode][JavaScript]Subsets II

Subsets II Given a collection of integers that might contain duplicates, nums, 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 nums = [1,2,2]

【LeetCode】Subsets II 解题报告

[题目] 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 solutio

【leetcode】Subsets II (middle) ☆

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: