题目

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.

题解

这道题也是来算一种combination的题,跟 CombinationsWord Break II 都比较类似(其实和leetcode里面很多题都相似)。

代码如下:

 1   public ArrayList<String> letterCombinations(String digits) {
 2       ArrayList<String> result=new ArrayList<String>();
 3       if (digits==null)
 4           return result;
 5 
 6       String[] keyboard={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
 7       StringBuilder current=new StringBuilder();
 8       
 9       int index=0;
       buildResult(digits, index, current, keyboard, result);
       return result;
   }
   
   private void buildResult(String digits, int index, StringBuilder current, String[] keyboard, ArrayList<String> result){
       if (index==digits.length()){
         result.add(current.toString());
         return;
         }
         
       int num=digits.charAt(index)-'0';//get integer number
       for (int i=0; i<keyboard[num].length(); i++){
         current.append(keyboard[num].charAt(i));
         buildResult(digits, index+1, current, keyboard, result);
         current.deleteCharAt(current.length()-1);
         }
     }

Refrence:http://rleetcode.blogspot.com/2014/02/letter-combinations-of-phone-number-java.html

Letter Combinations of a Phone Number leetcode java的更多相关文章

  1. Letter Combinations of a Phone Number - LeetCode

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

  2. Letter Combinations of a Phone Number——LeetCode

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

  3. LeetCode解题报告—— Container With Most Water & 3Sum Closest & Letter Combinations of a Phone Number

    1.  Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a ...

  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. LeetCode: Letter Combinations of a Phone Number 解题报告

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

  6. Leetcode之回溯法专题-17. 电话号码的字母组合(Letter Combinations of a Phone Number)

    [Leetcode]17. 电话号码的字母组合(Letter Combinations of a Phone Number) 题目描述: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组 ...

  7. 【leetcode】Letter Combinations of a Phone Number

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

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

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

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

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

随机推荐

  1. normalizr实践使用(个人总结,仅供参考)

    # normalizr实践使用 原数据 (自编数据,本数据仅供参考) var aaaObj ={ "id" : "0000000000000000000000000000 ...

  2. android activity 启动模式

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 1,标准的, 2,单个 顶部 3,单个 任务 4,单个 实例 标准的 就是 每启动一次这 ...

  3. 【BZOJ-2888】资源运输 LCT + 启发式合并

    2888: 资源运输 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 63  Solved: 33[Submit][Status][Discuss] D ...

  4. 吴恩达-coursera-机器学习-week4

    第八.神经网络:表述(Neural Networks: Representation) 8.1 非线性假设 8.2 神经元和大脑 8.3 模型表示1 8.4 模型表示2 8.5 样本和直观理解1 8. ...

  5. spring-boot 速成(8) 集成druid+mybatis

    spring-boot与druid.mybatis集成(包括pageHelper分页插件), 要添加以下几个依赖项: compile('mysql:mysql-connector-java:6.0.5 ...

  6. 关于MongoDB时区问题

    由于MongoDb存储时间按照UTC时间存储的,其官方驱动MongoDB.driver存储时间的时候将本地时间转换为了utc时间,但它有个蛋疼的bug,读取的时候非常蛋疼的是返回的是utc使时间.一个 ...

  7. Linux网络编程socket选项之SO_LINGER,SO_REUSEADDR

    from http://blog.csdn.net/feiyinzilgd/article/details/5894300 Linux网络编程中,socket的选项很多.其中几个比较重要的选项有:SO ...

  8. AngularJS自定义Directive与controller的交互

    有时候,自定义的Directive中需要调用controller中的方法,即Directive与controller有一定的耦合度. 比如有如下的一个controller: app.controlle ...

  9. Windows Phone本地数据库(SQLCE):12、插入数据(翻译)

    这是“windows phone mango本地数据库(sqlce)”系列短片文章的第十二篇. 为了让你开始在Windows Phone Mango中使用数据库,这一系列短片文章将覆盖所有你需要知道的 ...

  10. C#编程(七十三)----------浅析C#中内存管理

    浅析C#中内存管理 前言:个人觉得C#吸收了各种语言的优点,可谓集大成者,但是不知但,这种集所有语言于一身的情况是好是坏.C#编程的一个优点就是程序员不需要关心具体的内存管理,尤其是垃圾收集器会处理所 ...