Level:

  Medium

题目描述:

Given a string containing digits from 2-9 inclusive, 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. Note that 1 does not map to any letters.

Example:

Input: "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.

思路分析:

  建立hashmap将每个数字对应的字符映射存放在map中,利用递归回溯解决问题。

代码:

public class Solution{
public List<String>letterCombinations(String digits){
List<String>res=new ArrayList<>();
if(digits==null||digits.equals(""))
return res;
HashMap<Character,char[]>map=new HashMap<>();
map.put('0',new char[]{});
map.put('1',new char[]{});
map.put('2',new char[]{'a','b','c'});
map.put('3',new char[]{'d','e','f'});
map.put('4',new char[]{'g','h','i'});
map.put('5',new char[]{'j','k','l'});
map.put('6',new char[]{'m','n','o'});
map.put('7',new char[]{'p','q','r','s'});
map.put('8',new char[]{'t','u','v'});
map.put('9',new char[]{'w','x','y','z'});
StringBuilder str=new StringBuilder();
findComb(digits,map,res,str);
return res;
}
public void findComb(String digits,HashMap<Character,char[]>map,List<String>res,StringBuilder str){
if(str.length==digits.length){//str的长度等于digits的长度证明是其中一个结果
res.add(str.toString());
return;
}
for(char c:map.get(digits.charAt(str.length()))){
str.append(c);
findComb(digits,map,res,str);//递归回溯找出所有的结果
str.deleteCharAt(str.length()-1);//每找出一个结果后,str的长度减一,添加下一种可能
}
}
}

24.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. bash&nbsp;shell笔记4&nbsp;处理用…

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://twentyfour.blog.51cto.com/945260/521448 知 ...

  2. springmvc高级知识点

  3. 刷题向》关于一道像差分约束的数学题BZOJ1045(NORMAL)

    关于这道题,乍一看很像查分约束,但是实际上这道题是可以用数学方法直接解决的. 这道题在蓝书上有原题,可以看到题解,在此再赘述一遍 首先,最终每个小朋友的糖果数量可以计算出来,等于糖果总数除以n,用av ...

  4. 带你剖析WebGis的世界奥秘----瓦片式加载地图(转)

    带你剖析WebGis的世界奥秘----瓦片式加载地图 转:https://zxhtom.oschina.io/zxh/20160805.html  编程  java  2016/08/05 0留言,  ...

  5. dataview 组件使用示例

    来自<sencha touch 权威指南> ------------------------------- 例子1——app.js代码如下: Ext.require(['Ext.data. ...

  6. Row_Number() OVER()函数使用举例

    语法:ROW_NUMBER() OVER(PARTITION BY COLUMN ORDER BY COLUMN) 简单的说row_number()从1开始,为每一条分组记录返回一个数字,这里的ROW ...

  7. 如何杀死一个已经detached的screen会话?

    如果向杀死一个已经detached的screen会话,可以使用以下命令: screen -X -S seesion-id quit 举例如下: [root@localhost ~]# screen - ...

  8. LibreOJ 6279 数列分块入门 3(分块+排序)

    题解:自然是先分一波块,把同一个块中的所有数字压到一个vector中,将每一个vector进行排序.然后对于每一次区间加,不完整的块加好后暴力重构,完整的块直接修改标记.查询时不完整的块暴力找最接近x ...

  9. delphi 获取windows任务栏的高度

    function GetWinTrayWnd: Integer; // 获取windows任务栏高度 var TrayWnd: HWnd; //任务栏句柄 Rec : TRect; begin Tra ...

  10. web.xml配置及详解

    1.web.xml 是网络程序中的一个很重要的配置文件. 2.XML基础标准是为XML的进一步实用化制定的标准,它规定了采用XML制定标准时的一些公用特征.方法或规则.XML Schema描述了更加严 ...