题目:

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

Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.

代码:

每日一题哦,今天做到这个题目,仔细一看,就会找到数字对应的字符串,然后把字符串组合起来,返回一个列表。

想想就不应该很复杂,可是,还是做了一个小时,唉,看来必须每天坚持练习练习!

逻辑很简单,就是用一个列表保存每次两个字符串相加的结果,从digits的第一个元素开始,不断和下一次相加,一直加到digits最后一位元素:

def letterCombinations(self, digits):
        """
        :type digits: str
        :rtype: List[str]
        """
        letter_dic = {2:"abc",3:"def",4:"ghi",5:"jkl",6:"mno",7:"pqrs",8:"tuv",9:"wxyz"}
        if len(digits)==0:return []
        res_list = [list(letter_dic[int(digits[0])])]
        digits = digits[1:]
        while len(digits) !=0:                
            res_list.append(self.str_connect(res_list[-1],list(letter_dic[int(digits[0])])))       
            digits = digits[1:]
            #print (res_list)
        return res_list[-1]
    
    def str_connect(self,str_ori,str_add):
        res = []
        for i in range(0,len(str_ori)):
            for j in str_add:
                res.append(str_ori[i]+j)
        return res

17. Letter Combinations of a Phone Number的更多相关文章

  1. [LeetCode][Python]17: Letter Combinations of a Phone Number

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...

  2. Leetcode 17. Letter Combinations of a Phone Number(水)

    17. Letter Combinations of a Phone Number Medium Given a string containing digits from 2-9 inclusive ...

  3. 刷题17. Letter Combinations of a Phone Number

    一.题目说明 题目17. Letter Combinations of a Phone Number,题目给了下面一个图,输入一个字符串包括2-9,输出所有可能的字符组合. 如输入23所有可能的输出: ...

  4. 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  5. [LeetCode] 17. Letter Combinations of a Phone Number 电话号码的字母组合

    Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...

  6. [leetcode 17]Letter Combinations of a Phone Number

    1 题目: Given a digit string, return all possible letter combinations that the number could represent. ...

  7. Java [leetcode 17]Letter Combinations of a Phone Number

    题目描述: Given a digit string, return all possible letter combinations that the number could represent. ...

  8. Leetcode 17.——Letter Combinations of a Phone Number

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  9. 【一天一道LeetCode】#17. Letter Combinations of a Phone Number

    一天一道LeetCode (一)题目 Given a digit string, return all possible letter combinations that the number cou ...

随机推荐

  1. js的this上下文的坑

    很明显,this这个坑,在多层嵌套的时候还是一样被废,不管是call, apply还是bind. 例如: var fun = function() { this.name = 'test'; var ...

  2. 编译安装apache

    1.安装依赖库:“Development  Tools”  “Server Platform Development”  pcre-devel 2.安装apr,apr-util 从yum源安装或去ap ...

  3. Thinking in java学习笔记之持有对象总结

  4. 【BZOJ-2325】道馆之战 树链剖分 + 线段树

    2325: [ZJOI2011]道馆之战 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 1153  Solved: 421[Submit][Statu ...

  5. 将DataTable中的数据导出到Excel

    public static void Export(System.Data.DataTable dt,NPOI.HSSF.UserModel.HSSFWorkbook workbook,string ...

  6. 如何将EXCEL表导入ORACLE数据库中?【转】

    来源:https://zhidao.baidu.com/question/383828330.html?qbl=relate_question_2&word=excel%20%B1%ED%CA ...

  7. iis搭建FTP服务器

    win7下如何开启iis请参考前一篇 使用iis并搭建 iis 图片服务器 ftp登陆格式  : ftp://[帐号]:[密码]@[IP]:[端口] ftp://用户名:密码@FTP服务器IP或域名: ...

  8. Phabricator是什么,代码审查工具

    Phabricator是什么? Phabricator支持两种代码审查工作流:"review"(提交前审查)和 "audit"(提交后审查). Phabrica ...

  9. AndroidStudio导入Library

    1.把它像Module一样导入. File >New >ImportModule(选择你要导入的Library). 如果出现了下面的情况,意思是跟项目中的Module重名,改个名字就行了. ...

  10. 【转载】使用Pandas进行数据匹配

    使用Pandas进行数据匹配 本文转载自:蓝鲸的网站分析笔记 原文链接:使用Pandas进行数据匹配 目录 merge()介绍 inner模式匹配 lefg模式匹配 right模式匹配 outer模式 ...