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 ...
随机推荐
- elasticsearch 2.4 windows版jvm内存设置
本文编写目的是因为网上有很多es修改内存配置的文章,方法也各有不同,但在我的情况下(es 2.4 windows版)发现很多方法都是无效的,有效只有以下方法 第一个是xms,第二个是xmx
- UNITY_MATRIX_MVP和UnityObjectToClipPos
在unity5.6以上版本中,shader中的UNITY_MATRIX_MVP将会被UnityObjectToClipPos替代,以后我们在写顶点函数时就是这样的 v2f vert(appdata v ...
- <转>Spring 知识点提炼
Spring 知识点提炼 1. Spring框架的作用 轻量:Spring是轻量级的,基本的版本大小为2MB 控制反转:Spring通过控制反转实现了松散耦合,对象们给出它们的依赖,而不是创建或查找依 ...
- 如何处理错误消息Please install the gcc make perl packages
如何处理这行错误消息? Please install the gcc make perl packages from your distribution. 执行命令行:yum install gcc ...
- Android(java)学习笔记109:Java中输入和输出流概念
程序在内存中运行,文件在磁盘上,把文件从磁盘上读入内存中来,当然是输入流了, 反之,把内存中的数据写到磁盘上的文件里面去就是输出.通常都是这样的,用了过滤流的情况复杂一些,则另当别论.
- 激光推送报错:APNs is not available,please check your provisioning profile and certification 和 设置别名问题 app not registed, give up set tag:
前几天,项目中用到了推送功能,就集成了激光,遇到了2个问题,就给大家分享一下, 第一个问题: 在集成的过程是按照激光的文档做的,但是最后配置完了,一运行,就打印出这么一句话, APNs is not ...
- 团队作业-Beta冲刺(周三)
这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass1 这个作业要求在哪里 https://edu.cnblo ...
- Django 从0开始创建一个项目
title: Django 从0开始创建一个项目 tags: Django --- Django 从0开始创建一个项目 创建Django工程及配置 创建工程:django-admin starproj ...
- DROP GROUP - 删除一个用户组
SYNOPSIS DROP GROUP name DESCRIPTION 描述 DROP GROUP 从数据库中删除指定的组.组中的用户不被删除. 组中的用户不被删除. PARAMETERS 参数 n ...
- 《毛毛虫团队》第八次团队作业:ALPHA冲刺
一:实验名称:软件测试与ALPHA冲刺 二:实验目的与要求 (1)掌握软件测试基础技术. (2)学习迭代式增量软件开发过程(Scrum). 三:实验步骤 任务一:各个成员今日完成的任务: 任务二:明日 ...