[leetcode]17. Letter Combinations of a Phone Number手机键盘的字母组合
Given a string containing digits from 2-9 inclusive, 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. Note that 1 does not map to any letters.

Example:
Input: "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.
题意:
给定一个数字串,看看有多少种对应的字母串。
思路:
Assumption:
字符串的合法性,是否包含1这个数字?如果包含,怎么处理?同样,输入是否考虑 * 或 #?
空字符串怎么处理?
多个解按什么顺序返回?
High Level带着面试官walk through:
take "23" for example:
when index = 0, pointing to '2' in "23"
String letters = "abc"
for each char in letters,
add to path
move index forward, pointing to '3' in "23", recursively call the helper function to add every char of "def" to the path





code
class Solution {
private static String[] keyboard =
new String[]{ " ", "", "abc", "def", // '0','1','2',...'9'
"ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
public List<String> letterCombinations(String digits) { //"23"
List<String> result = new ArrayList<>();
if(digits.length() == 0 ) return result;
helper(digits, 0, new StringBuilder(), result);
return result;
}
private void helper(String digits, int index, StringBuilder sb, List<String> result){
if(index == digits.length()){
result.add(sb.toString());
return;
}
String letters = keyboard[digits.charAt(index) - '0'];
for(int i = 0; i < letters.length(); i++ ){
char c = letters.charAt(i);
sb.append(c);
helper(digits, index+1, sb, result);
sb.deleteCharAt(sb.length() - 1);
}
}
}
注意: 这个题也可以用String path来存每条路径。 可是我自己觉得用StringBuilder这么动态伸缩,更显得有“Backtracking”的味道。
[leetcode]17. Letter Combinations of a Phone Number手机键盘的字母组合的更多相关文章
- Leetcode 17. Letter Combinations of a Phone Number(水)
17. Letter Combinations of a Phone Number Medium Given a string containing digits from 2-9 inclusive ...
- [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
1 题目: Given a digit string, return all possible letter combinations that the number could represent. ...
- Java [leetcode 17]Letter Combinations of a Phone Number
题目描述: Given a digit string, return all possible letter combinations that the number could represent. ...
- Leetcode 17.——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 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 ...
- 17. Letter Combinations of a Phone Number[M]电话号码的字母组合
题目 Given a string containing digits from 2-9 inclusive, return all possible letter combinations that ...
- LeetCode——17. Letter Combinations of a Phone Number
一.题目链接: https://leetcode.com/problems/letter-combinations-of-a-phone-number/ 二.题目大意: 给定一段数字字符串,其中每个数 ...
随机推荐
- python, Image
PIL: Python Image Library, python平台的图像处理库,要使用Image首先要从PIL库导入Image: from PIL import Image 如果没有安装PIL的包 ...
- windos下安装django
一:pip install Django 安装完以后,运行python manager.py runserver 0.0.0.0:8000报错: 1):没有安装Mysql-python ...
- Maven+Spirng+Mybatis+CXF搭建WebService服务
https://jingyan.baidu.com/article/39810a23b1de2fb637fda66c.html
- 黄聪:谷歌验证 (Google Authenticator) 的实现原理是什么?
著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:徐小花链接:http://www.zhihu.com/question/20462696/answer/18731073来源: ...
- Response的Content-Type一览
文件扩展名 Content-Type(Mime-Type) 文件扩展名 Content-Type(Mime-Type) .* application/octet-stream .tif image/t ...
- 廖雪峰Java7处理日期和时间-2Data和Calendar-1Date
计算机中如何存储和表示日期和时间 Epoch Time:从1970年1月1日零点(格林威治时区/GMT+00:00)到现在经历的秒数,也叫timestamp, 例如: 秒级: * 北京 2016-11 ...
- django 多线程下载图片
example1: from multiprocessing.dummy import Pool as ThreadPool #多线程 import time import urllib2 urls ...
- 关于spring boot在IDE工具中可以启动成功,但是打成jar包以及运行jar包失败的问题
1. 运行jar包报错,如下图: 2. 首先,找到pom.xml,把下面的build块中的内容改成如下所示: 3. 然后,请千万不要用Intellij idea来打包项目为Jar,你应该来到项目的根目 ...
- [sql]sql函数coalesce返回第一个非空的值
下面来看几个比较有用的例子: 首先,从MSDN上看看这个函数的使用方法,coalesce函数(下面简称函数),返回一个参数中非空的值.如: SELECT COALESCE(NULL, NULL, G ...
- vue+窗格切换+田字+dicom显示_01
环境:vue+webpack+cornerstone ide:vs code 需求:窗格设置+拼图设置 1.点击左边第一个窗格或者默认显示. 2.点击第二个也同理显示,以此类推 3.选择左边的窗格之后 ...