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. JS模拟list

    /* * List 大小可变数组 */ function List() { this.list = new Array(); }; /** * 将指定的元素添加到此列表的尾部. * @param ob ...

  2. Scala进阶之App特质

    App特质的作用 App特质的作用那就是延迟初始化,从代码上看它继承自DelayedInit,里面有个delayedInit方法 trait App extends DelayedInit Delay ...

  3. 【laravel5.4】PHP5.6+ 调用命名空间下类方法、属性和对象

    1.调用命名空间的类方法,对象.属性 类对象:\App\User(); 类方法:\App\User::find($this->user_id) //查询构造器方法,将$this->user ...

  4. 转载:PHP详解ob_clean,ob_start和ob_get_contents函数

    1.这三个函数运用在PHP4和PHP5中.在一些PHP项目中,经常能看到这三个函数的使用. 有的输出,前面本来要显示在页面里的东西全都被清除了,不显示了. 第二个函数:ob_start(); 告诉ph ...

  5. SpringBoot启动

    一.启动时加载数据,就用CommandLineRunner 只需要将类实现CommandLineRunner,并且加上Component注解,还可以通过Order来控制启动顺序. @Component ...

  6. iOS archive(归档)

    归档是一种很常用的文件储存方法,几乎任何类型的对象都能够被归档储存(实际上是一种文件保存的形式),浏览网上的一些资料后,并结合自己的一些经验,总结成此文. 一.使用archiveRootObject进 ...

  7. 正确关闭Redis

    1.首先关闭单机版 我的单机版 是放在redis文件夹下面的 首先你要启动你的单机版redis 直接shutdown quit 退出去 ps aux|grep redis  查看运行的redis 关闭 ...

  8. razor----js

    <script> $(document).ready(function () { // 2  直接加引号转换 var SpecialAptitude = '@Model.SpecialAp ...

  9. android studio 中出现"...ProjectScript\buildscript\cache.properties.lock"

      Owner PID: unknownOur PID: 8496Owner Operation: unknownOur operation: Initialize cacheLock file: C ...

  10. DBA_实践指南系列4_Oracle Erp R12系统备份和恢复Backup(案例)

    2013-12-04 Created By BaoXinjian