图 DFS BFS 回溯

来源:互联网 发布:网络黑市怎么打开 编辑:程序博客网 时间:2024/06/08 20:48

#1 图的遍历

DFS:先序也可后序,就是无法中序。递归,非递归

https://oj.leetcode.com/tag/depth-first-search/

BFS:层序。只能递归。好处是容易找最短路径。

https://oj.leetcode.com/tag/breadth-first-search/

图:因为有环,所以需要判断是否访问过

https://oj.leetcode.com/tag/graph/

Backtracking:根据条件,提前终止某些分支的遍历。

https://oj.leetcode.com/tag/backtracking/

D1: https://oj.leetcode.com/problems/clone-graph/ (BFS/DFS)

E12:https://oj.leetcode.com/problems/copy-list-with-random-pointer/ (使用map不是最好办法)

D2:https://oj.leetcode.com/problems/word-ladder/ (BFS)

          https://oj.leetcode.com/problems/word-ladder-ii/ (BFS + DFS)

D3:https://oj.leetcode.com/problems/word-search/ (DFS backtracking) (

D4:https://oj.leetcode.com/problems/word-break/ (DFS + Mem 或者DP)

          https://oj.leetcode.com/problems/word-break-ii/ (DFS + Mem)

D8:https://oj.leetcode.com/problems/valid-sudoku/ (三个布尔矩阵)先除3再乘3的方式计算cell

          https://oj.leetcode.com/problems/sudoku-solver/ (DFS backtracking)

检测有向图中的环 (DFS + path检测)

http://lintcode.com/en/problem/topological-sorting/ (BFS)

用于查找所有结果模板

public class Solution {    public List<List<Integer>> subsets(int[] S) {        Arrays.sort(S);        List<List<Integer>> result = new ArrayList<List<Integer>>();List<Integer> path = new ArrayList<Integer>();        helper(S, 0, path, result);        return result;    }    private void helper(int[] input, int index, List<Integer> path, List<List<Integer>> result) {        result.add(new ArrayList<Integer>(path));        for (int i = index; i < input.length; i++) {            path.add(input[i]);            helper(input, i + 1, path, result);            path.remove(path.size() - 1);        }    }}


https://leetcode.com/problems/palindrome-partitioning/

https://leetcode.com/problems/restore-ip-addresses/

C12: https://oj.leetcode.com/problems/path-sum-ii/

D5:https://oj.leetcode.com/problems/letter-combinations-of-a-phone-number/ (DFS)

D6:https://oj.leetcode.com/problems/generate-parentheses/ 1子节点: )或( 2生成结果条件:左右括号都是零 3停止条件: 左边剩的多或者左右之一用光

D7:https://oj.leetcode.com/problems/n-queens/ 模板

         https://oj.leetcode.com/problems/n-queens-ii/ 

D9:https://oj.leetcode.com/problems/subsets/ (输入元素不重复)

          https://oj.leetcode.com/problems/subsets-ii/ (输入元素重复,结果不允许重复。hashset省事 前后比较最好)

D10:https://oj.leetcode.com/problems/combinations/

            https://oj.leetcode.com/problems/combination-sum/  (输入元素不重复 输入元素可以多次使用)

            https://oj.leetcode.com/problems/combination-sum-ii/ (输入元素重复 输入元素不可以多次使用) 

D11:https://oj.leetcode.com/problems/permutations/ (visited实现)

            http://www.lintcode.com/en/problem/permutations/ (swap实现)

            https://oj.leetcode.com/problems/permutations-ii/  (hashmap去重省事O(n^n) visited数组去非常重复杂O(n!))

            https://oj.leetcode.com/problems/permutation-sequence/ (除(n-1)!, 模(n-1)! 递归)

            https://leetcode.com/problems/next-permutation/ ()


D12:https://oj.leetcode.com/problems/wildcard-matching/ (TODO)

D13:https://oj.leetcode.com/problems/regular-expression-matching/ 

https://oj.leetcode.com/problems/surrounded-regions/ (非递归BFS/DFS)

0 0
原创粉丝点击