17. Letter Combinations of a Phone Number
题目:
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的更多相关文章
- [LeetCode][Python]17: Letter Combinations of a Phone Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...
- 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 ...
- 刷题17. Letter Combinations of a Phone Number
一.题目说明 题目17. Letter Combinations of a Phone Number,题目给了下面一个图,输入一个字符串包括2-9,输出所有可能的字符组合. 如输入23所有可能的输出: ...
- 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- [LeetCode] 17. Letter Combinations of a Phone Number 电话号码的字母组合
Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...
- [leetcode 17]Letter Combinations of a Phone Number
1 题目: Given a digit string, return all possible letter combinations that the number could represent. ...
- Java [leetcode 17]Letter Combinations of a Phone Number
题目描述: Given a digit string, return all possible letter combinations that the number could represent. ...
- Leetcode 17.——Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- 【一天一道LeetCode】#17. Letter Combinations of a Phone Number
一天一道LeetCode (一)题目 Given a digit string, return all possible letter combinations that the number cou ...
随机推荐
- CCF 201604-2 俄罗斯方块
题目不难,但是感觉很有意思.一开始忘了把调试信息注释掉,WA了两次... 试题编号: 201604-2 试题名称: 俄罗斯方块 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 ...
- duilib学习 --- 360demo 学习
我想通过360demo的学习,大概就能把握duilib的一般用法,同时引申出一些普遍问题,和普遍解决方法.并在此分享一些链接和更多内容的深入学习..... 原谅我是一个菜鸟,什么都想知道得清清楚楚.. ...
- Android Studio 工具插件
1.Android Studio 翻译插件,可以将英文翻译为中文. https://github.com/Skykai521/ECTranslation 2.Android Studio之Androi ...
- ABP之依赖注入
写在开头 ABP开源项目最近有点小火,还开展了线下活动.本着学习DDD的心态与学习开源代码的的好奇,我也看了一遍ABP源码,在此将自己学习ABP的一些心得记录下来. 作为核心的IoC 作为一种解耦的方 ...
- 线段树 HDU 3397(真)
5 种操作 0 1 然后 异或 似乎这种2个更新的先后每次都搞不清 覆盖有覆盖就可以不异或 也不知道为什么 #include<stdio.h> #include<string.h& ...
- [分类算法] :朴素贝叶斯 NaiveBayes
1. 原理和理论基础(参考) 2. Spark代码实例: 1)windows 单机 import org.apache.spark.mllib.classification.NaiveBayes im ...
- NOIP2016呵呵记
经过了一年的想象和臆测,经历了学长们的几次考试,通过老师的言语莫名感受过了所谓oi式压力之后,自己的考试也终于到来了. 考前的生活也没有想象中的那么充实,无非跟着神犇刷刷题,讨论算法,学点新技巧,然后 ...
- BZOJ1026: [SCOI2009]windy数
传送门 md直接wa了78次,身败名裂 没学过数位DP硬搞了一道数位DP的模板题,感觉非常的愉(sha)悦(cha). 二分转化枚举思想.首先DP预处理出来$f[i][j]$表示有$i$位且第$i$位 ...
- [转载]用 grub2 启动 clover.iso 来启动 OS X
这个帖子只用来解决特定问题,是楼主这两天辛苦的结晶,如果你遇到了跟我差不多的情形,你就可以尝试这个解决方案. 特定情景:1.不管你的机器支不支持 UEFI ,反正你现在是用传统 BISO + MBR ...
- JSP登录页面使用Enter键登录【转】
按回车键实现登录 第一种方法: <html xmlns="http://www.w3.org/1999/xhtml" ><head> & ...