图 DFS BFS 回溯

#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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值