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. 最简单的nginx教程 - 如何把一个web应用部署到nginx上

    Nginx (engine x) 是一个高性能的HTTP和反向代理服务,也是一个IMAP/POP3/SMTP服务.Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Ра ...

  2. 分享一个settings.xml

    <?xml version="1.0" encoding="UTF-8"?> <settings> <localRepositor ...

  3. POJ-1759 Garland---二分+数学

    题目链接: https://cn.vjudge.net/problem/POJ-1759 题目大意: N个灯泡离地H_i,满足H1 = A ,Hi = (Hi-1 + Hi+1)/2 – 1,HN = ...

  4. BZOJ1820:[JSOI2010]Express Service 快递服务(DP)

    Description 「飞奔」快递公司成立之后,已经分别与市内许多中小企业公司签订邮件收送服务契约.由于有些公司是在同一栋大楼内,所以「飞奔」公司收件的地点(收件点)最多只有m点 (1, 2, …, ...

  5. 【[NOI2003]文本编辑器】

    题目 发现这样一句话就会导致\(T\) ch[m][0]=++m; 并不是很知道为什么,可能这是某种未定义行为在不同编译器下会有不同后果? 至于这道题就很简单了,几个有关光标位置的操作就用一个变量模拟 ...

  6. Let’s Encrypt 最近很火的免费SSL 使用教程

    2015年10月份,微博上偶然看到Let's Encrypt 推出了beta版,作为一个曾经被https虐出血的码农来说,这无疑是一个重磅消息.并且在全站Https的大趋势下,Let's Encryp ...

  7. Android学习笔记_63_手机安全卫士知识点归纳(3)分享 程序锁 服务 进程管理 widget

    1.分享: Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); shareIntent.setT ...

  8. Oracle数据库对象,同义词、序列、视图、索引

    数据库对象简介 Oracle 数据库对象又称模式对象 数据库对象是逻辑结构的集合,最基本的数据库对象是表 其他数据库对象包括: 同义词是现有对象的一个别名. 简化SQL语句 隐藏对象的名称和所有者 提 ...

  9. 菜鸟崛起 DB Chapter 5 MySQL 5.6数据库表的基本操作

    5   数据库表的基本操作 在数据库中,数据表是数据库中最重要.最基本的操作对象,是数据存储的基本单位.数据表被定义为列的集合,数据在表中是按照行和列的格式来存储的.每一行代表一条唯一的记录,每一列代 ...

  10. 29.You executed the following command to perform a backup of the USERS tablespace:

    29.You executed the following command to perform a backup of the USERS tablespace:SQL> ALTER TABL ...