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所有可能的输出: ...
随机推荐
- Ubuntu环境安装Gradle
AndroidStudio使用全新的构建系列—–Gradle. 这是官方为什么使用gradle 的理由: Domain Specific Language (DSL) to describe and ...
- 用户级线程demo
http://blog.csdn.net/dabing69221/article/details/17426953 前言: 前几天复习了一下多线程,发现有许多网上讲的都很抽象,所以,自己把网上的一些案 ...
- ACM-ICPC (10/14)
动态规划的四个姿势 动态规划要学好,姿势一定要骚,在实战的时候,你将你的转移方程按照以下四种姿势搞一发后,一定会是耳目一新,引来萌妹子的注意~~~哈哈!!! 言归正传了!!! 之所以写动态规划优化,是 ...
- SP1811 【LCS - Longest Common Substring】
\(SAM\)上匹配 我们就是需要找到两个串的最长公共子串 先对其中一个串建出\(SAM\),之后我们把另一个串放到上面跑 如果当前在\(SAM\)的状态是\(now\),下一个字符是\(c\),匹配 ...
- sst上传和下载码云
第一次 Team-----share---->Add----->commit-------remote----->pull 第二次 直接share开始.
- c语言描述的二分插入排序法
#include<stdio.h> #include<stdlib.h> //二分插入排序法 void BinsertSort(int a[],int n){ int low, ...
- katalon安装 appium with mac 遇到的坑
1. Install Homebrew from Terminal: /usr/bin/ruby -e "$(curl -fsSL https://raw.gi ...
- jquery mobile 移动web(3)
可折叠功能块. div 元素的 data-role 属性设置为 collapsible 代码如下: <div data-role="collapsible"> < ...
- numpy的总结
一:基础篇 1)数值 import numpy as np np.set_printoptions(linewidth=200,suppress=True) a = np.array([1,2,3,4 ...
- centos7-mongodb3.4.6集群的搭建
0.需要环境 安装包:mongodb-linux-x86_64-3.4.6.tgz 安装路径:/usr/mongodb 服务器: 192.168.177.131/132/133 mongos 2000 ...