[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/ 二.题目大意: 给定一段数字字符串,其中每个数 ...
随机推荐
- EF 指定字段修改
public virtual void Modify(T model, params string[] ProNames) { DbEntityEntry entry = db.Entry<T& ...
- marathon 测试
marathon 初步使用 关闭selinux setenforce 0 Marathon之应用篇 先来了解一下 Marathon 是怎么布署decker的 json shell.json { } ...
- Verilog HDL中的运算符关系
1,位运算符 按位运算的运算符是位运算符,原来的操作数有几位,结果就有几位,若两个操作数位数不同,则位数短的操作数左端会自动补0. (1),按位取反:~ (2),按位与:& (3),按位或:| ...
- Python基础------运算符
运算符类型 算数运算符 + 加 - 减 * 乘 / 除 %取余 ...
- Office_PPT_让你一分钟完成上百张图片的快速保存
1 方式 修改PPT文件格式,由PPT修改为rar,再进行解压操作 进入到ppt->media中找到你在PPT为文件中使用的图片. 2 PPT北京图片下载网址 别样网:https://www.s ...
- startup.bat 一闪而过解决方案
原文地址:http://blog.csdn.net/stypace/article/details/38083581 启动tomcat时cmd窗口一闪而过解决方法. 注: 我这边只是配置了环境变量就o ...
- 使用 JavaScript 将 XML 转成 JSON
function xmlToJson(xml) { // Create the return object var obj = {}; if (xml.nodeType == 1) { // elem ...
- 使用css实现时间轴
本文将使用css来实现一个左右交叉布局的时间轴,效果如下: 使用的都是一些常用的css,代码如下: <!DOCTYPE> <html> <head> <tit ...
- halcon批量读取图片
以前这个代码都是自己写,不仅繁琐,而且容易忘记.其实Halcon中提供了相关的方法.记录一下吧,其实很简单. 读取一个文件夹下的所有图片[助手]>[打开新的image acquisition ] ...
- leetcode739
class Solution(object): def dailyTemperatures(self, T: 'List[int]') -> 'List[int]': S = list() n ...