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/ 二.题目大意: 给定一段数字字符串,其中每个数 ...
随机推荐
- ubuntu下Django的下载与安装(三种方法)
方法一: 1下载: 1 安装python,Linux系统下,一般是安装好的,可以输入如下命令查看 python -V 如果没有安装,则需要安装,安装方法如下,首先从官网下载源码,然后: (1) $ t ...
- poj 3415 Common Substrings 后缀数组+单调栈
题目链接 题意:求解两个字符串长度 大于等于k的所有相同子串对有多少个,子串可以相同,只要位置不同即可:两个字符串的长度不超过1e5; 如 s1 = "xx" 和 s2 = &qu ...
- 在win7上建立本地FTP站点详细步骤
一.安装FTP组件点击:控制面板—>程序和功能—>打开或关闭Windows功能. 勾选“FTP服务器”及“FTP服务”“FTP扩展性”,点击“确定”,安装FTP组件. 勾选Web管理工具的 ...
- 退出telnet
telnet时,很多时候通过ctrl+c依然无法退出.可以采取下面的方式进行退出: ctrl+],然后进入 telnet>,然后输入q或quit即可.
- Could not retrieve mirrorlist http://mirrorlist.centos.org || PYCURL ERROR 6
yum:Could not retrieve mirrorlist http://mirrorlist.centos.org || PYCURL ERROR 6 通过centos安装openldap的 ...
- sql查找最小缺失值与重用被删除的键(转载)
转载自:http://blog.csdn.net/yanghua_kobe/article/details/6262550 在数据处理时,我们经常会使用一些“自增”的插入方式来处理数据.比如学生学号: ...
- 【BZOJ 1010】 [HNOI2008]玩具装箱toy
Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为1... ...
- C# json to dynamic object
dynamic obj = Newtonsoft.Json.JsonConvert.DeserializeObject(json); string greeting = obj.greeting; R ...
- visio
为您的产品密钥: 7DNWX-MRX4G-QCGX2-DG6BP-DC8RP http://technet.microsoft.com/zh-cn/evalcenter/hh973399.aspx ...
- Ext.Ajax.request()方法和FormPanel.getForm().submit()方法,都返回success()方法的差异
我还是不发表到博客园首页吧,要不然还是要被取消,>_< 还是言归正传吧,关于Ext.Ajax.request()方法和FormPanel.getForm().submit()方法返回suc ...