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 ...
随机推荐
- 获取SilverLight.Web项目中路径Uri
方法一: //获取指定要呈现的xaml内容的包活xaml文件Uri var strFullUrl = Application.Current.Host.Source.AbsoluteUri; if ( ...
- Sending Email from mailx Command in Linux Using Gmail’s SMTP
The mailx or mail command in Linux is still providing service for guys like me, especially when we n ...
- C#中linq
class IntroToLINQ { static void Main() { // The Three Parts of a LINQ Query: // 1. Data source. ] { ...
- Oracle 表的访问方式(2)-----索引扫描
索引扫描(Index scan) 我们先通过index查找到数据对应的rowid值(对于非唯一索引可能返回多个rowid值),然后根据rowid直接从表中得到具体的数据,这种查找方式称为索引扫描或索引 ...
- Python-Day7 面向对象进阶/异常处理/Socket
一.面向对象高级语法部分 1.静态方法 通过@staticmethod装饰器即可把其装饰的方法变为一个静态方法,什么是静态方法呢?其实不难理解,普通的方法,可以在实例化后直接调用,并且在方法里 ...
- 动态模板中 SWIPER 划不动问题
原文: 地址:http://hao.jser.com/archive/8030/ 作者:segmentfault 问题: 动态循环生成swiper-slide类,在swiper-wrapper里生成6 ...
- Bluetooth
Android provides a default Bluetooth stack, BlueDroid, that is divided into two layers: The Bluetoot ...
- 使用libuv实现生产者和消费者模式
生产者和消费者模式(Consumer + Producer model) 用于把耗时操作(生产线程),分配给一个或者多个额外线程执行(消费线程),从而提高生产线程的响应速度(并发能力) 定义 type ...
- iOS 10的23个隐藏新特性-b
上周iOS 10正式版推送后,24小时的更新率已经超过15%,实在惊人.虽然有着初期变砖.5S6卡顿.移动VoLTE无法使用.美版无信号等BUG,但不可忽视的是,iOS 10还是带来了很多从前没有的功 ...
- android 开启或者隐藏软键盘
一. 隐藏软键盘方法一(注:此方法本人使用时发现isActivie()失效,建议还是用其他方法..): InputMethodManager imm = (InputMethodManager)get ...