给定一个数字字符串,返回数字所有可能表示的字母组合。

输入:数字字符串 "23"
输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

详见:https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/

实现语言:Java

class Solution {
public List<String> letterCombinations(String digits) {
List<String> result =new ArrayList<>();
if(digits == null || digits.isEmpty()){
return result;
}
result.add("");
String []btns = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
for(int i =0 ; i < digits.length() ;i++){
List<String> tmp = new ArrayList<>();
String letter = btns[digits.charAt(i)-'0'];
//遍历上一个列表,取出每一个元素,并和新的元素的每一个字符加起来保存
for(int j = 0 ; j < result.size();j++){
//遍历当前数字对应的所有字符
for(int k = 0; k< letter.length(); k++){
tmp.add(result.get(j)+letter.charAt(k));
}
}
result = tmp;
}
return result;
}
}

参考:https://vvaaiinn.iteye.com/blog/2208353

https://www.cnblogs.com/kepuCS/p/5271654.html

017 Letter Combinations of a Phone Number 电话号码的字母组合的更多相关文章

  1. [LintCode] Letter Combinations of a Phone Number 电话号码的字母组合

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

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

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

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

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

  4. 【LeetCode】17. Letter Combinations of a Phone Number 电话号码的字母组合

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:电话号码, 字母组合,回溯法,题解,leetcode, ...

  5. lintcode 中等题:Letter Combinations of a Phone Number 电话号码的字母组合

    题目 电话号码的字母组合 给一个数字字符串,每个数字代表一个字母,请返回其所有可能的字母组合. 下图的手机按键图,就表示了每个数字可以代表的字母. 样例 给定 "23" 返回 [& ...

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

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

  7. Leetcode17.Letter Combinations of a Phone Number电话号码的字母组合

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

  8. 【JAVA、C++】LeetCode 017 Letter Combinations of a Phone Number

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

  9. 【LeetCode】017. Letter Combinations of a Phone Number

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

随机推荐

  1. [转]HTTP头的Expires与Cache-control

    1.概念 Cache-control用于控制HTTP缓存(在HTTP/1.0中可能部分没实现,仅仅实现了Pragma: no-cache) 数据包中的格式: Cache-Control: cache- ...

  2. 办公软件-Excel:Microsoft Office Excel 2003百科

    ylbtech-办公软件-Excel:Microsoft Office Excel 2003百科 Microsoft® Office Excel 2003 是一种电子表格程序,可提供对于 XML 的支 ...

  3. 面向对象要点(this关键字)

    package day07; public class ThisKeywords { private String name; private void Foo(String name) { this ...

  4. mybatis 学习四 源码分析 mybatis如何执行的一条sql

    总体三部分,创建sessionfactory,创建session,执行sql获取结果 1,创建sessionfactory      这里其实主要做的事情就是将xml的所有配置信息转换成一个Confi ...

  5. 大内存电脑在vbox安装linux报错

    问题描述: 1.机器:Linux主机,特别是主机为大内存,比如: 4G内存的使用pae内核的Ubuntu系统的thinkpad电脑. 2.情况:使用VirtualBox安装Linux系统时,比如:通过 ...

  6. xdu2017校赛F

    Problem F Dogs of Qwordance Senior Backend R&D Engineers 问题描述 那年夏天,锘爷和杰师傅漫步在知春公园的小道上.他们的妻子.孩子牵 着 ...

  7. VS编译器中设置 输出窗口 只显示error,不显示warning 要如何配置

    VS编译器中设置 输出窗口 只显示error,不显示warning 要如何配置 在编译大型项目的时候,总是VS编译器的输出窗口总是会出现一堆warning警告,要想在里面找到error错误,要使用鼠标 ...

  8. Learning Python 001 第一个程序

    Python 第一个程序 我使用的开发工具是PyCharm软件.我们使用的是Python3.5 for windows . 如果你还没有安装PyCharm软件 和 Python3.5,请到这里来看如果 ...

  9. CSS学习系列3--CSS3中的box-shadow的使用

    在CSS中,text-shadow是给文本添加阴影效果. box-shadow则是给元素块添加周边阴影效果. box-shadow基本的语法形式如下 box-shadow: [inset] x-off ...

  10. AutoCAD 2014 在Server 2008 中 十字光标很慢

    另外,请在你们的Windows Server上增加如下注册表: On      both Windows Server 2008 R2 and Windows Server 2012 create E ...