标签:leetcode
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.
题目:给定一个数字字符串,返回所有数字能代表的字母的组合。
思路:将数字和对应的字母放到一个哈希表中。
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; }
LeetCode——Letter Combinations of a Phone Number
标签:leetcode
原文地址:http://blog.csdn.net/laozhaokun/article/details/38019017