Time：2021-2-27

# 1. Introduction to backtracking

Backtracking problem is the traversal process of decision tree. The following problems need to be considered for backtracking problem

• Path: the choice that has been made, that is, the path from the root node to the current node
• Selection list: what other choices can be made in the current situation, that is, to continue to traverse the nodes and what paths can be taken
• End condition: reach the leaf node of the decision tree, or stop if the condition is not satisfied

# 2. Backtracking problem framework

After understanding several problems of backtracking, let’s look at the basic framework of backtracking

``````List<String> result = new ArrayList<String>();
Public void backtrack{
If (meet the end condition)
result.add (the path that has been selected);
return;

For select in the list of choices that can be made{
Select list. Remove;
Backtrack (the path that has been selected and can be selected);

//Revocation
The selected path. Remove;
A list of choices. Add (selection);
}
}``````

# 3. Cases

Optics does not practice the fake grip, before seeing the idea, first write it to realize, and then see if there is any place to optimize

## 3.1 string permutation

Given aThere is no repetitionA sequence of numbers that returns all possible permutations. Source:https://leetcode-cn.com/problems/permutations/

Example:

``````//Input: [1,2,3]
//Output:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]``````
``````class Solution {

public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
int[] visited = new int[nums.length];
backtrack(res, nums, new ArrayList<Integer>(), visited);
return res;

}

private void backtrack(List<List<Integer>> res, int[] nums, ArrayList<Integer> tmp, int[] visited) {
if (tmp.size() == nums.length) {
return;
}
for (int i = 0; i < nums.length; i++) {
if (visited[i] == 1) continue;
visited[i] = 1;
backtrack(res, nums, tmp, visited);
visited[i] = 0;
tmp.remove(tmp.size() - 1);
}
}
}``````

notes: where`res`Is the global return, TMP is the node on the current path,`visited `and`nums `To identify which nodes are currently accessible

Given a string containing only numbers, recover it and return all possible IP address formats. Source:https://leetcode-cn.com/problems/restore-ip-addresses/

A valid IP address consists of exactly four integers (each of which is between 0 and 255 and can’t contain a leading 0), separated by ‘.

For example: “0.1.2.201” and “192.168.1.1” are valid IP addresses, but “0.011.255.245”, “192.168.1.312” and “192.168.1.1” are valid IP addresses [email protected] “Is an invalid IP address.

Example 1:

Input: S = “25525511135”
Output: [“255.255.11.135”, “255.255.111.35”]
Example 2:

Enter: S = “0000”
Output: [“0.0.0.0”]
Example 3:

Input: S = 1111
Output: [“1.1.1.1”]
Example 4:

Input: S = “010010”
Output: [“0.10.0.10”, “0.100.1.0”]
Example 5:

Input: S = “101023”
Output: [“1.0.10.23”, “1.0.102.3”, “10.1.0.23”, “10.10.2.3”, “101.0.2.3”]

Solution code

``````class Solution {
List<String> res = new ArrayList();
List<String> temp = new ArrayList();
helper(res,temp,s);
return res;
}

void helper(List<String> res,List<String> temp,String next) {
if(temp.size() > 4) {
return;
}
if(temp.size() == 4 && next.length() == 0) {
String ip = temp.get(0) + "." + temp.get(1) + "." + temp.get(2) + "." + temp.get(3);
return;
}
for(int i = 0; i < next.length(); i++) {
String s = next.substring(0,i+1);
if(s.length() > 1 && s.charAt(0) == '0') {
continue;
}
if(s.length() > 3) {
continue;
}
if(s.length() == 3 && "255".compareTo(s) < 0) {
continue;
}
helper(res,temp,next.substring(i+1));
temp.remove(temp.size() - 1);
}
}
}``````

notes: the overall framework still adopts the above structure, but when judging whether the current node should join temp, it needs to make a little complicated judgment
Wu Xie, little third master, is a rookie in the field of big data and artificial intelligence.