【leetcode刷题笔记】Letter Combinations of a Phone Number
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的更多相关文章
- 刷题17. Letter Combinations of a Phone Number
一.题目说明 题目17. Letter Combinations of a Phone Number,题目给了下面一个图,输入一个字符串包括2-9,输出所有可能的字符组合. 如输入23所有可能的输出: ...
- [刷题] 17 Letter Combinations of a Phone Number
要求 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合 1 不对应任何字母 示例 输入:"23" 输出:["ad", "ae&q ...
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
- 18.9.10 LeetCode刷题笔记
本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...
- LeetCode刷题笔记 - 12. 整数转罗马数字
学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...
- Leetcode刷题笔记(双指针)
1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...
- lintcode 中等题:Letter Combinations of a Phone Number 电话号码的字母组合
题目 电话号码的字母组合 给一个数字字符串,每个数字代表一个字母,请返回其所有可能的字母组合. 下图的手机按键图,就表示了每个数字可以代表的字母. 样例 给定 "23" 返回 [& ...
- LeetCode (17)Letter Combinations of a Phone Number
题目 Given a digit string, return all possible letter combinations that the number could represent. A ...
- LeetCode刷题笔记(1-9)
LeetCode1-9 本文更多是作为一个习题笔记,没有太多讲解 1.两数之和 题目请点击链接 ↑ 最先想到暴力解法,直接双循环,但是这样复杂度为n平方 public int[] twoSum(int ...
随机推荐
- TCP是如何保证包的顺序传输
转自:http://blog.csdn.net/ggxxkkll/article/details/7894112 大家都知道,TCP提供了最可靠的数据传输,它给发送的每个数据包做顺序化(这看起来非常烦 ...
- WAS集群系列(3):集群搭建:步骤1:准备文件
说明:"指示轨迹"为"点选顺序",截图为点击后效果截图 环境 项目点 指标 WAS版本号 7.0 操作系统 Windows 2008 系统位数 64bit 内存 ...
- 【HDU 5316】Magician(线段树)
一開始度错题了,题意是求一段和最大的[子序列],要求相邻两个元素的位置必须互为奇偶. 这样我们能够使用线段树维护4个值: 一段区间内开头结尾元素为: 奇奇 奇偶 偶奇 偶偶 的最大值 之后在pushu ...
- gulp配置,实例演示
项目完成后的目录 我们所需要的插件为:gulp-minify-css gulp-concat gulp-uglify gulp-rename del 如下图所示,完成后的项目目录结构: 附加,获取pa ...
- oracle中位图索引和B-tree索引的区别
1.适用系统的不同:位图索引适合OLAP系统,而B-tree索引适合OLTP系统. 2.占用存储空间不同:位图索引只需要很小的存储空间,而B-tree索引需要占用很大的存储空间. 3.创建需要的时间不 ...
- swift - 全屏pop手势
UINavigationController系统自带有侧滑手势,但是这个手势第一点只能边缘侧滑才可以有效,第二点当手动隐藏系统的导航时,这个手势就不能生效了 为了能到达到全屏pop的效果这里有2中解决 ...
- 快速搭建一个成熟,强壮的App框架【转载】
App框架搭建 招聘信息: iOS 研发工程师 iOS开发工程师 iOS开发实习工程师 新浪微博-Android开发工程师&iOS开发工程师 美术设计师(2D) UI设计师 cocos2dx手 ...
- cp 命令 简要
1.cp 1.log hanDir 将1.log复制到hanDir目录中 2.cp 1.log -i hanDir 复制前询问是否覆盖重名文件 3.cp -a han ...
- java.io.IOException: Illegal partition for 67 (-1)
今天写MapReduce的分区进行排序的功能,自己写了一个Partitioner,然后用的时候就错了 public static class MyPartition extends Partition ...
- JavaScript跨浏览器事件处理
var EventUtil = { getEvent: function(event){ return event ? event : window.event; }, getTarget: func ...