标签:character put amp note ret def 没有 lin linked
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"].
public class Solution {
public List<String> letterCombinations(String digits) {
LinkedList<String> ans = new LinkedList<String>();
if(digits.length() == 0) return ans;
String[] mapping = new String[] {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
ans.add("");
for(int i = 0; i < digits.length(); i++){
//得到那个数字 3
int x = Character.getNumericValue(digits.charAt(i));
// ans.peek().length()==i 说明还没有更新好
while(ans.peek().length()==i){
String t = ans.remove();
// 3 对应的 a b c, 在弹出的所有的后面都加上a b c,再加回去
for(char s : mapping[x].toCharArray())
ans.add(t+s);
}
}
return ans;
}
}
17. Letter Combinations of a Phone Number
标签:character put amp note ret def 没有 lin linked
原文地址:https://www.cnblogs.com/lawrenceSeattle/p/10265250.html