题目

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. 解决apache上访问 cgi脚本时总是在网页中显示出脚本的源代码而不是执行结果的问题

    apache是支持cgi脚本的,但是需要保证四个条件: 1.放置cgi脚本的文件夹本身需要对apache服务器这个用户(一般默认用户名是www,linux下的用户机制请自行百度)开放x(即可执行)权限 ...

  2. 【2017 4 24 - B】 组合数

    [题目描述] 输入格式: 一行一个正整数n 输出格式: 一行一个数f(n)对1000000007取余的值 [分析] 就是乱搞?? 就是问根到叶子有多少条路径嘛. 然后路径可以π.1.1.π...这样表 ...

  3. 8.5 正睿暑期集训营 Day2

    目录 2018.8.5 正睿暑期集训营 Day2 总结 A.占领地区(前缀和) B.配对(组合) C 导数卷积(NTT) 考试代码 T1 T2 T3 2018.8.5 正睿暑期集训营 Day2 时间: ...

  4. BZOJ2924 : [Poi1998]Flat broken lines

    首先旋转坐标系 $x'=x-y$ $y'=-x-y$ 则对于一个点,它下一步可以往它左上角任意一个点连线. 根据Dilworth定理,答案=这个偏序集最长反链的长度. 设f[i]为到i点为止的最长反链 ...

  5. django orm 优化

    .markdown-body hr::after,.markdown-body::after{clear:both}.loopLine,.messageLine0{marker-end:"u ...

  6. Educational Codeforces Round 14 A. Fashion in Berland 水题

    A. Fashion in Berland 题目连接: http://www.codeforces.com/contest/691/problem/A Description According to ...

  7. .Net中常用的重要的第三方组件

    RSS.NET.dll RSS.NET是一款操作RSS feeds的开源.NET类库.它为解析和编写RSS feeds提供了一个可重用的对象模型.它完全兼容RSS 0.90, 0.91, 0.92, ...

  8. 零宽断言 -- Lookahead/Lookahead Positive/Negative

    http://www.vaikan.com/regular-expression-to-match-string-not-containing-a-word/ 经常我们会遇到想找出不包含某个字符串的文 ...

  9. Windows 2008 R2防火墙设置运行被ping通

    参考文献: http://huobumingbai.blog.51cto.com/1196746/323896/

  10. 如何:声明、实例化和使用委托(C# 编程指南)

    委托的声明如下所示: C#   public delegate void Del<T>(T item); public void Notify(int i) { } C#   Del< ...