1. Letter Combinations of a Phone Number
    Medium

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.

1o-o_ 2abc 3def
4ghi_ 5jkl 6mno
7pqrs 8tuv 9wxyz
*+___ 0___ #↑__

除了0的第一个“”以外的位置的“”是为了对齐

 
我觉得上面那玩意儿不如图

Example:

Input: "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]

大意是指要把输入的数字按照手机键盘的映射关系转成可能的字符串

  • 不要有重复的
  • 不用在意字符串顺序(输出结果数组的排序)

Note:

虽然题目中给出输入数字在[2, 9],实测(2019-01-05) 01也有对应的映射关系

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的更多相关文章

  1. 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 ...

  2. [LeetCode] 17. Letter Combinations of a Phone Number 电话号码的字母组合

    Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...

  3. [leetcode 17]Letter Combinations of a Phone Number

    1 题目: Given a digit string, return all possible letter combinations that the number could represent. ...

  4. Java [leetcode 17]Letter Combinations of a Phone Number

    题目描述: Given a digit string, return all possible letter combinations that the number could represent. ...

  5. Leetcode 17.——Letter Combinations of a Phone Number

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  6. [leetcode]17. Letter Combinations of a Phone Number手机键盘的字母组合

    Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...

  7. [LeetCode] 17. Letter Combinations of a Phone Number ☆☆

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  8. [LeetCode]17. Letter Combinations of a Phone Number电话号码的字母组合

    Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...

  9. LeetCode——17. Letter Combinations of a Phone Number

    一.题目链接: https://leetcode.com/problems/letter-combinations-of-a-phone-number/ 二.题目大意: 给定一段数字字符串,其中每个数 ...

  10. LeetCode 17 Letter Combinations of a Phone Number (电话号码字符组合)

    题目链接 https://leetcode.com/problems/letter-combinations-of-a-phone-number/?tab=Description HashMap< ...

随机推荐

  1. .net core webapi jwt 更为清爽的认证

    原文:.net core webapi jwt 更为清爽的认证 我的方式非主流,控制却可以更加灵活,喜欢的朋友,不妨花一点时间学习一下 jwt认证分为两部分,第一部分是加密解密,第二部分是灵活的应用于 ...

  2. 更改Tomcat命令行窗体标题

     在windows下启动多个tomcat时.不好区分哪个tomcat相应哪个服务,能够通过下面方法设置Tomcat命令行窗体的标题: 1.在%tomcat_home%\bin\catalina.b ...

  3. Coursera 机器学习Course Wiki Lecture Notes

    https://share.coursera.org/wiki/index.php/ML:Main 包含了每周的Lecture Notes,以便复习回顾的时候使用.

  4. SpringCloud中Redis的使用

    1.引入redis相关jar包 <dependency> <groupId>org.springframework.boot</groupId> <artif ...

  5. qt动画入门

    Qt-4.6新增了Animation Framework(动画框架),让我们可以方便的写一些生动的程序. 不必像曾经的版本号一样,全部的控件都枯燥的呆在伟大光荣的QLayout里,或许它们可以唱个歌, ...

  6. Android使用procrank和dumpsys meminfo 、top分析内存占用情况

    如果你想查看所有进程的内存使用情况,可以使用命令procrank.dumpsys meminfo查看,当然也只可以过滤出某个进程如:dumpsys meminfo | grep -i phone 先来 ...

  7. bash仅仅读的环境变量

    环境变量名 变量的用途 $0 程序的名字 $1~$9 命令參数1~9的值 $* 全部命令行參数的值 $@ 全部命令行參数的值.假设$@被""包含.即"$@",这 ...

  8. 有遍历struct中字段信息的函数或方法

    例:struct a{int a;char b[10];double c;}; 在程序中只知道一个结构 a 的指针, 有没有函数能通过结构的名字 和 指向结构的指针 随次得到 结构中的变量类型 和 变 ...

  9. 杭电 1596 find the safest road (最短路)

    http://acm.hdu.edu.cn/showproblem.php?pid=1596 这道题目与杭电2544最短路的思想是一样的.仅仅只是是把+改成了*,输入输出有些不一样而已. find t ...

  10. Smoke testing (software)

    Smoke testing (software) - Wikipedia https://en.wikipedia.org/wiki/Smoke_testing_(software) In compu ...