leetcode-17-电话号码的字母组合’
题目描述:
方法一:回溯
class Solution:
def letterCombinations(self, digits):
"""
:type digits: str
:rtype: List[str]
"""
phone = {'': ['a', 'b', 'c'], '': ['d', 'e', 'f'], '': ['g', 'h', 'i'], '': ['j', 'k', 'l'], '': ['m', 'n', 'o'], '': ['p', 'q', 'r', 's'], '': ['t', 'u', 'v'], '': ['w', 'x', 'y', 'z']}
def backtrack(combination, next_digits):
# if there is no more digits to check
if len(next_digits) == 0:
# the combination is done
output.append(combination)
# if there are still digits to check
else:
# iterate over all letters which map
# the next available digit
for letter in phone[next_digits[0]]:
# append the current letter to the combination
# and proceed to the next digits
backtrack(combination + letter, next_digits[1:])
output = []
if digits: backtrack("", digits)
return output
二:
class Solution:
def letterCombinations(self, digits):
"""
:type digits: str
:rtype: List[str]
"""
m = {'': ['a', 'b', 'c'], '': ['d', 'e', 'f'], '': ['g', 'h', 'i'], '': ['j', 'k', 'l'], '': ['m', 'n', 'o'], '': ['p', 'q', 'r', 's'], '': ['t', 'u', 'v'], '': ['w', 'x', 'y', 'z']}
if not digits:
return []
ls1 = ['']
for i in digits:
ls1 = [x + y for x in ls1 for y in m[i]]
return ls1
leetcode-17-电话号码的字母组合’的更多相关文章
- Java实现 LeetCode 17 电话号码的字母组合
17. 电话号码的字母组合 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23& ...
- [LeetCode] 17. 电话号码的字母组合 ☆☆☆(回溯) ###
描述 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23"输出:[&q ...
- [LeetCode] 17. 电话号码的字母组合(回溯)
题目 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" 输出:[& ...
- [LeetCode] 17. 电话号码的字母组合
题目描述:https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/ 题目描述: 给定一个仅包含数字 2-9 的字符 ...
- LeetCode 17. 电话号码的字母组合(Letter Combinations of a Phone Number)
题目描述 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" 输出: ...
- leetcode 17电话号码的字母组合
与子集70?类似,子集每次两个分支,本题每次k个分支,子集是第一次不push第二次push元素,本题是每次都push元素,因此,本题答案的长度都为k,子集题目为各种组合: /** res,level, ...
- Leetcode之回溯法专题-17. 电话号码的字母组合(Letter Combinations of a Phone Number)
[Leetcode]17. 电话号码的字母组合(Letter Combinations of a Phone Number) 题目描述: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组 ...
- leetcode(js)算法之17电话号码的字母组合
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母 示例: 输入:"23" 输出:[" ...
- Leetcode题库——17.电话号码的字母组合
@author: ZZQ @software: PyCharm @file: letterCombinations.py @time: 2018/10/18 18:33 要求:给定一个仅包含数字 2- ...
- leetcode题目17.电话号码的字母组合(中等)
题目描述: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23"输出: ...
随机推荐
- Pandas重复值处理
import pandas as pd #生成数据 data1,data2,data3,data4=['a',3],['b',2],['a',3],['c',2] df=pd.DataFrame([d ...
- js中常用的正则表达式
我一般对正则的使用方式如下,该方法会返回一个boolean值,然后对这个返回值来进行判断 // 判断是否是整数 function isInt(num) { var reg = new RegExp(& ...
- JavaScript中this对象原理简洁说明
今天看了阮一峰大神的博客文章:JavaScript 的this原理,把纠结很久的this的指向终于理解清楚了 原文:http://www.ruanyifeng.com/blog/2018/06/jav ...
- 【转】谈一谈 Normalize.css
原文链接: https://www.jianshu.com/p/9d7ff89757fd 笔记: 如何使用?
- HttpURLConnection模拟登录学校的正方教务系统
教务系统登录界面 如图1-1 1-1 F12-->network查看登录教务系统需要参数: __VIEWSTAT txtUserName TextBox2 txtSecretCode Radio ...
- 2019_7_31python
练习 输入三条边长如果能构成三角形就计算周长和面积 import math a,b,c = input().split(',') a = float(a) b = float(b) c = floa ...
- HashMap和Hashtable有什么区别
HashMap和Hashtable都实现了Map接口,因此很多特性非常相似.但是,他们有以下不同点: HashMap允许键和值是null,而Hashtable不允许键或者值是null. Hashtab ...
- php Excel导出id
<form action="{:U('Index/files')}" method="post" enctype="multipart/form ...
- DOM——创建元素的三种方式
document.write() document.write('新设置的内容<p>标签也可以生成</p>'); innerHTML var box = document. ...
- QueryList 来做采集是什么样子
采集百度搜索结果列表的标题和链接. $data = QueryList::get('https://www.baidu.com/s?wd=QueryList') // 设置采集规则 ->rule ...