LeetCode: Letter Combinations of a Phone Number 解题报告
Letter Combinations of a Phone Number
Given a digit string, 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.
Input:Digit string "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.

SOLUTION 1:
经典递归模板。
注意这一行不要写错了,根据当前的数字来取得可能的字母组合:
//get the possiable selections.
String s = map[digits.charAt(index) - '0'];
public class Solution {
String[] map = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
public List<String> letterCombinations(String digits) {
List<String> ret = new ArrayList<String>();
if (digits == null) {
return ret;
}
dfs(digits, new StringBuilder(), ret, 0);
return ret;
}
public void dfs(String digits, StringBuilder sb, List<String> ret, int index) {
int len = digits.length();
if (index == len) {
ret.add(sb.toString());
return;
}
// get the possiable selections.
String s = map[digits.charAt(index) - '0'];
for (int i = 0; i < s.length(); i++) {
sb.append(s.charAt(i));
dfs(digits, sb, ret, index + 1);
sb.deleteCharAt(sb.length() - 1);
}
}
}
GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/combination/LetterCombinations.java
LeetCode: Letter Combinations of a Phone Number 解题报告的更多相关文章
- [LeetCode]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 电话号码的字母组合
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- LeetCode——Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- [LeetCode] Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- [LeetCode] Letter Combinations of a Phone Number(bfs)
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- LeetCode Letter Combinations of a Phone Number (DFS)
题意 Given a digit string, return all possible letter combinations that the number could represent. A ...
- [LeetCode] Letter Combinations of a Phone Number 回溯
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- LeetCode Letter Combinations of a Phone Number 电话号码组合
题意:给一个电话号码,要求返回所有在手机上按键的组合,组合必须由键盘上号码的下方的字母组成. 思路:尼玛,一直RE,题意都不说0和1怎么办.DP解决. class Solution { public: ...
- leetcode Letter Combinations of a Phone Number python
class Solution(object): def letterCombinations(self, digits): """ :type digits: str : ...
随机推荐
- 【安卓】给gallery内"控件"挂载事件,滑动后抬起手指时也触发事件(滑动时不应触发)的解决、!
思路: 1.gallery内控件挂载事件(如:onClickListener)的方法类似listview,可直接在baseAdapter.getView内给控件挂载(详细方法百度). 2.貌似没问题, ...
- HDUOJ----John
John Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submis ...
- 彻底理解Python切片
关于list的insert函数 list#insert(ind,value)在ind元素前面插入value 首先对ind进行预处理:如果ind<0,则ind+=len(a),这样一来ind就变成 ...
- Python 随机数 random
1. Python seed() 函数 seed() 方法改变随机数生成器的种子,可以在调用其他随机模块函数之前调用此函数. seed( )是不能直接访问的,需要导入 random 模块,然后 ...
- PHP5.3新特性
1.首先对之前滥用的语法进行了规范 众所周知PHP在语言开发过程中有一个很好的容错性,导致在数组或全局变量中包含字符串不使用引号是可以不报错的,很多业余的开发者因为懒惰而产生的安全问题十分严重,之所以 ...
- SQL Server 2005/2008遍历所有表更新统计信息
DECLARE UpdateStatisticsTables CURSOR READ_ONLY FOR 02 SELECT sst.name, 03 Schema_name(ss ...
- ASP.NET CORE 学习之自定义异常处理
为什么异常处理选择中间件? 传统的ASP.NET可以采用异常过滤器的方式处理异常,在ASP.NET CORE中,是以多个中间件连接而成的管道形式处理请求的,不过常用的五大过滤器得以保留,同样可以采用异 ...
- FA_固定资产六大业务增加、修改、报废、在建、折旧、盘点概述(概念)
2014-06-04 Created By BaoXinjian
- C#模拟PrtScn实现截屏
有了之前的基础知识了解,如今開始实现PrtScn和Alt+PrtScn. 首先新建一个WPF应用程序,命名为PrintscreenAndAltPrintScreen 导入keybd_event方法: ...
- POJ 3254 简单状压DP
没什么可说的,入门级状压DP.直接撸掉 #include <iostream> #include <cstring> #include <cstdlib> #inc ...