The article directories
-
-
- 17. Alphabetic combinations of telephone numbers
- Split type (leaf node) : 131. Split palindrome string
- Split type (leaf node) : 93. Recover IP address
-
17. Alphabetic combinations of telephone numbers
Class Solution {// Combine problems: SumResult =new ArrayList<>(); sumResult=new ArrayList<>(); StringBuilder stringBuilder=new StringBuilder(); Add is stringBuilder.append path.remove(path.size()-1) is stringBuilder stringBuilder.deleteCharAt(stringBuilder.length()-1) public List<String> letterCombinations(String digits) {if (digits==null || digits.length()==0) returnsumResult; // Return [] instead of null Map<Character,String> digitDic = getLetter(digits); DigitDic (digitDic, digits,0); digitDic (digitDic, digits,0); digitDic (digitDic, digits,0); digitDic (digitDic, digits,0); digitDic (digitDic, digits,0) // k with nreturn sumResult;
}
public void backtracking(Map<Character,String> digitDic,String digits,int startIndex){
if(startIndex==digits.length()){// digits.length() is k, but it is better to use digits instead of k. Sumresult.add (stringBuilder.toString())); // I <path.size is the same as I <k, because the entry condition is path.size()==kreturn; // returnSo I have one down hereelse} // digitDic should be initialized with characters instead of strings. // digitDic should be initialized with characters instead of strings. // digitDic should be initialized with characters instead of strings. Digitdi. get(digit. charAt(startIndex)); digitdi. get(digit. charAt(startIndex)); digitdi. get(digit. charAt(startIndex)); // Change the input digits to a string (through the data dictionary) // then iterate over the stringfor(int i=0; i< letters.length(); I ++){stringBuilder.appEnd (letters.charat (I)); / / each character through backtracking (digitDic, who, startIndex + 1); / / because it is a combination, here is the startIndex + 1, into the next string stringBuilder. DeleteCharAt (stringBuilder. The length () - 1); } } public Map<Character,String> getLetter(String digits){ char[] charArray = digits.toCharArray(); // k is chararray. Length k is the number of elements in each subcombination, soforMap<Character,String> result = new HashMap<>();for(int i=0; i<charArray.length; i++){if (charArray[i] == '2') result.put('2'."abc");
else if (charArray[i] == '3') result.put('3'."def");
else if (charArray[i] == '4') result.put('4'."ghi");
else if (charArray[i] == '5') result.put('5'."jkl");
else if (charArray[i] == '6') result.put('6'."mno");
else if (charArray[i] == '7') result.put('7'."pqrs");
else if (charArray[i] == '8') result.put('8'."tuv");
else if (charArray[i] == '9') result.put('9'."wxyz");
}
returnresult; }}Copy the code
The whole Solution is divided into two parts. In the main function of Solution, first, call getLetter(digits); The Map
,string>
function creates a Map
,string>
data dictionary; Second, call the backtracking traceback function
For traceback functions: 3 parameters, Map
,string>
digitDic is the data dictionary that was passed in, put the data in the data dictionary first, and then recursively retrieve it, so pass it in, in fact, there’s nothing in the recursive function that changes the data dictionary, just retrieve, So it can be a class variable. String digits is the input String that we started out with, that we took out when we recursed, so we passed it in, and in fact, we don’t change the data dictionary anywhere in the recursive function, we just took it out, it has to be an argument, not a class variable, because it’s a function of the main function of the Solution class, You can only pass int startIndex which is the sum of each recursion, so it has to be an argument
For the for loop, stringBuilder.append(letters.charat (I)); / / each character through backtracking (digitDic, who, startIndex + 1); // Since it’s a combination, startIndex+1, drill down to the next string
For the two class variables, first, sumResult is the final return value of the main function of Solution, so it must be the same as the return type required by the problem, so it is List
second, The stringBuilder replaces the List
path variable and is used in sumResult because the final return value is [” ab “, “ac”]. So replace the List
type with the StringBuilder type.
Split type (leaf node) : 131. Split palindrome string
The segmentation problem can be abstracted as a combination problem!
Given a string s, divide s into substrings such that each substring is a palindrome string.
Returns all possible segmentation schemes for S.
Example: Input: aab Output: [[” AA “, “B”], [” A “, “A”, “B”]]
class Solution {
List<List<String>> result=new ArrayList<>();
List<String> path =new ArrayList<>();
public void backtracking(String s,int startIndex){
ifResult.add (new ArrayList<>(path)); result.add(new ArrayList<>(path));return;
}
for(int i=startIndex; i<s.length(); I ++){// instead of starting at 0, start at startIndex, otherwise I +1 would not work at allif(isVerdict(s,startIndex, I)){// subting is closed at the front and open at the back, so I +1 is required to fetch the I path.add(s.substring(startIndex, I +1)); // If startIndex to I is a palindrome, put it in path}else{
continue; } backtracking(s,i+1); Remove (path.size()-1); // Remove (path.size()-1); } } public boolean isVerdict(String s,int start,int end){for(int i=start,j=end; i<j; I ++,j--) {// the length of s is odd or even, not I <=j.if(s.charAt(i)! =s.charAt(j)){return false; // not equal, returnfalse}}return true; / / the lastreturn true} public List<List<String>> partition(String s) { backtracking(s,0); // A given large combination is a set or string, so start with a small index 0, or from 0 to n-1 if given an nreturnresult; }}Copy the code
Split type (leaf node) : 93. Recover IP address
Given a string containing only numbers, undo it and return all possible IP address formats.
A valid IP address consists of four integers (each integer is between 0 and 255, and cannot contain a leading 0), separated by periods (.).
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 [email protected] are invalid IP addresses.
Example 1: Input: s = 25525511135 Output: [” 255.255.11.135 “, “255.255.111.35”]
Example 2: Input: 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”]
Tip: 0 <= S. length <= 3000 s consists of numbers only
class Solution {
List<String> result = new ArrayList<>();
public void backtracking(StringBuilder stringBuilder,int startIndex,int pointNum){
if (pointNum==3){
if(isVerdict (stringBuilder, startIndex stringBuilder), length () - 1)) {/ / because isVerdict method, It is closed left and closed right so we pass in the original string s and 0-n-1 result.add(stringBuilder.toString()); // if s is a valid IP address, place it in result instead of pathfor} // if both conditions are met (end condition + success condition = successful end condition), add it to resultreturn; } // start from starrtIndex, can't start from 0, otherwise the last time I +2 passed through would not have been usefulfor(int i=startIndex; i<stringBuilder.length(); i++){if(isVerdict (stringBuilder, startIndex, I)) {/ / from startIndex to I, left closed right closed interval / / if the division is legitimate, insert the dot the stringBuilder. Insert (I + 1,'. '); pointNum++; backtracking(stringBuilder,i+2,pointNum); / / insert after the dot, want to the latter, I + 2 position the stringBuilder. DeleteCharAt (I + 1); // delete I +1 pointNum--; }else {
break;
}
}
}
private boolean isVerdict(StringBuilder s,int startIndex,int endIndex){
if (startIndex > endIndex){
return false;
}
if (s.charAt(startIndex) == '0'&& startIndex! =endIndex){ //return false;
}
int sum = 0;
for(int i=startIndex; i<=endIndex; I++){// bothif (s.charAt(i) < '0' || s.charAt(i)>'9') {return false; } sum = sum * 10 + (s.charat (I) -'0');
if (sum > 255){
return false; // Characters greater than 255 are removed}}return true; } public List<String> restoreIpAddresses(String s) { StringBuilder stringBuilder=new StringBuilder(s); Backtracking (stringBuilder, 0, 0);returnresult; }}Copy the code