Question

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.

Solution

We can draw out the solution space tree and then dfs traverse this tree.

For example, input is "23"

        ' '

      /  |  \

     'a'   'b'  'c'

   / | \ / | \ / | \

  'd'   'e'  'f''d' 'e' 'f' 'd'  'e'   'f'

Two java tricks:

Convert String to int:  Integer.parseInt(string);

Convert char to int:     Character.getNumericValue(element.charAt(2));

 public class Solution {
public static final char[][] telephone = {
{'a','b','c',' '},
{'d','e','f',' '},
{'g','h','i',' '},
{'j','k','l',' '},
{'m','n','o',' '},
{'p','q','r','s'},
{'t','u','v',' '},
{'w','x','y','z'}
}; public List<String> letterCombinations(String digits) {
List<String> result = new ArrayList<String>();
if (digits == null || digits.length() < 1)
return result;
dfs(digits, 0, new ArrayList<Character>(), result);
return result;
} private void dfs(String digits, int start, List<Character> list, List<String> result) {
if (start < 0)
return;
if (start >= digits.length()) {
int size = list.size();
StringBuilder sb = new StringBuilder(size);
for (char tmpChar : list)
sb.append(tmpChar);
result.add(sb.toString());
return;
}
// Convert char to int
int index = Character.getNumericValue(digits.charAt(start));
if (index < 2 || index > 9)
return;
char[] chars = telephone[index - 2];
for (char tmpChar : chars) {
if (tmpChar != ' ') {
list.add(tmpChar);
dfs(digits, start + 1, list, result);
list.remove(list.size() - 1);
}
}
}
}

Letter Combinations of a Phone Number 解答的更多相关文章

  1. [LintCode] Letter Combinations of a Phone Number 电话号码的字母组合

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  2. 69. Letter Combinations of a Phone Number

    Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...

  3. 【leetcode】Letter Combinations of a Phone Number

    Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...

  4. [LeetCode][Python]17: Letter Combinations of a Phone Number

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...

  5. Letter Combinations of a Phone Number:深度优先和广度优先两种解法

    Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...

  6. leetcode-algorithms-17 Letter Combinations of a Phone Number

    leetcode-algorithms-17 Letter Combinations of a Phone Number Given a string containing digits from 2 ...

  7. 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  8. Letter Combinations of a Phone Number - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Letter Combinations of a Phone Number - LeetCode 注意点 可以不用按字典序排序 解法 解法一:输入的数字逐 ...

  9. LeetCode: Letter Combinations of a Phone Number 解题报告

    Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...

随机推荐

  1. EBS R12 查询EBS用户相关SQL

    --R12查询EBS在线用户SQL  SELECT U.USER_NAME,        APP.APPLICATION_SHORT_NAME,        FAT.APPLICATION_NAM ...

  2. SQL Server (MSSQLSERVER) 服务因 找不到指定的模块。 服务特定错误而停止。

    新装了sql server 2008,发现sqlserver 服务没法起来.查看系统日志是7024如下: 事件类型: 错误 事件来源: Service Control Manager 事件种类: 无 ...

  3. 恢复Linux下被误删除的文件(笔记)

    恢复Linux下被误删除的文件 [root@xuegod63 ~]# mount /dev/cdrom /mnt/ 分一个区:sda4  查找:extundelete 分一个区:sda4  [root ...

  4. 一个简陋的 CSS 样式

    有些网友对 Smart Framewok 中的 Sample 示例的样式比较感兴趣.由于本人对前端不太精通,但为了满足网友们的需求,只好献丑了. 以下这个简陋的 CSS 样式: ? 1 2 3 4 5 ...

  5. wifi_uplink脚本分析

    ~ >: vim apps/tools/wifi_uplink  #!/bin/sh # Copyright (C) 2012 GuoWenxue <guowenxue@gmail.com ...

  6. Cordova for android怎样在App中处理退出button事件

    项目须要在HTML5 Android App中增加对返回键的处理,发现直接在Activity中加返回键处理代码不起作用,分析cordova源代码发现返回键已经被WebView处理掉了,所以仅仅能在js ...

  7. [置顶] 创建GitHub技术博客全攻略

    [置顶] 创建GitHub技术博客全攻略 分类: GitHub2014-07-12 13:10 19710人阅读 评论(21) 收藏 举报 githubio技术博客网站生成 说明: 首先,你需要注册一 ...

  8. html进阶css(2)

    选择器的类型 <!doctype html> <html> <head> <meta charset="utf-8"> <ti ...

  9. Linux串口编程のtermios 结构

    termios 结构是在POSIX规范中定义的标准接口,它类似于系统V中的termio接口,通过设置termios类型的数据结构中的值和使用一小 组函数调用,你就可以对终端接口进行控制. 可以被调整来 ...

  10. shell脚本例子

    #!/bin/sh   str="####" echo $1 | grep $str 1>/dev/null if [ `echo $?` -eq 0 ] then   ec ...