17. Letter Combinations of a Phone Number
2016-01-06 21:02
736 查看
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.
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.
do a dfs search to see all the combinations.
class Solution(object):
def letterCombinations(self, digits):
"""
:type digits: str
:rtype: List[str]
"""
NUMBER_HASH={'2':['a','b','c'],
'3':['d','e','f'],
'4':['g','h','i'],
'5':['j','k','l'],
'6':['m','n','o'],
'7':['p','q','r','s'],
'8':['t','u','v'],
'9':['w','x','y','z']
}
digit_length = len(digits)
def dfs(number_length, string, result):
if number_length == digit_length:
result.append(string)
return
for letter in NUMBER_HASH[digits[number_length]]:
dfs(number_length + 1, string + letter, result)
result = []
if len(digits) == 0:
return result
dfs(0, '', result)
return result
A mapping of digit to letters (just like on the telephone buttons) is given below.
Input:Digit string "23" Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.
do a dfs search to see all the combinations.
class Solution(object):
def letterCombinations(self, digits):
"""
:type digits: str
:rtype: List[str]
"""
NUMBER_HASH={'2':['a','b','c'],
'3':['d','e','f'],
'4':['g','h','i'],
'5':['j','k','l'],
'6':['m','n','o'],
'7':['p','q','r','s'],
'8':['t','u','v'],
'9':['w','x','y','z']
}
digit_length = len(digits)
def dfs(number_length, string, result):
if number_length == digit_length:
result.append(string)
return
for letter in NUMBER_HASH[digits[number_length]]:
dfs(number_length + 1, string + letter, result)
result = []
if len(digits) == 0:
return result
dfs(0, '', result)
return result
相关文章推荐
- SBT 构建 spark streaming集成kafka (scala版本)
- 秒杀多线程第六篇 经典线程同步 事件Event
- Android网络开发框架Retrofit(四:扩展篇,Retrofit+RxJava)
- Hessian
- Https工具类
- splay树 hdu1166 敌兵布阵
- Mac平台的Cocos2dx开发环境搭建
- 17. Letter Combinations of a Phone Number
- Java中关键字Volatitle的作用!
- 机器学习系列(5)_从白富美相亲看特征选择与预处理(上)
- 时间复杂度和空间复杂度(1)
- navigationBar 和 navigationItem 设置 titleView 时的不同
- 机器学习系列(5)_从白富美相亲看特征选择与预处理(上)
- 关于setter 和 getter方法的一些总结(初级)
- RPC开源框架性能对比
- 设计模式-单例
- Codeforces Round #336 Zuma
- MySQL中使用innobackupex、xtrabackup进行大数据的备份和还原教程
- mac下非常好用的命令行工具 iTerm2
- 【DirectX 9.0学习之路(第三话)】——D3DPRESENT_PARAMETERS 结构参数的详细说明