40.leetcode17_letter_combinations_of_a_phone_number

Posted vlice

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了40.leetcode17_letter_combinations_of_a_phone_number相关的知识,希望对你有一定的参考价值。

1.题目描述

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"].

2.题目分析

注意含有“0”“1”就返回空列表

3.解题思路

 1 class Solution(object):
 2     def letterCombinations(self, digits):
 3         """
 4         :type digits: str
 5         :rtype: List[str]
 6         """
 7         strs="" #当前字符串
 8         temp=[] #当前字符串列表
 9         result=[] #最后要返回的结果
10         dic={"2":"abc","3":"def","4":"ghi","5":"jkl","6":"mno","7":"pqrs","8":"tuv","9":"wxyz"} #设置字典便于查询
11         if "1"in digits or"0"in digits: #排除含有“0”“1”的情况
12             return []
13         for i in digits: 
14             temp=result #将上一级字符串列表给temp
15             result=[] #result列表清空
16             l1=len(temp)
17             str1=dic[i]
18             l2=len(str1)
19             j=0
20             if l1==0: #得到第一级字符串列表
21                 for s in str1:
22                     result.append(s) 
23             while j<l1:
24                 k=0
25                 while k<l2:
26                     strs=temp[j] #获得当前字符串
27                     strs+=str1[k]
28                     result.append(strs) #补充result列表
29                     k+=1
30                 j+=1
31          return result

 

以上是关于40.leetcode17_letter_combinations_of_a_phone_number的主要内容,如果未能解决你的问题,请参考以下文章

35-40LeetCode:Python解题

35-40LeetCode:Python解题

LeetCode 572. 另一个树的子树(Subtree of Another Tree) 40

17.5.21 22:17 第一天+++补充!忘记吧代码放上来了

17windows_17_listbox列表框

delphi 四舍五入的坑