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

输入:数字字符串 "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. 非系统表空间损坏,rman备份恢复

    实验条件:有完整可用备份--查询表空间情况SQL> select tablespace_name,status from dba_tablespaces;TABLESPACE_NAME STAT ...

  2. Maven(2)-坐标和依赖

    本文简要介绍Maven里面的坐标(coodinate)以及maven依赖管理(Dependency) 一.坐标 先来个截图: 在上图peoject栏目有groupId,artifactId,versi ...

  3. 第二次C语言实验报告

    #一.设计题目,设计思路,实现方法 ##设计题目 15-10 找最长的字符串,14-5 指定位置输出字符串,13-6 数组循环右移,12-5 查找指定字符,11-5 打印杨辉三角. ##设计思路 15 ...

  4. shell入门-sed-1

    sed这个工具比grep复杂一点,功能比grep复杂一点 功能也能指定匹配的行,不能颜色显示 sed 基础功能 [root@wangshaojun ~]# sed -n '10'p 1.txtuucp ...

  5. spring 4.0 JUnit简单的Dao,Service测试

    1.AbstractTransactionalJUnit4SpringContextTests 和AbstractJUnit4SpringContextTests.我们在测试用例类要继承两种中的一个. ...

  6. JS中双击和单击事件冲突解决

    在JS中代码中同一功能块中通常同时会用到单击.双击事件,但通常会遇到一个问题,就是在双击的时候即执行了一次双击事件,而且还执行了两次单击事件.此类冲突在ZTree.DHTMLX中经常遇到. 想要解决两 ...

  7. 【机器学习】关联规则分析(一):Apriori

    一.Apriori原理 Apriori是关联分析中较早的一种方法,主要用来挖掘那些频繁项集合,其思想是: 1.如果一个项目集合不是频繁集合,那么任何包含它的项目(超集)也一定不是频繁集. 2.如果一个 ...

  8. java多线程有几种实现方法,都是什么?

    转自:http://www.cnblogs.com/liujichang/p/3150387.html 多线程有两种实现方法,分别是继承Thread类与实现Runnable接口 同步的实现方法有两种, ...

  9. 25. CTF综合靶机渗透(17)

    靶机链接 https://www.vulnhub.com/entry/the-ether-evilscience,212 运行环境 本靶机提供了VMware的镜像,从Vulnhub下载之后解压,运行v ...

  10. hdu1059

    #include <stdio.h> #include <string.h> #define MAXN 120005 int main() { int num[7]; int ...