美文网首页
425. Letter Combinations of a Ph

425. Letter Combinations of a Ph

作者: 鸭蛋蛋_8441 | 来源:发表于2019-06-28 09:35 被阅读0次

Description

Given a digit string excluded '0' and '1', 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.

12

ABC

3

DEF

4

GHI

5

JKL

6

MNO

7

PQRS

8

TUV

9

WXYZ

Although the answer above is in lexicographical order, your answer could be in any order you want.

Example

Example 1:

Input: "23"

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

Explanation:

'2' could be 'a', 'b' or 'c'

'3' could be 'd', 'e' or 'f'

Example 2:

Input: "5"

Output: ["j", "k", "l"]

思路:

按照给定的数字先去dict里查找对应的字母, 这里的扩展是按照数字的顺序一层一层往下的,当数字全部走完扩展也就到了最底层,也就达到了返回条件,还有要注意string不是传reference,还有就是循环的时候不用index直接用letter会比较简洁。

代码:

相关文章

网友评论

      本文标题:425. Letter Combinations of a Ph

      本文链接:https://www.haomeiwen.com/subject/rjgjcctx.html