lintcode -- 电话号码的字母组合
来源:互联网 发布:mac pro 关闭手写输入 编辑:程序博客网 时间:2024/04/30 09:33
Given a digit string excluded 01
, 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.
注意事项
以上的答案是按照词典编撰顺序进行输出的,不过,在做本题时,你也可以任意选择你喜欢的输出顺序。
给定 "23"
返回 ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]
/*
给定一个数字字符串除外01,返回所有可能的字母组合,该数字可以代表。
数字到字母(如电话按钮)的映射如下。
思路:
1.先把数字对应的字母存入Hash Map中
2.回溯法组合
*/
public class Solution {
public ArrayList<String> letterCombinations(String digits) {
ArrayList<String> result = new ArrayList<String>();
if (digits == null || digits.equals("")) {
return result;
}
Map<Character, char[]> map = new HashMap<Character, 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);//回退
}
}
}
- LintCode: 电话号码的字母组合
- lintcode,电话号码的字母组合
- lintcode -- 电话号码的字母组合
- 电话号码的字母组合-lintcode
- LintCode 电话号码的字母组合
- lintcode---电话号码的字母组合
- 电话号码的字母组合-LintCode
- lintcode-电话号码的字母组合-425
- 电话号码的字母组合
- 电话号码的字母组合
- 电话号码的字母组合
- 电话号码的字母组合
- 电话号码的字母组合
- LeetCode 17 Letter Combinations of a Phone Number(电话号码的字母组合)
- 425.Letter Combinations of a Phone Number-电话号码的字母组合(中等题)
- [LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合
- 17:Letter Combinations of a Phone Number(字母组合的电话号码)
- Letter Combinations of a Phone Number (电话号码字母组合) 【leetcode】
- 关于弹性调度系统设计细节
- c下的不常用的字符串操作
- 使用docker安装lamp
- python-面向对象
- 机械陀螺仪(ADS8327, STM32F405RGT6)的研究总结报告(连载)
- lintcode -- 电话号码的字母组合
- 暑期集训之ACboy needs your help again!
- Android横竖屏切换时,状态和信息的维护
- ubuntu中搜狗输入法无法中英切换解决方法
- java 获取中文拼音首字母(缩写)
- Redis源码学习(2):事件循环
- 智能专项练习--JavaScript+jQuery+ES6+angular+node(1)
- 洛谷 P1825 [USACO11OPEN]玉米田迷宫Corn Maze
- LeetCode 492. Construct the Rectangle