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.


题解:用一个二维数组map保存数字到字母的映射,然后深度优先递归搜索如下的一棵树(以"23"为例,树不完整)

代码如下:

 public class Solution {
public List<String> letterCombinations(String digits) {
ArrayList<String> answer = new ArrayList<String>(); char[][] map = {{'a','b','c'},{'d','e','f'},{'g','h','i'},{'j','k','l'},{'m','n','o'},{'p','q','r','s'},{'t','u','v'},{'w','x','y','z'}};
String currentPath = new String(); DeepSearch(map, answer, digits, currentPath);
return answer;
} public void DeepSearch(char[][] map,List<String> answer,String digits,String currentPath){
if(digits.length() == 0)
{
String temp = new String(currentPath);
answer.add(temp); return;
} int now = digits.charAt(0) - '0';
for(int j = 0;j < map[now-2].length;j++){
currentPath += map[now-2][j];
DeepSearch(map, answer, digits.substring(1), currentPath);
currentPath = currentPath.substring(0, currentPath.length()-1);
}
}
}

【leetcode刷题笔记】Letter Combinations of a Phone Number的更多相关文章

  1. 刷题17. Letter Combinations of a Phone Number

    一.题目说明 题目17. Letter Combinations of a Phone Number,题目给了下面一个图,输入一个字符串包括2-9,输出所有可能的字符组合. 如输入23所有可能的输出: ...

  2. [刷题] 17 Letter Combinations of a Phone Number

    要求 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合 1 不对应任何字母    示例 输入:"23" 输出:["ad", "ae&q ...

  3. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  4. 18.9.10 LeetCode刷题笔记

    本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...

  5. LeetCode刷题笔记 - 12. 整数转罗马数字

    学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...

  6. Leetcode刷题笔记(双指针)

    1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...

  7. lintcode 中等题:Letter Combinations of a Phone Number 电话号码的字母组合

    题目 电话号码的字母组合 给一个数字字符串,每个数字代表一个字母,请返回其所有可能的字母组合. 下图的手机按键图,就表示了每个数字可以代表的字母. 样例 给定 "23" 返回 [& ...

  8. LeetCode (17)Letter Combinations of a Phone Number

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

  9. LeetCode刷题笔记(1-9)

    LeetCode1-9 本文更多是作为一个习题笔记,没有太多讲解 1.两数之和 题目请点击链接 ↑ 最先想到暴力解法,直接双循环,但是这样复杂度为n平方 public int[] twoSum(int ...

随机推荐

  1. servlet监听器与事件

    前言 在Servlet 2.4/JSP 2.0中,共同拥有八个Listener接口,六个Event类别. 參考:Servlet中的八大Listener 入门 阅读文件夹 Web监听器 监听器的分类 S ...

  2. Linux Shell 高级变量及字符串

    高级变量包含三个部分1.变量扩展2.命令替换3.算术扩展在Bash Shell中,$算符会触发到上述三种扩展,基本形式如下:    基本型             扩展种类            例子 ...

  3. 【整理】Python中实际上已经得到了正确的Unicode或某种编码的字符,但是看起来或打印出来却是乱码

    转自:http://www.crifan.com/python_already_got_correct_encoding_string_but_seems_print_messy_code/ [背景] ...

  4. Weka关联规则分析

    购物篮分析: Apriori算法: 参数设置: 1.car 如果设为真,则会挖掘类关联规则而不是全局关联规则. 2. classindex 类属性索引.如果设置为-1,最后的属性被当做类属性. 3. ...

  5. Error in as.POSIXlt.character(x, tz, ...) :

    > sqlFetch(channel,"user")Error in as.POSIXlt.character(x, tz, ...) :   character strin ...

  6. 机器学习5—logistic回归学习笔记

    机器学习实战之logistic回归 test5.py #-*- coding:utf-8 import sys sys.path.append("logRegres.py") fr ...

  7. phpexcel导入xlsx文件报错xlsx is not recognised as an OLE file 怎么办

    最初的做法  代码如下 1 include 'classes/PHPExcel/IOFactory.php'; 2 $inputFileName = $target; 3 $objReader = n ...

  8. 使用java命令运行class文件带包名时出错

    会出现classnotfound的错误. 如:文件x:/Test.java; package xx public class Test{} javac 编译后,java命令出错,需要手动构建包目录xx ...

  9. web安全之SQL注入---第四章 如何进行SQL注入攻击

    第四章 如何进行SQL注入攻击1.数字注入2.字符串注入    '#    '--

  10. 记录-移动端网页触摸内容滑动js插件

    需求: 在webapp中需要左右滑动手机,移动主页的轮播图.也可用在引导页(欢迎页)的大图左右滑动 可用: 百度:swiper插件 在项目中导入插件,这里只有部分代码,具体百度swiper <l ...