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. Asp.net Web Application 打开 SharePoint 2010 Site 错误 The Web application at could not be found

    解决办法如下: 1. 修改项目的.net framework 为3.5 2. Application Pool 选用 Sharepoint App pool 3. 修改 web.config如下: & ...

  2. java Web 监听器Listener详解

    简介 JavaWeb中的监听器是Servlet规范中定义的一种特殊类,它用于监听web应用程序中的ServletContext.HttpSession和 ServletRequest这三大域对象的创建 ...

  3. 刷题向》DP》值得一做》关于对DP问题的充分考虑(normal)

    在你辛苦调试一道DP题,遇到瓶颈的时候,你是否感到一股洪荒之力遏制住你的思想,使你给题库贡献了一倍的WA.RE.TLE量,却没有AC过一次? 在这时,你应该考虑的是砸电脑再次重新考虑整个题目,再应对自 ...

  4. 【bzoj1016】[JSOI2008]最小生成树计数

    1016: [JSOI2008]最小生成树计数 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 4863  Solved: 1973[Submit][St ...

  5. 【总结整理】AXURE原件

    iphone:750*1334 一般用分辨率的一半 移动的时候,按住shift拖动,可水平移动 框选的时候,箭头选择包含模式,只要不全部包含进来,就不会被选中 ctrl+'=显示背景网格 ctrl+s ...

  6. HTTP 协议中 URI 和 URL 有什么区别?

    HTTP 协议中 URI 和 URL 有什么区别? HTTP = Hyper Text Transfer ProtocolURI = Universal Resource IdentifierURL ...

  7. java就业指南 zookeeper分布式系统 zookeeper实现分布式锁 有用

    目前几乎很多大型网站及应用都是分布式部署的,分布式场景中的数据一致性问题一直是一个比较重要的话题.分布式的CAP理论告诉我们“任何一个 分布式系统都无法同时满足一致性(Consistency).可用性 ...

  8. GridView删除行

    在GridView绑定数据的时候需要设置该GridView的主键值,设置的这个主键与取出来的数据的一个字段对应.比如,取出来的数据表中有个ID的字段,那设这个ID为该GridView的主键是比较好的. ...

  9. Go程序设计3——并发编程

    1 channel 一般channel的声明形式为: var chanName chan ElementType 与一般的变量声明不同的地方仅仅是在类型之前增加了chan关键字.ElementType ...

  10. (转)什么?你还不会写JQuery 插件

    原文地址:http://www.cnblogs.com/joey0210/p/3408349.html 前言 如今做web开发,jquery 几乎是必不可少的,就连vs神器在2010版本开始将Jque ...