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. Java学习个人备忘录之多态

    对象的多态性 class 动物 {} class 猫 extends 动物 {} class 狗 extends 动物 {} 猫 x = new 猫();//意思是建立本类的对象 new 猫(),并通 ...

  2. ACM hust 2.1

    来自咸鱼王的呻吟 http://www.xiami.com/song/3599639?spm=a1z1s.3521865.23309997.1.PbLu7E 配合咸鱼食用效果更佳(右键新窗口打开) 题 ...

  3. maven仓库中添加自定义的包jar包

    mvn install:install-file -DgroupId=impl -DartifactId=center -Dversion=1.0 -Dpackaging=jar -Dfile=D:\ ...

  4. [转]MATLAB cell数据类型

    细胞型数据类型(cell)使不同类型和不同维数的数组可以共存,细胞型数组实际上可以认为是一种以任意形式的数组为分量的多维数组. 1.细胞型数据的定义 1)直接赋值定义:细胞型变量在定义时需要使用大括号 ...

  5. RT-thread内核之系统时钟

    一.系统时钟 rt-thread的系统时钟模块采用全局变量rt_tick作为系统时钟节拍,该变量在系统时钟中断函数中不断加1.而系统时钟中断源和中断间隔一般由MCU硬件定时器(如stm32的嘀嗒定时器 ...

  6. RESTful Webservice

    1,REST和RESTFUL是什么? REST ( REpresentational State Transfer ),State Transfer 为 "状态传输" 或 &quo ...

  7. BZOJ4567:[SCOI2016]背单词——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=4567 Lweb 面对如山的英语单词,陷入了深深的沉思,“我怎么样才能快点学完,然后去玩三国杀呢?” ...

  8. BZOJ1027 [HNOI2004]打鼹鼠 【dp】

    1207: [HNOI2004]打鼹鼠 Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 3647  Solved: 1746 [Submit][Sta ...

  9. 20165218 2017-2018-1 《Java程序设计》第三周学习总结

    20165218 2017-2018-1 <Java程序设计>第三周学习总结 教材学习内容总结-第四章 类与对象 面向对象语言 需要完成某种任务时,首先要想到,谁去完成任务,即哪个对象去完 ...

  10. javascript实用例子

    js学习笔记,别错过!很有用的. /////////////////////////////////////////////////////////////////////////////////// ...