码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode17 Letter Combinations of a Phone Number

时间:2020-02-06 16:44:30      阅读:70      评论:0      收藏:0      [点我收藏+]

标签:col   sel   for   nbsp   button   ssi   number   input   elf   

 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)

 

leetcode17 Letter Combinations of a Phone Number

标签:col   sel   for   nbsp   button   ssi   number   input   elf   

原文地址:https://www.cnblogs.com/yawenw/p/12268845.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!