1. 原题链接

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

2. 题目要求

给定一个数字字符串digits,每一个数字对应拨号键盘上的数字,每个数字又对应不同的字母。例如“3”对应“d“、“e”、“f”三个字母。输出digits所含数字对应的所有字母组合。

例如,digits="23",输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]

3. 解题思路

思路一:采用多重for循环暴力解决,但是时间复杂度为O(nx),x为digits字符串中包含的数字个数。

思路二:使用队列的思想,首先建立一个空的LinkedList列表对象res,在res中加入“”,避免第一次for循环时从res中取出对象时报空指针异常

使用for循环,用peek( )方法从res中将列表的头元素取出,并将digits的第一个数字对应的字母依次插入头元素后面;最后重新加入到res中。

4.代码实现

import java.util.LinkedList;
import java.util.List; public class LetterCombinationsOfPhoneNumber17 {
public static void main(String[] args) {
List<String> ls = LetterCombinationsOfPhoneNumber17.letterCombinations("23");
for (String str : ls) {
System.out.println(str);
}
} public static List<String> letterCombinations(String digits) {
LinkedList<String> res = new LinkedList<>();
String[] mapping = new String[]{"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; if (digits.length() != 0) { // 入过digits为空直接返回空的res
res.add("");
for (int i = 0; i < digits.length(); i++) {
int x = Character.getNumericValue(digits.charAt(i));
while (res.peek().length() == i) { // 判断该次组合是否完成
String t = res.remove(); // 从队列中取出队头元素,先进先出
for (char s : mapping[x].toCharArray()) { // 将该数组对应的字符串转换成字符数组,进行遍历
System.out.println("t+s:"+t + s);
res.add(t + s); // 在原有字符串后加入新的字符,然后重新加入队列
}
}
}
return res;
} else {
return res;
}
}
}

  

LeetCode:17. Letter Combinations of a Phone Number(Medium)的更多相关文章

  1. [LeetCode][Python]17: Letter Combinations of a Phone Number

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...

  2. 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  3. 【一天一道LeetCode】#17. Letter Combinations of a Phone Number

    一天一道LeetCode (一)题目 Given a digit string, return all possible letter combinations that the number cou ...

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

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

  5. [LeetCode] Letter Combinations of a Phone Number(bfs)

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

  6. 【LeetCode】17. Letter Combinations of a Phone Number

    题目: 思路:设置两个List,一个存储当前层,一个存储最终层 public class Solution { public List<String> letterCombinations ...

  7. LeetCode 17 Letter Combinations of a Phone Number (电话号码字符组合)

    题目链接 https://leetcode.com/problems/letter-combinations-of-a-phone-number/?tab=Description HashMap< ...

  8. 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 ...

  9. 刷题17. Letter Combinations of a Phone Number

    一.题目说明 题目17. Letter Combinations of a Phone Number,题目给了下面一个图,输入一个字符串包括2-9,输出所有可能的字符组合. 如输入23所有可能的输出: ...

随机推荐

  1. python入门3 python变量,id(),is运算符

    python变量无需声明数据类型,可以直接赋值使用. 比如: num=100 #整数 str="字符串" #字符串 turple1 =('mon','tue','wed','thu ...

  2. Python语言程序设计基础(5)—— 函数和代码复用

    lambda sum = lambda x,y : x + y print(sum(3,3),type(sum)) 默认参数 def prints(str,times = 2) : print(str ...

  3. netbackup 8.1安装前注意事项

    一.NetBackup 主服务器的 Web 服务器用户/组设置步骤 文章 ID:100034818 上次发布时间:2017-08-24 产品:NetBackup   问题 从 NetBackup 8. ...

  4. 创建一个dynamics 365 CRM online plugin (十二) - Asynchronous Plugins

    这篇是plugin的终结. 通过之前的11期我们应该发现了plugin其实学习起来不难. async plugin 是把plugin的功能async run起来. e.g.  我们之前做过的preOp ...

  5. 过拟合是什么?如何解决过拟合?l1、l2怎么解决过拟合

    1. 过拟合是什么? https://www.zhihu.com/question/264909622    那个英文回答就是说h1.h2属于同一个集合,实际情况是h2比h1错误率低,你用h1来训练, ...

  6. SpringMVC学习记录六——异常定义和上传图片

    19       异常处理 19.1      异常处理思路 系统中异常包括两类:预期异常和运行时异常RuntimeException,前者通过捕获异常从而获取异常信息,后者主要通过规范代码开发.测试 ...

  7. 使用dva框架的总结

    最近的项目是react+dva+atd+webpack的一个后台项目,刚接触dva就感觉很喜欢,很简洁用着很爽. 关于使用redux中的一些问题 1.文件切换问题. redux的项目通常哟啊分为red ...

  8. Interview Question Overload、Refactoring和Override?

    Overload Overload我们百度翻译知道是超载的意思,不过我们一般称其为重载,在这里我们不纠结于它的翻译,我们来讲讲重载是什么意思,重载的好处.在下面我们以Overload来代表重载(为了记 ...

  9. js事件委托代码优化【感悟总结】

    前两天接手了同事的一个项目,是一个网站首页,其中有段代码很累赘,要实现的功能就是, 通过给父元素添加鼠标移入移出事件,来控制子元素显示隐藏. html代码,一共有四个父元素div,每个父元素嵌套一个子 ...

  10. spring入门(六) spring mvc+mybatis

    1.引入依赖 <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> < ...