Letter Combinations of a Phone N
2018-03-11 本文已影响0人
nafoahnaw
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.
imageInput: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 List<String> letterCombinations(String digits) {
LinkedList<String> result = new LinkedList<String>();
if(digits == null || digits.length() == 0){
return result;
}
String[] buttons = new String[]{"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
result.add("");
/**
* 利用FIFO
*/
for(int i = 0; i < digits.length(); i++){
int c = Character.getNumericValue(digits.charAt(i));
while (result.peek().length() == i){
/**
* 顶端弹出栈
*/
String p = result.remove();
for(char cr : buttons[c].toCharArray()){
/**
* 入栈,在队列尾端增加
*/
result.offer(p + cr);
}
}
}
return result;
}