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. java代码优化29个点

    通过java代码规范来优化程序,优化内存使用情况,防止内存泄露 可供程序利用的资源(内存.CPU时间.网络带宽等)是有限的,优化的目的就是让程序用尽可能少的资源完成预定的任务.优化通常包含两方面的内容 ...

  2. Python pip命令

    一.简介 pip 是一个安装和管理 Python 包的工具 , 是 easy_install 的一个替换品.distribute是setuptools的取代(Setuptools包后期不再维护了),p ...

  3. BOOL运算符号(从C#入门经典第五版中摘录)

    只总结自己觉得难的哈: (1) var1=!var2;    //(非) (2) var1=var2&var3;    //(与) (3)var1=var2|var3;    //(或) (4 ...

  4. 19. AUTO INCREMENT 字段

    Auto-increment 会在新记录插入表中时生成一个唯一的数字. AUTO INCREMENT 字段 我们通常希望在每次插入新记录时,自动地创建主键字段的值. 我们可以在表中创建一个 auto- ...

  5. NEERC17 J Journey from Petersburg to Moscow

    CF上可以提交.   链接 依然是很妙的解法. 我们可以枚举每一个出现过的边权$L$,然后把所有边的边权减掉这个$L$,如果小于$L$就变为$0$,然后跑一遍最短路然后加上$k * L$更新答案即可. ...

  6. Servlet入门第一天

    1. 使用 JavaEE 版的 Eclipse 开发动态的 WEB 工程(JavaWEB 项目) 1). 把开发选项切换到 JavaEE 2). 可以在 Window -> Show View ...

  7. ByteUnit

    JDK里面有TimeUnit,看spark源码有个ByteUnit.这个类还是挺不错的. public enum ByteUnit { BYTE (1), KiB (1024L), MiB ((lon ...

  8. CodeBlocks调试功能(转)

    转自:迂者-贺利坚 http://blog.csdn.net/sxhelijian/article/details/15026159 示例代码: #include <iostream> u ...

  9. What is difference between 3-layer architecture and MVC architecture?

    By Vikas Singh on Sep 26, 2014 In 3-layer architecture  3-layer architecture separates the applicati ...

  10. JavaEE互联网轻量级框架整合开发(书籍)阅读笔记(1):Mybatis和Hibernate概念理解

    一.关键字说明: oop:面向对象 aop:面向切面 ioc:控制反转 orm:对象关系映射 pojo:数据库表映射的java实体类 二.常识说明:1.hibernate和mybatis都属于持久层. ...