1. 算法思想
回溯法(backtracking)是优先搜索的一种特殊情况,又称为试探法,常用于需要记录节点状态的深度优先搜索。
排列、组合、选择类问题使用回溯法比较方便。
回溯步骤:修改当前节点状态-》递归子节点-》回改当前节点状态。
注意点:按引用或全局记录状态,所有状态修改在递归完成后需回退,才能继续进行下一节点的访问。
修改有两种情况:一种是修改最后一位输出,比如排列组合;一种是修改访问标记,比如矩阵里搜字符串。
2.常见题型
LeetCode-46. Permutations [C++][Java]_贫道绝缘子的博客-优快云博客Given an arraynumsof distinct integers, returnall the possible permutations. You can return the answer inany order.https://blog.youkuaiyun.com/qq_15711195/article/details/123301860LeetCode-47. Permutations II [C++][Java]_贫道绝缘子的博客-优快云博客Given a collection of numbers,nums,that might contain duplicates, returnall possible unique permutationsin any order.
https://blog.youkuaiyun.com/qq_15711195/article/details/123302599LeetCode-77. Combinations [C++][Java]_贫道绝缘子的博客-优快云博客Given two integersnandk, returnall possible combinations ofknumbers out of the range[1, n]. You may return the answer inany order.
https://blog.youkuaiyun.com/qq_15711195/article/details/123305538LeetCode-79. Word Search [C++][Java]_贫道绝缘子的博客-优快云博客Given anm x ngrid of charactersboardand a stringword, returntrueifwordexists in the grid. The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter ce
https://blog.youkuaiyun.com/qq_15711195/article/details/123306202LeetCode-51. N-Queens [C++][Java]_贫道绝缘子的博客-优快云博客Then-queenspuzzle is the problem of placingnqueens on ann x nchessboard such that no two queens attack each other. Given an integern, returnall distinct solutions to then-queens puzzle.
https://blog.youkuaiyun.com/qq_15711195/article/details/123307536LeetCode-52. N-Queens II [C++][Java]_贫道绝缘子的博客-优快云博客Then-queenspuzzle is the problem of placingnqueens on ann x nchessboard such that no two queens attack each other. Given an integern, returnthe number of distinct solutions to then-queens puzzle.
https://blog.youkuaiyun.com/qq_15711195/article/details/123311096LeetCode-39. Combination Sum [C++][Java]_贫道绝缘子的博客-优快云博客You may return the combinations inany order. Thesamenumber may be chosen fromcandidatesanunlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different.
https://blog.youkuaiyun.com/qq_15711195/article/details/123312301LeetCode-40. Combination Sum II [C++][Java]_贫道绝缘子的博客-优快云博客Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations incandidateswhere the candidate numbers sum totarget. Each number incandidatesmay only be usedoncein the combination.
https://blog.youkuaiyun.com/qq_15711195/article/details/123312030LeetCode-37. Sudoku Solver [C++][Java]_贫道绝缘子的博客-优快云博客Write a program to solve a Sudoku puzzle by filling the empty cells.
https://blog.youkuaiyun.com/qq_15711195/article/details/123313416
LeetCode-46. Permutations [C++][Java]_贫道绝缘子的博客-优快云博客Given an arraynumsof distinct integers, returnall the possible permutations. You can return the answer inany order.https://blog.youkuaiyun.com/qq_15711195/article/details/123301860LeetCode-47. Permutations II [C++][Java]_贫道绝缘子的博客-优快云博客Given a collection of numbers,nums,that might contain duplicates, returnall possible unique permutationsin any order.
https://blog.youkuaiyun.com/qq_15711195/article/details/123302599