算法练习--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< ...
随机推荐
- 如何绕过Win8、Win10的systemsetting与注册表校验设置默认浏览器
*本文原创作者:浪子_三少,属Freebuf原创奖励计划,未经许可禁止转载 在win7时我们只需修改注册表就能设置默认浏览器,但是win8.win10下不能直接修改的因为同样的注册表项,win8.wi ...
- Android自己定义View基础篇(三)之SwitchButton开关
自己定义View基础篇(二) 自己定义View基础篇(一) 自己定义View原理 我在解说之前,先来看看效果图,有图有真相:(转换gif图片效果太差) 那来看看真实图片: 假设你要更改样式,请改动例如 ...
- HDOJ 1217 Arbitrage(拟最短路,floyd算法)
Arbitrage Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- 【转载】关于Hash
这个HASH算法不是大学里数据结构课里那个HASH表的算法.这里的HASH算法是密码学的基础,比较常用的有MD5和SHA,最重要的两条性质,就是不可逆和无冲突.所谓不可逆,就是当你知道x的HASH值, ...
- Serial attached SCSI
http://en.wikipedia.org/wiki/Serial_attached_SCSI Serial attached SCSI From Wikipedia, the free ency ...
- Intel processor brand names-Xeon,Core,Pentium,Celeron----Atom
http://en.wikipedia.org/wiki/Intel_atom Intel Atom From Wikipedia, the free encyclopedia (Redirect ...
- 编译 Deedle
编译 Deedle Deedle 中含有 RProvider. 要编译 Deedle.须要先下载 R.地址: http://cran.cnr.berkeley.edu/bin/windows/base ...
- ribbon负载均衡进行服务消费
相同服务以不同端口形式注册到eureka上,ribbon从eureka上获取冰进行服务消费,会偶现如下现象: I/O error on GET request for "http://COM ...
- Arcgis Engine(ae)接口详解(2):featureClass查询
//属性查询~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //IQueryFilter代表查询条件,QueryFilterClass代表只限于属性查询(就是没有空间查询) ...
- 2016/06/16 phpexcel
程序部分 require_once './phpexcel/PHPExcel.php'; // 首先创建一个新的对象 PHPExcel object $objPHPExcel = new ...