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

A mapping of digit to letters (just like on the telephone buttons) is given below.

Input: Digit string "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.

解法1:

  采用队列的方法,遍历digits的每一位数字,对于遍历到的数字,将队列中所有的字符串从头部移除,加上当前数字对应的字母后依次添加到队列后端。

public class Solution {
public List<String> letterCombinations(String digits) {
List<String> res = new ArrayList<>(); if (digits == null || digits.length() == 0) {
return res;
} String[] letters = new String[]{"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
res.add(""); for (int i = 0; i < digits.length(); i++) {
int size = res.size();
String str = letters[digits.charAt(i) - '2'];
for (int j = 0; j < size; j++) {
String front = res.remove(0); // 不是remove(j),每次都应该移除第一个字符串
for (int k = 0; k < str.length(); k++) {
res.add(front + str.charAt(k));
}
}
}
return res;
}
}

解法2:

  采用递归的方法,每次添加一个字母后进行下一次递归:

public class Solution {
public List<String> letterCombinations(String digits) {
List<String> res = new ArrayList<>(); if (digits == null || digits.length() == 0) {
return res;
} String[] letters = new String[]{"abc", "def", "ghi", "jkl", "mno", "pqrs", "utv", "wxyz"};
helper(res, letters, digits, "");
return res;
} public void helper(List<String> res, String[] letters, String digits, String temp) {
if (digits.length() == 0) {
res.add(temp);
return;
}
String str = letters[digits.charAt(0) - '2'];
for (int i = 0; i < str.length(); i++) {
helper(res, letters, digits.substring(1), temp + str.charAt(i));
}
}
}

解法2:

[LeetCode] 17. Letter Combinations of a Phone Number ☆☆的更多相关文章

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

  2. [LeetCode] 17. Letter Combinations of a Phone Number 电话号码的字母组合

    Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...

  3. [leetcode 17]Letter Combinations of a Phone Number

    1 题目: Given a digit string, return all possible letter combinations that the number could represent. ...

  4. Java [leetcode 17]Letter Combinations of a Phone Number

    题目描述: Given a digit string, return all possible letter combinations that the number could represent. ...

  5. Leetcode 17.——Letter Combinations of a Phone Number

    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手机键盘的字母组合

    Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...

  7. [LeetCode]17. Letter Combinations of a Phone Number电话号码的字母组合

    Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...

  8. LeetCode——17. Letter Combinations of a Phone Number

    一.题目链接: https://leetcode.com/problems/letter-combinations-of-a-phone-number/ 二.题目大意: 给定一段数字字符串,其中每个数 ...

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

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

随机推荐

  1. 2.重新安装CM服务

    步骤1.停止CM服务2.删除CM服务3.添加CM服务4.测试数据库 步骤 1.停止CM服务 2.删除CM服务 没有发现可以单独删除某一项CM服务,必须全部删除 3.添加CM服务 4.测试数据库 如果报 ...

  2. Sublime Text 插件之:MarkDown

    Sublime Text 插件之:MarkDown 喜欢写文档的同学应该离不开 MarkDown ,ST(Sublime Text)的插件 Markdown Preview 就支持实时在浏览器中预览p ...

  3. jquery中的$(document).ready()、JavaScript中的window.onload()以及body中的onload()、DomContentLoaded()区别

    $().ready().$(handler).$(document).ready(handler)均不是原生JS中的,都是jQuery中封装的方法.这些事件在当页面的dom节点加载完毕后就执行,无需等 ...

  4. POJ 1995 (快速幂)

    这道题普通做法会发生溢出且会超时,应当用快速幂来求解. 快速幂讲解 #include <cstdio> #include <cmath> using namespace std ...

  5. pthon_flask小汇总

    一.Jinja2中的关键字 1.include关键字 用include可以导入另外一个模板到当前模板中 <pre> {% include 'header.html' %} Body {% ...

  6. 2019寒假训练营第三次作业part2 - 实验题

    热身题 服务器正在运转着,也不知道这个技术可不可用,万一服务器被弄崩了,那损失可不小. 所以, 决定在虚拟机上试验一下,不小心弄坏了也没关系.需要在的电脑上装上虚拟机和linux系统 安装虚拟机(可参 ...

  7. P4语法(1)基础数据类型和Header

    文章学习自:P4语言编程详解 由于原文有一点的年份,所以也继续阅读了相关的最新规范. P4语言规范 基础数据类型 布尔型(bool) 运算符 描述 and 双目运算符,结果为布尔型 or 双目运算符, ...

  8. 第二次作业 编程题 PAT 1001A+B Format

    Github的object-oriented仓库:1001.A+BFormat(20) 1.解题的思路过程 在之前学习C语言时曾经碰到过类似的将数字转换成字符输出的情况,这道题目要求输出的数字每三个间 ...

  9. 菜鸟的飞翔日记-os篇

    一轮王道os复习感想 1概述 虽然去年有上操作系统这门必修课,考的成绩也算理想,本来还有点沾沾自喜,嗯,觉得自己学的还不错,知道有一天我拿起了王道,(没给王道打广告)看王道的原因完全在于为考研做准备, ...

  10. 播放MP3

    播放背景音乐 上文来自:http://blog.csdn.net/henulwj/article/details/8977738 using System; using System.Collecti ...