回溯问题作为面试算法中经典问题之一,同时也是很容易总结出一套固定解题模板的算法类别,这里使用Leetcode中top-100 liked 为例,并尝试在解题过程中总结出对应的解题思路和解题模板
重点:
1.对于回溯问题最重要的一点就是在 foward—>backtrack 这一过程状态的变化 例如从一个状态i 向前进行若干操作之后回溯到状态i 此时要把对应的其他改变的值恢复到状态i
2.对于回溯使用的场景大多是穷尽所有的情况 同时又不易通过遍历将情况一一穷举出来的时候。
示例1
Letter Combinations of a Phone Number
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.
Input: "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
class Solution {
List<String> result=new ArrayList<>();
Map<String, String> phone = new HashMap<String, String>() {
{
put("2", "abc");
put("3", "def");
put("4", "ghi");
put("5", "jkl");