cool and pythonic solution to leetcode 17.
2016-10-30 22:34
387 查看
17. Letter Combinations of a Phone Number
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.
思路:hashtable+笛卡尔积
my code:
class Solution(object):
def letterCombinations(self, digits):
"""
:type digits: str
:rtype: List[str]
"""
if not digits:
return []
dict = {'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'],
'1':['*'],
'0':[' ']
}
ll = [dict[key] for key in digits]
return [''.join(item) for item in itertools.product(*ll)]
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:Digit string "23" Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
思路:hashtable+笛卡尔积
my code:
class Solution(object):
def letterCombinations(self, digits):
"""
:type digits: str
:rtype: List[str]
"""
if not digits:
return []
dict = {'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'],
'1':['*'],
'0':[' ']
}
ll = [dict[key] for key in digits]
return [''.join(item) for item in itertools.product(*ll)]
相关文章推荐
- [python]leetcode(309). Best Time to Buy and Sell Stock with Cooldown
- Leetcode_best-time-to-buy-and-sell-stock(c++ and python version)
- 【LeetCode with Python】 Best Time to Buy and Sell Stock
- LeetCode "Best Time to Buy and Sell Stock with Cooldown" !
- leetcode 【 Best Time to Buy and Sell Stock 】python 实现
- [leetcode] Best Time to Buy and Sell Stock with Cooldown
- Leetcode_best-time-to-buy-and-sell-stock-ii(c++ and python version)
- [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期
- 188. Best Time to Buy and Sell Stock IV Leetcode Python
- LeetCode Best Time to Buy and Sell Stock with Cooldown
- [leetcode]Best Time to Buy and Sell Stock II @ Python
- [leetcode]Best Time to Buy and Sell Stock III @ Python
- leetcode Best Time to Buy and Sell Stock with Cooldown
- [LeetCode] Best Time to Buy and Sell Stock II Solution
- [leetcode]Best Time to Buy and Sell Stock III @ Python
- 【LeetCode with Python】 Best Time to Buy and Sell Stock II
- [LeetCode] [Python] [DP] Best Time to Buy and Sell Stock III
- leetcode 309 : Best Time to Buy and Sell Stock with Cooldown
- [LeetCode] Best Time to Buy and Sell Stock Solution
- 【LeetCode】【Python解决问题的方法】Best Time to Buy and Sell Stock II