给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。

给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。

示例:

输入:"23"
输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
转自:https://blog.csdn.net/xushiyu1996818/article/details/84334799
class Solution {
HashMap<Character,char[]> map = new HashMap<>();
List<String> result = new ArrayList<>();
public List<String> letterCombinations(String digits) {
int length = digits.length();
if(length == 0){
return result;
}
map.put('2',new char[]{'a','b','c'});
map.put('3', new char[]{'d','e','f'});
map.put('4', new char[]{'g','h','i'});
map.put('5', new char[]{'j','k','l'});
map.put('6', new char[]{'m','n','o'});
map.put('7', new char[]{'p','q','r','s'});
map.put('8', new char[]{'t','u','v'});
map.put('9', new char[]{'w','x','y','z'});
combine("",digits);
return result;
}
public void combine(String nowStr,String remain){
int length = remain.length();
if(length == 0){
result.add(nowStr);
return;
}
Character now = remain.charAt(0);
for(char nowChar:map.get(now)){
combine(nowStr+nowChar,remain.substring(1));
}
} }

2019-04-15 22:48:47

23  => ["ad","bd","cd","ae","be","ce","af","bf","cf"]

234=> ["adg","bdg","cdg","aeg","beg","ceg","afg","bfg","cfg","adh"。。。。。。]

 class Solution:
def letterCombinations(self, digits):
self.dict = {"":"abc", "":"def", "":"ghi", "":"jkl", "":"mno", "":"pqrs","":"tuv","":"wxyz"}
if digits == "":
return []
result = [""]
for digit in digits:
strs = self.dict[digit]
curResult = []
for char in strs:
for res in result:
curResult.append(res+char)
result = curResult
return result

2019-11-28 14:13:09

LeetCode--017--电话号码的字母组合(java)的更多相关文章

  1. Java实现 LeetCode 17 电话号码的字母组合

    17. 电话号码的字母组合 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23& ...

  2. [LeetCode] 17. 电话号码的字母组合

    题目描述:https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/ 题目描述: 给定一个仅包含数字 2-9 的字符 ...

  3. [LeetCode] 17. 电话号码的字母组合 ☆☆☆(回溯) ###

    描述 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23"输出:[&q ...

  4. [LeetCode] 17. 电话号码的字母组合(回溯)

    题目 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" 输出:[& ...

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

    题目描述 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" 输出: ...

  6. 【LeetCode】电话号码的字母组合

    [问题]给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:" 输出:["ad ...

  7. leetcode 17电话号码的字母组合

    与子集70?类似,子集每次两个分支,本题每次k个分支,子集是第一次不push第二次push元素,本题是每次都push元素,因此,本题答案的长度都为k,子集题目为各种组合: /** res,level, ...

  8. [LeetCode] 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]17. 电话号码的字母组合(Letter Combinations of a Phone Number) 题目描述: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组 ...

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

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

随机推荐

  1. Codeforces 706C - Hard problem - [DP]

    题目链接:https://codeforces.com/problemset/problem/706/C 题意: 给出 $n$ 个字符串,对于第 $i$ 个字符串,你可以选择花费 $c_i$ 来将它整 ...

  2. 表驱动方法(Table-Driven Methods)

    表驱动方法(Table-Driven Methods) - winner_0715 - 博客园 https://www.cnblogs.com/winner-0715/p/9382048.html W ...

  3. mysql 用户及权限

    永远不要给任何人(除了MySQL root帐户)访问 数据库中的 user表mysql!这很关键. mysql.user表存放所有用户的主机,用户名,密码,权限.直接修改表中信息,需flush pri ...

  4. Fiddler设置断点修改Request和Response

    一.Fiddler中修改Request有两种方法:  点击Rules-> Automatic Breakpoint ->Before Requset (这种方法会中断所有的会话) 消除命令 ...

  5. python 转换容量单位 实现ls -h功能

    功能1 把字节转换自适应转为其他单位(ls -h),超过1024投入高一级的区间,不足1024投入本级区间,如1000K是一个合理值,1030K就应该转换为1M,2050K应该转换为2M 功能2 把其 ...

  6. outlook2016用Exchange轻松绑定腾讯企业邮箱

    系统版本:Win10 X64 1709 英文版 邮箱:Outlook2016 背景知识: 1.发送邮件均使用SMTP协议(SMTP 全称“Simple Mail Transfer Protocol”, ...

  7. 实验一:C++简单程序设计

    [实验一] #2-28 实现一个简单的菜单程序,运行时显示“Menu:A(dd) D(elete) S(ort) Q(uit),Selete one:”提示用户输入.A表示增加,D表示删除,S表示排序 ...

  8. java求最大公约数,和最小公倍数

    import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner sc = ...

  9. C#:进程

    using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...

  10. supervisor 守护进程

    一.supervisor 安装 1.yum -y install epel-release 2.yum -y install supervisor 二.supervisor 配置文件详解 三.supe ...