题目:

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

举例:

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. 【selenium学习笔记一】python + selenium定位页面元素的办法。

    1.什么是Selenium,为什么web测试,大家都用它? Selenium设计初衷就是为web项目的验收测试再开发.内核使用的是javaScript语言编写,几乎支持所以能运行javaScript的 ...

  2. JavaScript空假值及其判断

    一.javaScript五种空值和假值 分别为undefined,null,false,"",0,这五个值的共同点是在执行if语句时都会执行false分支,执行对应的非语句的时候都 ...

  3. Windows 7, Visual Studio 2015下编译Webkit

    因工作需要,需要编译Windows版本的Webkit,中间走了不少弯路,都记录下来,供大家参考!也随时欢迎大家讨论(QQ群:345802342) 整个编译工作参考的是官方文档:https://webk ...

  4. 51nod 1191 消灭兔子

    题目来源: 2013腾讯马拉松赛第三场 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 有N只兔子,每只有一个血量B[i],需要用箭杀死免子.有M种不同类型的箭可以 ...

  5. iOS消息体系架构详解-融云即时通讯云

    iOS SDK 体系架构 本文档将详细介绍融云的 SDK 产品架构和消息体系,以便于您更深入的了解融云并更快速的开发自己的产品. 融云 SDK 系统架构 IMKit IMKit 的功能主要是封装各种界 ...

  6. Spark Job调优(Part 2)

    原文链接:https://wongxingjun.github.io/2016/05/11/Spark-Job%E8%B0%83%E4%BC%98-Part-2/ 这篇文章将会完成Part 1中留下的 ...

  7. CORS的原理及应用

    CORS的原理及应用 CORS是跨站资源共享,同样是解决浏览器的同源策略 其本质是设置响应头,使得浏览器允许跨域请求. 第三方网站返回数据的时候在浏览器的响应头中添加允许的域名,允许所有的用* 1 简 ...

  8. flowvisor连接ovs

    1 开启flowvisor $ sudo -u flowvisor fvconfig generate /etc/flowvisor/config.json $ sudo /etc/init.d/fl ...

  9. 关键字final

    final数据 对于一个final变量,如果是基本数据类型的变量,则其数值一旦在初始化之后便不能更改:如果是引用类型的变量,则在对其初始化之后便不能再让其指向另一个对象.再次赋值将引起编译报错. 当f ...

  10. 初识Mysql之基本简单语法总结

    一.  DDL(data definition language)语句:数据定义语言. 这些语句定义了不同的数据段.数据库.表.列.索引等数据库对象.常用语句关键字:create.drop.alter ...