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.

SOLUTION 1:

经典递归模板。

注意这一行不要写错了,根据当前的数字来取得可能的字母组合:

//get the possiable selections.
String s = map[digits.charAt(index) - '0'];

 public class Solution {
String[] map = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; public List<String> letterCombinations(String digits) {
List<String> ret = new ArrayList<String>();
if (digits == null) {
return ret;
} dfs(digits, new StringBuilder(), ret, 0);
return ret;
} public void dfs(String digits, StringBuilder sb, List<String> ret, int index) {
int len = digits.length();
if (index == len) {
ret.add(sb.toString());
return;
} // get the possiable selections.
String s = map[digits.charAt(index) - '0'];
for (int i = 0; i < s.length(); i++) {
sb.append(s.charAt(i));
dfs(digits, sb, ret, index + 1);
sb.deleteCharAt(sb.length() - 1);
}
} }

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/combination/LetterCombinations.java

LeetCode: Letter Combinations of a Phone Number 解题报告的更多相关文章

  1. [LeetCode]Letter Combinations of a Phone Number题解

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

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

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

  3. LeetCode——Letter Combinations of a Phone Number

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

  4. [LeetCode] Letter Combinations of a Phone Number

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

  5. [LeetCode] Letter Combinations of a Phone Number(bfs)

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

  6. LeetCode Letter Combinations of a Phone Number (DFS)

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

  7. [LeetCode] Letter Combinations of a Phone Number 回溯

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

  8. LeetCode Letter Combinations of a Phone Number 电话号码组合

    题意:给一个电话号码,要求返回所有在手机上按键的组合,组合必须由键盘上号码的下方的字母组成. 思路:尼玛,一直RE,题意都不说0和1怎么办.DP解决. class Solution { public: ...

  9. leetcode Letter Combinations of a Phone Number python

    class Solution(object): def letterCombinations(self, digits): """ :type digits: str : ...

随机推荐

  1. RHEL和Centos常用版本

    学而优则仕,思则进步也Q Redhat: http://pan.baidu.com/s/1qXKRqqS Centos: http://pan.baidu.com/s/1o8RrjXw

  2. 转:eclipse里面显示中文乱码

    显示中文会变成乱码解决方案:Windows- >Pereferences- >General->Workspace- >Text   File   Encoding   选项下 ...

  3. 微信小程序独家秘笈之抽奖大转盘

    代码地址如下:http://www.demodashi.com/demo/14209.html 一.前期准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.co ...

  4. 流动的推荐系统——兴趣Feed技术架构与实现

    流动的推荐系统 我们经常谈论的推荐系统(Recommender System),从形式上看是比较“静态”的推荐,通常位于网页主要信息的周边,比如电商网站的“看了又看”.“买了又买”.这种推荐系统在大多 ...

  5. 学习JUnit

    一.为什么测试很重要? 塑造系统的设计.我们知道输入和输出应该是什么样的,但是我们需要创建什么对象来做到这一点呢?代码应该塑造成什么样的"形状"?编写测试可以让我们知道应该创建什么 ...

  6. Python 的 Numpy 库

    Numpy: # NumPy库介绍 # NumPy的安装 #  NumPy系统是Python的一种开源的数值计算扩展 #  可用来存储和处理大型矩阵. #  因为不是Python的内嵌模块,因此 ...

  7. Android开发之探秘蓝牙隐藏API

    这次讲得深入些,探讨下蓝牙方面的隐藏API.用过Android系统设置(Setting)的人都知道蓝牙搜索之后可以建立配对和解除配对,但是这两项功能的函数没有在SDK中给出,那么如何去使用这两项功能呢 ...

  8. 重写lucene.net的分词器支持3.0.3.0版本

    lucene.net中每个分词器都是一个类,同时有一个辅助类,这个辅助类完成分词的大部分逻辑.分词类以Analyzer结尾,辅助类通常以Tokenizer结尾.分类词全部继承自Analyzer类,辅助 ...

  9. 什么是IIS应用程序池

    IIS应用程序池是将一个或多个应用程序链接到一个或多个工作进程集合的配置.因为应用程序池中的应用程序与其他应用程序被工作进程边界分隔,所以某个应用程序池中的应用程序不会受到其他应用程序池中应用程序所产 ...

  10. 红黑树 - C++代码实现

    红黑树的介绍 红黑树(Red-Black Tree,简称R-B Tree),它一种特殊的二叉查找树.红黑树是特殊的二叉查找树,意味着它满足二叉查找树的特征:任意一个节点所包含的键值,大于等于左孩子的键 ...