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. UVa 1586 - Molar Mass - ACM/ICPC Seoul 2007 - C语言

    关键在于判断数字是两位数还是单位数,其他部分没有难度. #include"stdio.h" #include"string.h" #include"c ...

  2. c# 批量处理数据录入

    c# 分批处理数据录入 //using System.Text; //using System.Data; //using System.Data.SqlClient; //using System; ...

  3. Fluent Python: @property

    Fluent Python 9.6节讲到hashable Class, 为了使Vector2d类可散列,有以下条件: (1)实现__hash__方法 (2)实现__eq__方法 (3)让Vector2 ...

  4. 拓扑排序(Toposort)

    摘自:https://blog.csdn.net/qq_35644234/article/details/60578189 <图论算法> 1.拓扑排序的介绍 对一个有向无环图(Direct ...

  5. c++反射概念-简单介绍

    C++ 反射机制的简单实现 C++并不支持反射机制,只能自己实现. 如果需要实现字字符串到函数到映射,一定要使用到函数指针. 简单实现反射机制,根据字符串来构造相应到类.主要有以下几点: (1) 可以 ...

  6. ChromeSwitchySharp代理设置步骤

    步骤: 1.新增情景模式配置如下:   2.设置切换规则   3.先“直接连接”进行登录:然后切换到“自动切换模式”访问对应url

  7. c# 委托初窥

    1.委托可以把方法当作参数在另一个方法中传递和调用 ,委托是方法的快捷方式. 2.委托是一个类. private void BeginSocketThread() { try { IPEndPoint ...

  8. CSS3制作各种形状图像(转)

    CSS3制作各种形状图像 浏览:1417 | 更新:2015-05-24 14:43 | 标签:css 1 2 3 4 5 6 7 分步阅读 圆形-椭圆形-三角形-倒三角形=左三角形-右三角形-菱形- ...

  9. JTS空间分析工具包(GIS开源)学习 JAVA

    JST空间分析工具包是一套JAVA API,提供一系列的空间数据分析操作.最近开发项目刚好需要用到,上网搜资料也少,就自己写下来记录一下.C++版本的拓扑分析开源工具叫:geos:.NET版本的拓扑分 ...

  10. VUE01指令

    一.下载Vue2.0的两个版本: 官方网站:http://vuejs.org/ 开发版本:包含完整的警告和调试模式 生产版本:删除了警告,进行了压缩 二.项目结构搭建 这个部分要视频中有详细讲解. 三 ...