leetcode17. Letter Combinations of a Phone Number
来源:互联网 发布:电脑初学者软件好 编辑:程序博客网 时间:2024/06/04 08:18
17. 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"].
解法
回溯法
建立一个Character到char[]的map
当sb增加时,根据sb的长度选择要添加的key对应的字符,比如digits=“234”,比如sb的长度是1,digits.charAt(1)=3;sb的长度是0,digits.charAt(0)=2; map.get(digits.charAt(0)) ={‘a’, ‘b’, ‘c’}。依次添加的是a->d->g, a->d->h, a->d->i, a->e->g, a->e->h,….,c->f->i。
public class Solution { public List<String> letterCombinations(String digits) { List<String> ret = new ArrayList<>(); if (digits == null || digits.length() == 0) { return ret; } Map<Character, char[]> map = new HashMap<Character, char[]>(); map.put('0', new char[]{}); map.put('1', new char[]{}); map.put('2', new char[]{'a', 'b', 'c'}); map.put('3', new char[]{'d', 'e', 'f'}); map.put('4', new char[]{'g', 'h', 'i'}); map.put('5', new char[] { 'j', 'k', 'l' }); map.put('6', new char[] { 'm', 'n', 'o' }); map.put('7', new char[] { 'p', 'q', 'r', 's' }); map.put('8', new char[] { 't', 'u', 'v'}); map.put('9', new char[] { 'w', 'x', 'y', 'z' }); StringBuilder sb = new StringBuilder(); helper(map, digits, sb, ret); return ret; } private void helper(Map<Character, char[]> map, String digits, StringBuilder sb, List<String> result) { if (sb.length() == digits.length()) { result.add(sb.toString()); return; } for (char c : map.get(digits.charAt(sb.length()))) { sb.append(c); helper(map, digits, sb, result); sb.deleteCharAt(sb.length() - 1); } }}
阅读全文
1 0
- [LeetCode17]Letter Combinations of a Phone Number
- LeetCode17:Letter Combinations of a Phone Number
- leetcode17 Letter Combinations of a Phone Number
- LeetCode17:Letter Combinations of a Phone Number
- leetcode17---Letter Combinations of a Phone Number
- LeetCode17. Letter Combinations of a Phone Number
- leetcode17:Letter Combinations of a Phone Number
- LeetCode17. Letter Combinations of a Phone Number
- leetcode17. Letter Combinations of a Phone Number
- leetcode17 Letter Combinations of a Phone Number
- [Java]LeetCode17 Letter Combinations of a Phone Number
- LeetCode17. Letter Combinations of a Phone Number可能是世界上最简明的解法了
- LeetCode17 17. Letter Combinations of a Phone Number My Submissions QuestionEditorial Solution
- LeetCode17 Letter Combinations of a Phone Number(Python and Java)
- LeetCode17——Letter Combinations of a Phone Number(手机几个按键对应的字母(符号)组合)
- LeetCode: Letter Combinations of a Phone Number
- LeetCode Letter Combinations of a Phone Number
- LeetCode: Letter Combinations of a Phone Number
- LeetCode 48.Rotate Image & 49.Group Anagrams
- firewalld
- 防止事件冒泡和阻止默认事件的兼容写法
- java8中的stream,function包的常用方法总结
- 对数组的一些操作
- leetcode17. Letter Combinations of a Phone Number
- jquery根据指定元素获取兄弟元素
- 03执行过程
- EventBus初体验
- QT5 Ping命令
- poj 1426
- BZOJ4850: [Jsoi2016]灯塔
- 28.使用onSaveInstanceState()保存活动被回收时的数据
- lintcode/leetcode由易至难第5题:Hamming Distance