题目:

给定一个数字字符串,返回数字所能代表的所有字母组合;

举例:

Input:Digit string "23"

Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

解题思路:

1. 首先要判断给定的数字字符串是否合法(isDigit()方法),即是否只包含0~9的数字,如果还包含其他,则直接返回空;

2. 其次要将每个电话号码所对应的字符串保存起来,这里我使用的是HashMap,key是数字,value是数字所对应的字符串;

3. 采用递归的方式将数字所代表的字母连接起来;举例:234,则对应的是abc,def,ghi,此时首先选择a,然后选择d,然后选择g,得到一个结果adg,选择h,得到一个结果adh,选择i,得到结果adi;选择e.选择g得到结果aeg,以此类推......

代码如下:

 public class Solution {
public List<String> letterCombinations(String digits) {
List<String> res = new ArrayList<String>();
if(digits == null || digits.length() < 1){
return res;
}
if(!isDigit(digits)) return res;
HashMap<Character, String> hm = new HashMap<Character, String>();
hm.put('0', "");
hm.put('1', "");
hm.put('7', "pqrs");
hm.put('8', "tuv");
hm.put('9', "wxyz");
String str = "abcdefghijklmno";
int i = 2;;
int j = 0;
while(i < 7){
hm.put((char)(i + '0'), str.substring(j, j + 3));
j = j + 3;
i++;
}
char[] ch = new char[digits.length()];
isCal(digits, 0, ch, res, hm);
return res;
}
// 判断给定的数字字符串是否合法,不合法,统一返回空
public boolean isDigit(String digits){
for(int i = 0; i < digits.length(); i++){
if(digits.charAt(i) < '0' || digits.charAt(i) >'9')
return false;
}
return true;
}
    // index代表的是第几个数字
public void isCal(String digits, int index, char[] ch, List<String> res, HashMap<Character, String> hm){
if(index == digits.length()){ // 如果成立,表明已经连接到最后一个数字了,因此要将结果加入res
res.add(new String(ch));
return;
}
char c = digits.charAt(index);
for(int i = 0; i < hm.get(c).length(); i++){ //
ch[index] = hm.get(c).charAt(i); // 获取index所对应数字的字符串中的字符
isCal(digits, index + 1, ch, res, hm); // 获取下一个数字所对应的字符串中的字符
} }
}


Leetcode17--->Letter Combinations of a Phone Number(电话号码对应的字母的组合)的更多相关文章

  1. Leetcode17.Letter Combinations of a Phone Number电话号码的字母组合

    给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" 输出:[&quo ...

  2. [LintCode] Letter Combinations of a Phone Number 电话号码的字母组合

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

  3. 算法练习--LeetCode--17. Letter Combinations of a Phone Number

    Letter Combinations of a Phone NumberMedium Given a string containing digits from 2-9 inclusive, ret ...

  4. [LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合

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

  5. [LeetCode] 17. Letter Combinations of a Phone Number 电话号码的字母组合

    Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...

  6. 【LeetCode】17. Letter Combinations of a Phone Number 电话号码的字母组合

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:电话号码, 字母组合,回溯法,题解,leetcode, ...

  7. LeetCode17 Letter Combinations of a Phone Number

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

  8. lintcode 中等题:Letter Combinations of a Phone Number 电话号码的字母组合

    题目 电话号码的字母组合 给一个数字字符串,每个数字代表一个字母,请返回其所有可能的字母组合. 下图的手机按键图,就表示了每个数字可以代表的字母. 样例 给定 "23" 返回 [& ...

  9. [LeetCode]17. Letter Combinations of a Phone Number电话号码的字母组合

    Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...

随机推荐

  1. 对flex-grow和flex-shrink的深入理解

    flex弹性布局,如果子元素宽度之和大于或者小于父元素宽度,空间就会存在剩余和不够,flex默认不换行,除非设置flex-wrap,那么这种情况下,有两个重要的属性,flex-grow和flex-sh ...

  2. 织梦dedecms手机版上下篇链接错误的解决方法

    打开 \include\arc.archives.class.php 1. 找到 $this->PreNext['pre'] = "上一篇:<a href='$mlink'> ...

  3. 31全志r58平台Android4.4.2下打开USB摄像头

    31全志r58平台Android4.4.2下打开USB摄像头 2018/10/26 16:00 版本:V1.0 开发板:SC5806 1.系统编译:(略) 2.需要修改的文件: W:\r58_andr ...

  4. 造成socket.error: [Errno 99] Cannot assign requested

    socket.error: [Errno 99] Cannot assign requested address 网上你去搜,基本都是说bind的时候,地址已经被用了,都是胡扯.地址被用报的错误应该是 ...

  5. freebsd快速删除磁盘数据

    At the start, mark all system disks as empty. Repeat the following command for each hard drive: dd i ...

  6. reactnative资源

    http://facebook.github.io/react-native/docs/getting-started.html

  7. Python 中 创建类方法为什么要加self

    Python的类方法和普通的函数有一个明显的区别,在类的方法必须有一个额外的第一个参数(self),但在调用这个方法的时候不必为这个参数数值(显胜于隐的引发).在Python的类方法中这个特别的参数指 ...

  8. HDU 3652 B-number (数位DP,入门)

    题意: 如果一个整数能被13整除,且其含有子串13的,称为"B数",问[1,n]中有多少个B数? 思路: 这题不要用那个DFS的模板估计很快秒了. 状态设计为dp[位数][前缀][ ...

  9. Logback文档(1)

    http://b6ec263c.wiz03.com/share/s/2SX2oY0nX4f32CY5ax1bapaL030VCK2svQZU2rRyDR05KMh5

  10. MySQL基础教程——创建数据库并插入数据

    本节将介绍 MySQL 新建数据库,新建表,插入数据以及基本数据类型的相关知识.本节实验将创建一个名为 mysql_shiyan 的数据库,其中有两张表 employee和 department. 1 ...