【题目】
Given a digit string, 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.
【思路】
直接采用递归
【Java代码】
public class Solution_17_letter_combinations_of_a_phone_number {
String[][] dict = {{"a","b","c"},{"d","e","f"},{"g","h","i"},{"j","k","l"},
{"m","n","o"},{"p","q","r","s"},{"t","u","v"},{"w","x","y","z"}};
public List<String> letterCombinations(String digits){
if(digits.length() == 0)
return new ArrayList<String>();
int index = Character.getNumericValue(digits.charAt(0))-2;
if(digits.length() == 1){
return Arrays.asList(dict[index]);
}
else{
List<String> curr = letterCombinations(digits.substring(1));
List<String>result = new ArrayList<String>();
for(int i = 0 ; i < dict[index].length ; i++)
for(int j = 0 ; j < curr.size() ; j++)
result.add(dict[index][i]+curr.get(j));
return result;
}
}
}