zoukankan      html  css  js  c++  java
  • 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)
  • 相关阅读:
    面向过程,面向对象三大特性
    JDBC连接数据库
    java线程
    ssm
    com组件方面的书籍
    剑雨
    JavaScript为元素动态添加事件之(attachEvent||addEventListener)
    Opacity多浏览器透明度兼容处理
    通过U盘安装Windows 7
    蜀门Online 简单打怪脚本(vbs)
  • 原文地址:https://www.cnblogs.com/yawenw/p/12268845.html
Copyright © 2011-2022 走看看