17. 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.

A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.

Example:

Input: "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].


idea: for instance, 23 , 2 represents abc, 3 represents def,
so 2 levels in total,
abc
def
3 * 3 results in total


=========================================


build the mapping from number to string , number 2 is mapped to index 2 , number 3 is mapped to index 3 for convience

=========================================
private static final:

For static final, all instances share the same value, and can't be altered after first initialized.

 "private" is an access specifier. It tells you that the member is only visible inside the class - other classes can't access the private members of a class. "staticmeansthat the variable is a class-level variable; there's only one variable, which is shared by all instances of the class.

 


=========================================
String[]

no need to use new String[]{.....}. can directly use {......}
it's like int[][] dirs = new int[][] dirs{{}, {}, {}, {}};
it's also okay to write int[][] dirs = {{}, {}, {}, {}};


=========================================


from char[] to String
char[] ans = new char[n];
new String(ans);


from stringBuilder to string
sb.toString()


=========================================

String digits = "23";
int index = 0;

digits.charAt(index) = '2'

if we want to get 2 , we must - '0', then we get the distance, and that's the number we want
int curNumber = digits.charAt(index) - '0'; 
always remmebr - '0'


=========================================
backtrack recovery :

method 1:
The
 java.lang.StringBuilder.deleteCharAt()
 method removes the char at the specified position in this sequence. This sequence is shortened by one char.

method 2:
sb.setLength(len);

=========================================
stringbuilder vs string


class Solution {

    private static final String[] mapping = new String[]{ "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" }; // why people don't put  new String[]{ } here ??????
    
    
    public List<String> letterCombinations(String digits) {
        
        char[] ans = new char[digits.length()];
        List<String> result = new ArrayList<>();
        if(digits == null || digits.length() == 0) return result;
       
        dfs(result, ans, 0, digits);
        return result;
    }
    private void dfs(List<String> result, char[] ans, int index, String digits){
        if(index == digits.length()){ 
            result.add(new String(ans));// new String() , ans is char[] 
            return;
        }
        
        int curNumber = digits.charAt(index) - '0'; //   -'0'
        String curOptions = mapping[curNumber];
        
        for(int i= 0; i < curOptions.length(); i++){
            ans[index] = curOptions.charAt(i);
            dfs(result, ans, index + 1, digits);
        }
    }

}

 

class Solution {
    // use a string array , here use scope private static final
    private static final String[] phone = new String[] { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
    
    public List<String> letterCombinations(String digits) {
        List<String> result = new ArrayList<>();
        if(digits == null || digits.length() == 0) return result;
        
        char[] input = digits.toCharArray();
        StringBuilder sb = new StringBuilder();
        dfs(result, input, 0, sb);
        return result;
    }
    private void dfs(List<String> result, char[] input, int index, StringBuilder sb){
        if(index == input.length){
            result.add(sb.toString()); // new String() ?? 
            return;
        }
        
        // 
        int curNumber = input[index] - '0'; // - '0', always forget here 
        String curOptions = phone[curNumber];
        int len = sb.length();
        
        for(int i = 0; i < curOptions.length(); i++){
            sb.append(curOptions.charAt(i));
            dfs(result, input, index + 1, sb);
            // sb is changed , so backtrack revoery is necessary 
            sb.setLength(len);
                
        }
        
    }
}

 

注意用iterative写法

 

Bfs 

 

https://leetcode.com/problems/letter-combinations-of-a-phone-number/discuss/8064/My-java-solution-with-FIFO-queue

posted on 2018-07-18 08:09  猪猪&#128055;  阅读(109)  评论(0)    收藏  举报

导航