lintcode 中等题:Letter Combinations of a Phone Number 电话号码的字母组合
题目
给一个数字字符串,每个数字代表一个字母,请返回其所有可能的字母组合。
下图的手机按键图,就表示了每个数字可以代表的字母。
给定 "23"
返回 ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]
以上的答案是按照词典编撰顺序进行输出的,不过,在做本题时,你也可以任意选择你喜欢的输出顺序。
解题
无法理解答案
回溯法,表示无法理解
public class Solution {
/**
* @param digits A digital string
* @return all posible letter combinations
*/
public ArrayList<String> letterCombinations(String digits) {
// Write your code here
HashMap<Integer,String> map = new HashMap<Integer,String>();
map.put(0,"");
map.put(1,"");
map.put(2,"abc");
map.put(3,"def");
map.put(4,"ghi");
map.put(5,"jkl");
map.put(6,"mno");
map.put(7,"pqrs");
map.put(8,"tuv");
map.put(9,"wxyz");
ArrayList<String> result = new ArrayList<String>();
if(digits == null || digits.length() ==0 )
return result;
ArrayList<Character> tmp = new ArrayList<Character>();
numtoString(digits,tmp,result,map);
return result; }
public void numtoString(String digits,ArrayList<Character> tmp,ArrayList<String> result,HashMap<Integer,String> map){
if( digits.length() ==0){
char[] arr = new char[tmp.size()];
for(int i=0 ;i< tmp.size(); i++){
arr[i] = tmp.get(i);
}
result.add(String.valueOf(arr));
return;
}
Integer curr = Integer.valueOf(digits.substring(0,1));
String letters = map.get(curr);
for(int i = 0;i< letters.length() ;i++){
tmp.add(letters.charAt(i));
numtoString(digits.substring(1),tmp,result,map);
tmp.remove(tmp.size() -1);
}
}
}
lintcode 中等题: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 ...
- [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-9 inclusive, return all possible letter combinations that th ...
- [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, ...
- 017 Letter Combinations of a Phone Number 电话号码的字母组合
给定一个数字字符串,返回数字所有可能表示的字母组合. 输入:数字字符串 "23"输出:["ad", "ae", "af" ...
- Leetcode17.Letter Combinations of a Phone Number电话号码的字母组合
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" 输出:[&quo ...
- leetcode第18题--Letter Combinations of a Phone Number
Problem: Given a digit string, return all possible letter combinations that the number could represe ...
- LeetCode OJ:Letter Combinations of a Phone Number(数字字母组合)
Given a digit string, return all possible letter combinations that the number could represent. A map ...
随机推荐
- Linux下mysql的安装和使用(C语言)
1 mysql的安装 我使用的ubuntu在线安装,非常简单,命令为: sudo apt-get install mysql-client mysql-server 2 mysql命令集合 网络太多了 ...
- 转:JAVA中this用法小结
转:http://blog.sina.com.cn/s/blog_6a6badc90100t8hm.html#SinaEditor_Temp_FontName Java关键字this只能用于方法方法体 ...
- jQuery WIN 7透明弹出层效果
jQuery WIN 7透明弹出层效果,点击可以弹出一个透明层的jquery特效,插件可以调弹出框的宽度和高度,很不错的一个弹出层插件. 适用浏览器:IE8.360.FireFox.Chrome.Sa ...
- 重拾C,一天一点点
数据类型及长度 char 字符型,占用一个字节 int 整型,通常代表特定机器中整数的自然长度 short 16位 int 16位或32位 ...
- 《PHP和MySQL Web开发》精彩的地方收录
1.用SESSION来做的购物车,做成数组,用isbn对应书的数量作为二维数组保存 $new GET传值加入购物车,submit是修改数量,提交后的表单,通过历遍原来的数组,对应isbn修改最新的数量 ...
- 如何在C#中实现图片缩放
//下面给出三个简单的方法,后面两个方法是扩展,估计有时用得着 //************************************************************// /// ...
- Use a layout_width of 0dip instead of wrap_content for better performance.......【Written By KillerLegend】
当你在一个Linearlayout布局中只为一个组件设置android:layout_weight属性时,那么这个组件将默认填充Linearlayout的剩余空间(宽度与高度方向),而不用事先进行测量 ...
- haproxy 安装与配置文件详解
本文主要阐述haproxy的安装配置详解,对于它的概念,作用,功能,和其它LB软件的区别,优点,缺点等不再进行说明. 一. haproxy 的安装配置 # cat /etc/redhat-releas ...
- 使用IE浏览器下载时候窗口一闪而过
使用IE浏览器下载东西时,窗口一闪而过,那么这个问题怎么处理呢? 解决办法: 1.按住ctrl键进行下载 2.浏览器>工具>internet选项>安全自定义级别>下载文件自动提 ...
- Uploadify 3.2 上传图片
uploadify version: uploadify 3.2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional// ...