算法练习--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< ...
随机推荐
- BUPT 2012复试机考 3T
97. 二叉排序树 时间限制 1000 ms 内存限制 65536 KB 题目描述 二叉排序树,也称为二叉查找树.可以是一颗空树,也可以是一颗具有如下特性的非空二叉树: 若左子树非空,则左子树上所有节 ...
- Android安全机制介绍
Android的安全机制包含下面几个方面: • 进程沙箱隔离机制. • 应用程序签名机制. • 权限声明机制. • 訪问控制机制. • 进程通信机制. ...
- Solidworks打印工程图超出范围了怎么办
打印预览,边框部分无法显示 页面设置,比例改为90%,试一下 正常了,如果你还是无法正常显示,就再改小比例
- EF使用自定义字符串连接数据库
edmx的构造函数: public TestCheckUpdatesEntities(): base(Config.DataBaseConnectionString(), "TestChec ...
- 【转载】WebService相关概念
一.序言 大家或多或少都听过 WebService(Web服务),有一段时间很多计算机期刊.书籍和网站都大肆的提及和宣传WebService技术,其中不乏很多吹嘘和做广告的成 分.但是不得不承认的是W ...
- hibernate载入持久化对象的两种方式——get、load
一.get与load对照 在hibernate中get和load方法是依据id取得持久化对象的两种方法.但在实际使用的过程中总会把两者混淆,不知道什么情况下使用get好,什么时候使用load方法效率更 ...
- vs2010配置VL_FEAT库
VL_FEAT库是计算机视觉中的一个开源库,支持C/C++,Matlab,可以在http://www.vlfeat.org/下载. 本文主要讲一下VS2010中如何配置vl_feat库(算是对原文的一 ...
- Hdu 4274 Spy's Work
Spy's Work Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- Ajax的简单实现
Ajax的实现需要服务器端和客户端配合来实现 下面看服务器端的代码,也就是用php编写的一个后台脚本文件 <?php //设置页面内容,编码格式是utf8 header("Conten ...
- MongoDB安装和简单介绍
前面我们把nodejs的web开发入门说了,如今来说说数据库,一般搭配的数据库是mysql和mongodb,今天我们来说mongodb MongoDB是一个基于分布式文件存储的数据库,由C++语言编写 ...