首页 > 代码库 > LeetCode——Letter Combinations of a Phone Number
LeetCode——Letter Combinations of a Phone Number
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。