题目:

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. CSS继承的特殊性

    p{color:red;} .first{color:green;} <p class="first">三年级时,我还是一个<span>胆小如鼠</s ...

  2. html与htm区别

    1.index.html与index.htm同时有,先访问.html 2.htm是为了兼容以前的DOS系统.

  3. ssh中org.springframework.orm.hibernate4.support.OpenSessionInViewFilter的作用及配置

     org.springframework.orm.hibernate4.support.OpenSessionInViewFilter 是Spring为我们解决Hibernate的Session的关闭 ...

  4. 通过CAGradientLayer制作渐变色效果(转)

    转载自:http://blog.it985.com/7986.html 看了极客学院的视频之后写的一篇博客,觉得不错,还是作为笔记使用. 简单介绍一下CAGradientLayer吧. Gradien ...

  5. Nginx基本使用

    Nginx基本使用 下载源码包http://nginx.org/ http://nginx.org/en/download.html yum -y install pcre-devel openssl ...

  6. BZOJ4520 [Cqoi2016]K远点对

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...

  7. [noip科普]关于LIS和一类可以用树状数组优化的DP

    预备知识 DP(Dynamic Programming):一种以无后效性的状态转移为基础的算法,我们可以将其不严谨地先理解为递推.例如斐波那契数列的递推求法可以不严谨地认为是DP.当然DP的状态也可以 ...

  8. JavaScript中的正则表达式(终结篇)

    JavaScript中的正则表达式(终结篇) 在之前的几篇文章中,我们了解了正则表达式的基本语法,但那些语法不是针对于某一个特定语言的.这篇博文我们将通过下面几个部分来了解正则表达式在JavaScri ...

  9. Agent理解

    简单来说,Agent是一个交互性的技术. 比如我们在网上购物时,主体是人,即请求的发出者,我们在搜索商品时,向系统发出请求,系统会给我们一个返回结果,然而我们发现很多情况下,我们会在商品栏目中发现我们 ...

  10. Samba服务器配置

    Samba服务器配置流程: (1)安装samba服务器先用#rpm -ivh samba-列出与samba有关的rpm包然后选择第一个包,用tab键补齐文件名 (2)创建新用户和其密码#useradd ...