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. 使用FREDATED引擎实现跨实例訪问

    跨数据库server.跨实例訪问是比較常见的一种訪问方式,在Oracle中能够通过DB LINK的方式来实现. 对于MySQL而言,有一个FEDERATED存储引擎与之相相应.相同也是通过创建一个链接 ...

  2. Windows App开发之编辑文本与绘制图形

    编辑文本及键盘输入 相信大家都会使用TextBox,但假设要让文本在TextBox中换行该怎么做呢?将TextWrapping属性设置为Wrap,将AcceptsReturn属性设置为True就好咯. ...

  3. Spring Security三种认证

    Spring Security: 1.用户名+密码认证 2.手机号+短信认证 Spring Social: 1.第三方认证, QQ登录等 Spring Security OAuth: 1.把认证之后的 ...

  4. YUV图像合成原理

    http://blog.csdn.net/zwz1984/article/details/50403150 http://zhongcong386.blog.163.com/blog/static/1 ...

  5. nodeJS 中关于 promise 的使用

    var readInfo = function (fileName) { var defer = q.defer(); fs.readFile(fileName, {encoding:'utf-8'} ...

  6. 华为nova3发布,将支持华为AI旅行助手

    ​​​华为nova3于7月18日18:00在深圳大运中心体育馆举行华为nova 3的发布会,从本次华为nova3选择的代言人-易烊千玺,不难看出新机依然延续nova系列的年轻属性,主打 “高颜值 爱自 ...

  7. 精准 iOS 内存泄露检测工具

    MLeaksFinder:精准 iOS 内存泄露检测工具 发表于 2016-02-22   |   zepo   |   23 Comments 背景 平常我们都会用 Instrument 的 Lea ...

  8. TCP可靠传输详解

    TCP提供了可靠的传输服务,这是通过下列方式提供的: 分块发送:应用数据被分割成TCP认为最适合发送的数据块.由TCP传递给IP的信息单位称为报文段或段(segment) 定时确认重传:当TCP发出一 ...

  9. PHPstudy如何在本地搭建多站点

    参考地址: http://jingyan.baidu.com/article/e52e36154227ef40c70c5147.html

  10. php编译参数选项 具体参数含义可以用./configure --help来查看

    php编译参数选项  PHP_INSTALL_PATH=/data/web/php MYSQL_INSTALL_PATH=/data/web/mysql ./configure --prefix=${ ...