[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/ 二.题目大意: 给定一段数字字符串,其中每个数 ...
随机推荐
- 什么是pytorch(2Autograd:自动求导)(翻译)
Autograd: 自动求导 pyTorch里神经网络能够训练就是靠autograd包.我们来看下这个包,然后我们使用它来训练我们的第一个神经网络. autograd 包提供了对张量的所有运算自动求导 ...
- Windows下struct和union字节对齐设置以及大小的确定(一 简介和结构体大小的确定)
在windows下设置字节对齐大小的方式,目前我了解有三种: 1. 在编译程序时候的编译选项 /Zp[n],如 cl /Zp4 表示对齐大小是4字节: 2. 预处理命令 #pragma pack ...
- MySQL Execution Plan--IN查询计划(2)
在MySQL中,IN查找经常出现性能问题,相同SQL在MySQL不同版本中表现不同. 准备测试数据: ## 创建表tb001 CREATE TABLE tb001( id INT unsigned N ...
- YAML 知识点
YAML:Ain't Markup Language 的缩写 YAML文件定义了一系列带有约束说明的任务,这些任务都是以任务名开始并且至少要包含script部分. 任务是由Runners接管并且由服务 ...
- Matlab关于视觉问题中的一些自有API
[randsample/randperm] y = randsample(n,k);从1:n中随机抽取k个数. y= randperm(n)或者y= randperm(n,k) [rectint] ...
- 高性能网络编程之IO和NIO阻塞分析
一.内容 1.阻塞和非阻塞是什么? 2.传统IO模型,他存在哪些阻塞点 3.NIO模型 4.对比总结 1.阻塞和非阻塞是什么? 阻塞:做某件事情,直到完成,除非超时,如果没有完成,继续等待. 非阻塞: ...
- [转]PostgreSQL命令行使用手册
启动pgsl数据库 1 pg_ctl -D /xx/pgdata start 查看pgsl版本 1 pg_ctl --version 命令行登录数据库 1 psql -U username -d db ...
- ROS下利用realsense采集RGBD图像合成点云
摘要:在ROS kinetic下,利用realsense D435深度相机采集校准的RGBD图片,合成点云,在rviz中查看点云,最后保存成pcd文件. 一. 各种bug 代码编译成功后,打开rviz ...
- IDEA忽略某些文件
Settings→Editor→File Types 在下方的忽略文件和目录(Ignore files and folders)中添加自己需要过滤的内容 下图为我自己添加过滤的内容,例如:*.im ...
- 关于性能(SEO)
性能优化有很多方面:细节决定成败,系统慢是由一个又一个的小细节造成的,所以开发初期做好充足的准备,开发中认真负责,不偷工减料,维护期更注重代码质量,这样才会让我妈的系统稳定高效. 1.0 使用CDN( ...