leetcode17 Letter Combinations of a Phone Number

 1 """
 2 Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.
 3 A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
 4 Example:
 5 Input: "23"
 6 Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
 7 """
 8 """
 9 本题三次循环,看不懂debug一遍就好了
10 """
11 class Solution1:
12     def letterCombinations(self, digits):
13         phone = {'2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl', '6': 'mno', '7': 'pqrs', '8': 'tuv', '9': 'wxyz'}
14         digits = 23
15         if digits == '':
16             return []
17         result = ['']
18         for digit in digits:
19             lst = phone[digit]
20             newresult = []
21             for x in result:   #!!!保存现有的排列组合
22                 for y in lst:  #
23                     newresult.append(x + y)
24             result = newresult
25         return result
26 
27 # phone = {'2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl', '6': 'mno', '7': 'pqrs', '8': 'tuv', '9': 'wxyz'}
28 # digits = '23'
29 # result = ['']
30 # for digit in digits:
31 #     lst = phone[digit]
32 #     newresult = []
33 #     for x in result:   #!!!保存现有的排列组合
34 #         for y in lst:  #
35 #             newresult.append(x + y)
36 #     result = newresult
37 #
38 # print(result)

 

posted @ 2020-02-06 15:26  yawenw  阅读(107)  评论(0编辑  收藏  举报