LeetCode:17. Letter Combinations of a Phone Number(Medium)
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)的更多相关文章
- [LeetCode][Python]17: Letter Combinations of a Phone Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...
- 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【一天一道LeetCode】#17. Letter Combinations of a Phone Number
一天一道LeetCode (一)题目 Given a digit string, return all possible letter combinations that the number cou ...
- 【LeetCode】17. Letter Combinations of a Phone Number 电话号码的字母组合
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:电话号码, 字母组合,回溯法,题解,leetcode, ...
- [LeetCode] Letter Combinations of a Phone Number(bfs)
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- 【LeetCode】17. Letter Combinations of a Phone Number
题目: 思路:设置两个List,一个存储当前层,一个存储最终层 public class Solution { public List<String> letterCombinations ...
- LeetCode 17 Letter Combinations of a Phone Number (电话号码字符组合)
题目链接 https://leetcode.com/problems/letter-combinations-of-a-phone-number/?tab=Description HashMap< ...
- 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 ...
- 刷题17. Letter Combinations of a Phone Number
一.题目说明 题目17. Letter Combinations of a Phone Number,题目给了下面一个图,输入一个字符串包括2-9,输出所有可能的字符组合. 如输入23所有可能的输出: ...
随机推荐
- 团队第四次SCrum
scrum 第四次冲刺 一.项目目的 为生活在长大的学生提供方快捷的生活服务,通过帖子发现自己志同道合的朋友,记录自己在长大点滴.本项目的意义在于锻炼团队的scrum能力,加强团队合作能 ...
- python入门19 异常及异常处理 异常捕获
常见异常举例 """ 一些异常举例:""" '''模块不存在 ModuleNotFoundError: No module named 'd ...
- 搭建packagist私服和composer
1.下载源码 https://github.com/composer/packagist 2.修改配置文件 cp app/config/parameters.yml.dist app/config/ ...
- IOS 社交分享
#import <Social/Social.h> @interface HMViewController () @end @implementation HMViewController ...
- redux创建store,处理更新数据
如果我们想使用redux,第一步需要通过 yarn add redux 来安装redux 安装成功后就需要去创建一个store,怎么创建呢,非常的简单,在src下面创建一个文件夹,这个文件夹名字就叫做 ...
- 程序的优化(PHP)
有些小细节往往容易被人忽视.有时候常常说优化代码优化代码,但是实际操作的时候,最容易被忽视的如下所示: echo 比 print 快. 使用echo的多重参数代替字符串连接. 在执行for循环之前确定 ...
- iText生成PDF 格式报表
1.导包 <dependency> <groupId>com.lowagie</groupId> <artifactId>itext</artif ...
- papers-06-02
午睡被同事吵醒,只好干活.看到微信公众号有一篇文章说老朋友呢,点进去发现原来相关的工作好多,而且好新好细致. 微信的文章可以见这里: 探究最陌生的老朋友Softmax 里面的几篇文章可以看看. Lar ...
- Python 学习笔记(七)Python字符串(二)
索引和切片 索引 是从0开始计数:当索引值为负数时,表示从最后一个元素(从右到左)开始计数 切片 用于截取某个范围内的元素,通过:来指定起始区间(左闭右开区间,包含左侧索引值对应的元素,但不包含右测 ...
- [Linux]文件浏览
1.使用file命令查看文件中数据的类型 [oracle@linuxforlijiaman Desktop]$ ls linux oracle.txt test.png [oracle@linuxfo ...