https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/
# -*- coding:utf-8 -*- class Solution(object): def letterCombinations(self, digits): """ :type digits: str :rtype: List[str] """ KEY = {'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']} if digits == '': return res = [''] for each in digits: res = [pre + sub for pre in res for sub in KEY[each]] return res if __name__ == '__main__': digit = '23' print(Solution().letterCombinations(digit))
思路:这道题不是很难,值得一提的就是这个语句嵌套,不得不说python还是方便。
0 条评论