Leetcode17--->Letter Combinations of a Phone Number(电话号码对应的字母的组合)
题目:
给定一个数字字符串,返回数字所能代表的所有字母组合;
举例:
![]()
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(电话号码对应的字母的组合)的更多相关文章
- Leetcode17.Letter Combinations of a Phone Number电话号码的字母组合
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" 输出:[&quo ...
- [LintCode] Letter Combinations of a Phone Number 电话号码的字母组合
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- 算法练习--LeetCode--17. Letter Combinations of a Phone Number
Letter Combinations of a Phone NumberMedium Given a string containing digits from 2-9 inclusive, ret ...
- [LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- [LeetCode] 17. Letter Combinations of a Phone Number 电话号码的字母组合
Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...
- 【LeetCode】17. Letter Combinations of a Phone Number 电话号码的字母组合
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:电话号码, 字母组合,回溯法,题解,leetcode, ...
- LeetCode17 Letter Combinations of a Phone Number
题意: Given a digit string, return all possible letter combinations that the number could represent. A ...
- lintcode 中等题:Letter Combinations of a Phone Number 电话号码的字母组合
题目 电话号码的字母组合 给一个数字字符串,每个数字代表一个字母,请返回其所有可能的字母组合. 下图的手机按键图,就表示了每个数字可以代表的字母. 样例 给定 "23" 返回 [& ...
- [LeetCode]17. Letter Combinations of a Phone Number电话号码的字母组合
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...
随机推荐
- 从typeof()说起
本文也同步发表在我的公众号“我的天空” 首先我们先思考一下,执行下列语句分别会显示什么? alert(typeof(Array)); alert(typeof(Array())); 我们进入正题! 在 ...
- 从0到1分步实现一个出生日期的正则表达式(JavaScript)
简言 在表单验证中,经常会用正则表达式做出生日期校验.本文把出生日期分割成几个部分,分步地介绍了实现一个出生日期校验的完整过程.相信您在理解了本篇的内容后,对如何编写和如何应用正则表达式会有进一步的理 ...
- The great pleasure in life is doing what people say you cannot do.
The great pleasure in life is doing what people say you cannot do. 人生最大的快乐是做到别人认为你做不到的事情.
- <Android HAL 之路> HAL 简介
HAL层概述 名称: HAL, Hardware Abstracting Layer,中文名字:硬件抽象层. 作用:对Linux内核驱动程序的封装,向上提供接口,屏蔽低层的实现细节.向上衔接Andro ...
- [总结] min-25筛
再不写总结我又会忘掉啊啊啊啊啊啊啊啊啊 这个\(min-25\)筛主要用来求一个积性函数的前缀和,就像这样\[\sum_{i=1}^n f(i)\] 不过这个积性函数要满足两个条件:质数\(p\)的函 ...
- pc端常见布局---垂直居中布局 单元素定高
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- POJ 3017 Cut the Sequence (单调队列优化DP)
题意: 给定含有n个元素的数列a,要求将其划分为若干个连续子序列,使得每个序列的元素之和小于等于m,问最小化所有序列中的最大元素之和为多少?(n<=105.例:n=8, m=17,8个数分别为2 ...
- 【Orange Pi Lite2】 ——1《如何开始使用开源硬件》
[Orange Pi Lite2] --1<如何开始使用开源硬件> 本文只在博客园发布 在开始前你需要准备的材料与软件 用户手册_Orange Pi Lite2 OrangePi_Lite ...
- mvc的help和functions语法
@helper show(int num ) { ) { @:存在 } else { @:不存在 } } @functions { /// <summary> /// 方法必须要求为静态 ...
- JS实现2,8,10,16进制的相互转换
// 10进制转为16进制 var a=1234567890; console.log(a.toString(16)) //499602d2 // 16进制转为10进制 var num=parseIn ...