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.
Input:Digit string "23" Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.
public class Solution {
public List<String> letterCombinations(String digits) {
List<String> list = new ArrayList<String>();
if("".equals(digits))
return list;
//跟往常一样,对输入进行最简单的判断
String[][] map = {{"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"}};
//将字母依次分配
list.add("");//初始化结果
for(int i=0, L=digits.length(); i<L; i++) {
int digit = digits.charAt(i)-'2';//对数值处理,对应第几个数组
List<String> prev =list;
list = new ArrayList<String>();
for(String str1 : map[digit]) {
for(String str2 : prev) {
list.add(str2 + str1);
}
}
}
return list;
}
}