算法练习--LeetCode--17. Letter Combinations of a Phone Number
- Letter Combinations of a Phone Number
MediumGiven 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.
1
o-o_
2
abc
3
def
4
ghi_
5
jkl
6
mno
7
pqrs
8
tuv
9
wxyz
*
+___
0
___
#
↑__
除了0的第一个“”以外的位置的“”是为了对齐
我觉得上面那玩意儿不如图Example:
Input: "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]
大意是指要把输入的数字按照手机键盘的映射关系转成可能的字符串
- 不要有重复的
- 不用在意字符串顺序(输出结果数组的排序)
Note:
虽然题目中给出输入数字在[2, 9]
,实测(2019-01-05)
0
和1
也有对应的映射关系
0 => " "
1 => "*"
基本分析:
- 要求长度为
n
的结果,首先要求长度为n-1
的结果! - n == 1 时候结果是已知的
"" : [" "],
"" : ["*"],
"" : ["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"]
- 为了节约时间要有缓存
基本思路
func letterCombinations(s: String) -> [String] {
if s 为空字符串, return 空数组
if cache 不包含 s {
cache[s] = cache[s的第一个字符] * letterCombinations(s除了第一字符以外剩下的部分的结果)
}
return cache[s]
}
代码
// Swift Code
class Solution {
var cache: [String : [String]] = [
"" : [" "],
"" : ["*"],
"" : ["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"]
] func letterCombinations(_ digits: String) -> [String] {
if digits.isEmpty { return [] }
if !cache.keys.contains(digits) {
cache[digits] = letterCombinations(String(digits.prefix())).flatMap({ (s) -> [String] in
letterCombinations(String(digits.suffix(digits.count - ))).map { s + $ }
})
}
return cache[digits]!
}
}
TestCase
- ""
- "23"
- "203"
- "213"
算法练习--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 string containing digits from 2-9 inclusive, return all possible letter combinations that th ...
- [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 ...
- LeetCode——17. Letter Combinations of a Phone Number
一.题目链接: https://leetcode.com/problems/letter-combinations-of-a-phone-number/ 二.题目大意: 给定一段数字字符串,其中每个数 ...
- LeetCode 17 Letter Combinations of a Phone Number (电话号码字符组合)
题目链接 https://leetcode.com/problems/letter-combinations-of-a-phone-number/?tab=Description HashMap< ...
随机推荐
- 【J2EE】十三个规范:愿天下苍生,人人如猿。
学习了J2ee后对java这个立足标准化的行为感到深深的佩服. 收买什么都不如收买人心,培养啥子都不如培养 习惯.没错,java就是在培养行业习惯,以一纸规范屹立不倒.毕竟技术什么的层出不穷,再新再前 ...
- Golang之bytes.buffer
bytes.buffer是一个缓冲byte类型的缓冲器存放着都是byte Buffer 是 bytes 包中的一个 type Buffer struct{-} A buffer is a variab ...
- typedef和#define的差别——————【Badboy】
typedef 和#define 都经常使用来定义一个标识符及keyword的别名.但他们之间有关键的差别. typedef 是语言编译过程的一部分; #define是宏定义语句,它本身并不在编译过程 ...
- AliYunDun关闭
停止阿里云盾AliYunDun服务解决大量写磁盘问题-小内存ECS服务器 阿里云数据库在没备案,涉及大量IO操作时会自动启动阿里云盾这个服务,会导致服务器变得很卡 关闭服务: service aegi ...
- 王立平--Unity破解
1.下载破解工具.关闭Unity,打开破解工具 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzQyNTUyNw==/font/5a6L5L2T/ ...
- 【网站支付PHP篇】thinkPHP集成汇潮支付(ecpss)
系列目录 支付宝集成:http://www.cnblogs.com/nerve/p/3437879.html 系列说明 最近在帮朋友的系统安装支付模块(兑换网站积分),现在总结一些开发心得,希望对大家 ...
- HDU 6183 Color it cdq分治 + 线段树 + 状态压缩
Color it Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) Pro ...
- ECMAScript学习笔记
1. ECMAScript不存在块级作用域,因此在循环内部定义的变量,在循环外也是可以访问的 eg: var count =10; fpr(var i=0; i<count; i++){ ale ...
- Mysql的Merge存储引擎实现分表查询
对于数据量很大的一张表,i/o效率底下,分表势在必行! 使用程序分,对不同的查询,分配到不同的子表中,是个解决方案,但要改代码,对查询不透明. 好在mysql 有两个解决方案: Partition(分 ...
- [树套树]K大数查询
有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数是多少.为了 ...