Java [leetcode 17]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.
解题思路:
递归法解题之。从第一个字符串开始确认结果,然后递归地确认余下各项结果。
参考博客:http://blog.csdn.net/china_wanglong/article/details/38495355
代码如下:
public class Solution {
public List<String> letterCombinations(String digits) {
List<String> result = new ArrayList<String>();
String[] map = new String[] { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
char[] tmp = new char[digits.length()];
if(digits.length() == 0)
return result;
rec(digits, 0, tmp, map, result);
return result;
}
public void rec(String digits, int index, char[] tmp, String[] map, List<String> result){
if(index == digits.length()){
result.add(new String(tmp));
return;
}
char tmpChar = digits.charAt(index);
for(int i = 0; i < map[tmpChar - '0'].length(); i++){
tmp[index] = map[tmpChar - '0'].charAt(i);
rec(digits, index + 1, tmp, map, result);
}
}
}
Java [leetcode 17]Letter Combinations of a Phone Number的更多相关文章
- Leetcode 17. Letter Combinations of a Phone Number(水)
17. Letter Combinations of a Phone Number Medium Given a string containing digits from 2-9 inclusive ...
- [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
1 题目: Given a digit string, return all possible letter combinations that the number could represent. ...
- Leetcode 17.——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-9 inclusive, return all possible letter combinations that th ...
- [LeetCode] 17. 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-9 inclusive, return all possible letter combinations that th ...
- LeetCode 17 Letter Combinations of a Phone Number (电话号码字符组合)
题目链接 https://leetcode.com/problems/letter-combinations-of-a-phone-number/?tab=Description HashMap< ...
- LeetCode——17. Letter Combinations of a Phone Number
一.题目链接: https://leetcode.com/problems/letter-combinations-of-a-phone-number/ 二.题目大意: 给定一段数字字符串,其中每个数 ...
随机推荐
- 关于Shell中命令替换$(...)与后置引用`...`的不同
命令替换 在bash中,$( )与` `(反引号)都是用来作命令替换的.命令替换与变量替换差不多,都是用来重组命令行的,先完成引号里的命令行,然后将其结果替换出来,再重组成新的命令行. $( )与`` ...
- 【BZOJ 1295】 [SCOI2009]最长距离
Description windy有一块矩形土地,被分为 N*M 块 1*1 的小格子. 有的格子含有障碍物. 如果从格子A可以走到格子B,那么两个格子的距离就为两个格子中心的欧几里德距离. 如果从格 ...
- setTimeOut传参数(转)
无论是window.setTimeout还是window.setInterval,在使用函数名作为调用句柄时都不能带参数.带参数则立马执行,没有延时效果.可通过下面方式实现. <script ...
- windows server 2008 r2电脑历史操作记录
1.看计算机哪天运行过. 在系统盘下的Windows\Tasks文件夹下找到文件SCHEDLGU.TXT. 2.看你最近打开过什么文件(非程序)或者文件夹 开始-->运行--> ...
- 关于安装Android Studio的一些问题的解决方法
问题1:每次Fetching android sdk component information 这是在检查你的 Android SDK .有人会在这里卡上很长时间,很大的原因就是:网络连接有问题.可 ...
- asp.net web api 开发时应当注意的事项
Self referencing when returning chain of objects. This can be solved using a design pattern called t ...
- 贪心算法——将正整数变为1
题目链接http://toutiao.com/a6320936270101528833/ 为避免链接失效,再粘贴一下题目内容: 给你一个数n,有3种操作: 1.这个数加1 2.这个数减1 3.如果这个 ...
- LDPY Ghost Win7 64位 纯净自选版 V5.0
★ 概述: ☆ 源安装盘是[Windows7_SP1_ULTIMATE]微软官方SP1正式版. ☆ 破解激活WIN7,补丁更新至2013/06/18所有系统安全关键补丁. ☆ 破解 Windows 7 ...
- php继承与重载
<?php class A { public $param = "paramA"; public function test() { echo "testA&quo ...
- linux 2.6 互斥锁的实现-源码分析
http://blog.csdn.net/tq02h2a/article/details/4317211 看了看linux 2.6 kernel的源码,下面结合代码来分析一下在X86体系结构下,互斥锁 ...