您的位置:首页 > 其它

17. Letter Combinations of a Phone Number

2016-12-07 22:22 375 查看
Given a digit string, 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.



Input: [/b]Digit string "23"
Output: [/b]["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
Note:[/b]
Although the above answer is in lexicographical order, your answer could be in any order you want.

分析

方法一

每个字母都会独自展开一个分支,就是排列组合问题。DFS搜索
"425"
|
g h i 4
/|\ /|\ /|\ a b c a b c a b c 7 .
.

方法二

转自:http://www.tuicool.com/articles/m6BFNfR 可以先将0...1的所有排列找到-->{"a", "b", "c"}再进一步将0...2的所有排列找到-->{"ad", "ae","af", "bd", "be", "bf", "cd", "ce", "cf"}如此循环...直到字符串末尾。实现如下:
来自为知笔记(Wiz)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: