24.Letter Combinations of a Phone Number(电话号对应的字符组合)
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(电话号对应的字符组合)的更多相关文章
- [LintCode] Letter Combinations of a Phone Number 电话号码的字母组合
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- 69. Letter Combinations of a Phone Number
Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...
- 【leetcode】Letter Combinations of a Phone Number
Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...
- [LeetCode][Python]17: Letter Combinations of a Phone Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...
- Letter Combinations of a Phone Number:深度优先和广度优先两种解法
Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...
- 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 ...
- 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- Letter Combinations of a Phone Number - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Letter Combinations of a Phone Number - LeetCode 注意点 可以不用按字典序排序 解法 解法一:输入的数字逐 ...
- LeetCode: Letter Combinations of a Phone Number 解题报告
Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...
随机推荐
- MAVEN学习总结1
一.安装Maven插件 下载下来的maven插件如下图所示:,插件存放的路径是:E:/MavenProject/Maven2EclipsePlugin
- 求正整数n的所有因子
因子的概念:假如整数n除以m,结果是无余数的整数,那么我们称m就是n的因子. 需要注意的是,唯有被除数,除数,商皆为整数,余数为零时,此关系才成立.反过来说,我们称n为m的倍数. 求一个正整数n的所有 ...
- python子进程模块subprocess详解与应用实例 之二
1.2. Popen 对象 Popen类的实例有下列方法: 1. Popen.poll() 检查子进程是否已经结束,设置并返回返回码值. 2. Popen.wait() 等待子进程结束,设置并返回返回 ...
- 开坑数位dp
[背景] 在10月3日的dp专练中,压轴的第6题是一道数位dp,于是各种懵逼. 为了填上这个留存已久的坑,蒟蒻chty只能开坑数位dp了. [例题一][HDU2089]不要62 题目大意:给你一个区间 ...
- 【bzoj1024】[SCOI2009]生日快乐
1024: [SCOI2009]生日快乐 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 2372 Solved: 1717[Submit][Statu ...
- 对输入字符进行HTML转义 OR 去HTML标签
/** * 对输入字符进行HTML转义 * @param mixed $data */ public static function escape($data) { if(is_array($data ...
- Django ——Timezone 处理
Django ——Timezone 处理 https://blog.csdn.net/qq_37049781/article/details/79347278 2018年02月22日 14:50:24 ...
- Oracle——单行函数
两种 SQL 函数 单行函数 字符函数 大小写控制函数 SELECT employee_id, last_name, department_id FROM employees WHERE last_n ...
- Robot Framework - 基础关键字 BuiltIn 库(一)
今天给大家分享的是Robot Framework 机器人框架中 BuiltIn 基础库的使用...BuiltIn 库里面提供了很多基础方法助力于我们在自动化测试领域中做的更好!——本系列教程是教会大家 ...
- Struts2 让跳转指定执行某个方法
很多时候,我们想让jsp页面中的某个超链接,点击后执行后台的某个方法,里面该如何做呢? 这里方法很多种 我举例两种: 1.在struts.xml配置,配置如下: <package name=&q ...