L1:Letter Combinations of a Phone Number
来源:互联网 发布:淘宝联盟高级账户佣金 编辑:程序博客网 时间:2024/05/24 04:18
https://oj.leetcode.com/problems/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.
/** * Copyright: NineChapter * - Algorithm Course, Mock Interview, Interview Questions * - More details on: http://www.ninechapter.com/ */public class Solution { public ArrayList<String> letterCombinations(String digits) { ArrayList<String> result = new ArrayList<String>(); if (digits == null) { return result; } 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, result); return result; } private void helper(Map<Character, char[]> map, String digits, StringBuilder sb, ArrayList<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); } }}
0 0
- L1: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] Letter Combinations of a Phone Number
- [LeetCode] Letter Combinations of a Phone Number
- LeetCode18:Letter Combinations of a Phone Number
- Letter Combinations of a Phone Number
- 【leetcode】Letter Combinations of a Phone Number
- LeetCode: Letter Combinations of a Phone Number
- Letter Combinations of a Phone Number
- Letter Combinations of a Phone Number
- [LeetCode]Letter Combinations of a Phone Number
- LeetCode-Letter Combinations of a Phone Number
- Letter Combinations of a Phone Number
- [LeetCode] Letter Combinations of a Phone Number
- Letter Combinations of a Phone Number
- LeetCode - Letter Combinations of a Phone Number
- L1:Combination Sum
- LabVIEW上位机与串口通信
- IOS开发--第一个IOS开发程序(简易的计算器)
- CentOS配置smaba与Windows共享文件
- 三极管的电平转换及驱动电路分析
- L1:Letter Combinations of a Phone Number
- 对功率谱的一点理解
- 数组的指针特性
- DSP-BIOS使用入门
- L1:Palindrome Partitioning
- 使用Tcl脚本分配FPGA管脚
- Markdown中插入数学公式的方法
- 金庸武侠小说之我的见解
- L1:Restore IP Addresses