题目

给一个数字字符串,每个数字代表一个字母,请返回其所有可能的字母组合。

下图的手机按键图,就表示了每个数字可以代表的字母。

样例

给定 "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 电话号码的字母组合的更多相关文章

  1. [LintCode] Letter Combinations of a Phone Number 电话号码的字母组合

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  2. [LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  3. [LeetCode]17. Letter Combinations of a Phone Number电话号码的字母组合

    Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...

  4. [LeetCode] 17. Letter Combinations of a Phone Number 电话号码的字母组合

    Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...

  5. 【LeetCode】17. Letter Combinations of a Phone Number 电话号码的字母组合

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:电话号码, 字母组合,回溯法,题解,leetcode, ...

  6. 017 Letter Combinations of a Phone Number 电话号码的字母组合

    给定一个数字字符串,返回数字所有可能表示的字母组合. 输入:数字字符串 "23"输出:["ad", "ae", "af" ...

  7. Leetcode17.Letter Combinations of a Phone Number电话号码的字母组合

    给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" 输出:[&quo ...

  8. leetcode第18题--Letter Combinations of a Phone Number

    Problem: Given a digit string, return all possible letter combinations that the number could represe ...

  9. LeetCode OJ:Letter Combinations of a Phone Number(数字字母组合)

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

随机推荐

  1. ORACLE 语句关联统计

    很久不用SQL语句了,貌似入职新公司后,又回归到了三年前的SQL时代,一写一坨的SQL好吧,也当回归一下过去的知识. 下面是统计2月份某数据的计费统计 select t.telno as 主号,VID ...

  2. mongodb的常用操作(二)

    继续mongodb的学习: 9.mongodb条件查询 假设有user集合,里面结构如下:{ "_id" : ObjectId("52ab35d281181f853264 ...

  3. PHP学习之数组的定义和填充

    数组就是把一组数据按顺序放在一起.PHP的数组和其它的语言数组有一点点不同:第一,保存的数据是可以是任何类型的:第二,数组的索引可以是数字,也可以是字符串. PHP的数组,说白了,就是关联数据每一条数 ...

  4. 集成环境wamp环境下 memcached的安装

    早就听说过memcached,但是一直没实践过.所有今天有时间就搞了一下,哎废了我一上午才搞定!一上午啊,好丢人.特写记录一下 先说一下我遇到的问题:按照别人教程(还有好多)说的,安装后没有任何的错误 ...

  5. PHP版本中的VC6,VC9,VC11,TS,NTS区别

    以windows为例,看看下载到得php zip的文件名 php-5.4.4-nts-Win32-VC9-x86.zip VC6:legacy Visual Studio 6 compiler,是使用 ...

  6. tomcat6.0 数据库连接池配置问题

    tomcat6.0 数据库连接池配置问题: 连接池配好后,启动tomat后,输入项目系统的登录名和密码,报 Cannot create JDBC driver of class '' for conn ...

  7. MySQL SHOW 语句大全

    常用的mysql show命令如下: 1.show databases;        显示mysql中所有数据库 2.show tables [from databases] ;     显示当前数 ...

  8. [原创]CentOS6.4编译安装Facebook的folly库(gcc4.8.1boost1.5.3)

      Folly: Facebook Open-souce LibrarY,Facebook开源的一个基础组件库,据说在大规模的场景中性能较高.目前因为自己负责的系统有几个地方性能较差,因此特意找来看看 ...

  9. jquery ztree插件

    jquery json数据格式 操作tree数据 http://www.ztree.me/v3/main.php#_zTreeInfo

  10. 多路选择器(multiplexer)简介

    1.多路器简介 简称:多路器 功能:多输入  单输出   组合逻辑电路 2.verilog代码实现: module Mux_8(addr,in1,in2,in3,in4,in5,in6,in7,in8 ...