LeetCode——Letter Combinations of a Phone Number
1591 ワード
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.
Note: Although the above answer is in lexicographical order, your answer could be in any order you want. 原題リンク:https://oj.leetcode.com/problems/letter-combinations-of-a-phone-number/
タイトル:数値文字列を指定し、すべての数値が表すアルファベットの組み合わせを返します.
構想:数字と対応するアルファベットをハッシュテーブルに配置します.
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. 原題リンク:https://oj.leetcode.com/problems/letter-combinations-of-a-phone-number/
タイトル:数値文字列を指定し、すべての数値が表すアルファベットの組み合わせを返します.
構想:数字と対応するアルファベットをハッシュテーブルに配置します.
static final HashMap<Character, String> map = new HashMap<Character, String>() {
{
put('2', "abc");
put('3', "def");
put('4', "ghi");
put('5', "jkl");
put('6', "mno");
put('7', "pqrs");
put('8', "tuv");
put('9', "wxyz");
}
};
public static List<String> letterCombinations(String digits) {
List<String> res = new ArrayList<String>();
res.add("");
for (int i = 0; i < digits.length(); i++) {
List<String> tmp = new ArrayList<String>();
for (String str : res) {
String letters = map.get(digits.charAt(i));
for (int j = 0; j < letters.length(); j++)
tmp.add(str + letters.charAt(j));
}
res = tmp;
}
return res;
}