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.

题目大意:给一组数字,按照电话键盘上的组合输出所有的可能。

解题思路:直接递归写DFS,输出所有组合。

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; public class LetterCombinationsofaPhoneNumber { public static void main(String[] sure) {
new LetterCombinationsofaPhoneNumber().letterCombinations("213");
} static Map<Character, String> mapping = new HashMap<>(); static {
mapping.put('0', "");
mapping.put('1', "");
mapping.put('2', "abc");
mapping.put('3', "def");
mapping.put('4', "ghi");
mapping.put('5', "jkl");
mapping.put('6', "mno");
mapping.put('7', "pqrs");
mapping.put('8', "tuv");
mapping.put('9', "wxyz");
} public List<String> letterCombinations(String digits) {
List<String> res = new ArrayList<>();
if (digits == null || digits.length() == 0) {
return res;
}
combine(digits, "", res, digits.length());
System.out.println(res);
return res;
} private void combine(String digits, String tmp, List<String> res, int length) {
if (length == 0) {
res.add(tmp);
return;
}
for (int i = 0; i < digits.length(); i++) {
String val = mapping.get(digits.charAt(i));
if ("".equals(val)) {
combine(digits.substring(i + 1), tmp, res, length - 1);
} else {
for (char c : val.toCharArray()) {
combine(digits.substring(i + 1), tmp + c, res, length - 1);
}
}
}
} }

Letter Combinations of a Phone Number——LeetCode的更多相关文章

  1. Letter Combinations of a Phone Number - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Letter Combinations of a Phone Number - LeetCode 注意点 可以不用按字典序排序 解法 解法一:输入的数字逐 ...

  2. Letter Combinations of a Phone Number leetcode java

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

  3. [LeetCode][Python]17: Letter Combinations of a Phone Number

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...

  4. LeetCode解题报告—— Container With Most Water & 3Sum Closest & Letter Combinations of a Phone Number

    1.  Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a ...

  5. Leetcode之回溯法专题-17. 电话号码的字母组合(Letter Combinations of a Phone Number)

    [Leetcode]17. 电话号码的字母组合(Letter Combinations of a Phone Number) 题目描述: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组 ...

  6. 【leetcode】Letter Combinations of a Phone Number

    Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...

  7. 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  8. LeetCode: Letter Combinations of a Phone Number 解题报告

    Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...

  9. [LeetCode]Letter Combinations of a Phone Number题解

    Letter Combinations of a Phone Number: Given a digit string, return all possible letter combinations ...

随机推荐

  1. Java基础知识强化之IO流笔记21:FileInputStream读取数据

    1. 字节输入流的操作步骤: (1)创建字节输入流的对象 (2)调用read()方法读取数据,并把数据显示到控制台 (3)关闭字节输入流的对象资源 2. FileInputStream构造: File ...

  2. python之 sys.exit() os._exit() exit() quit()的简单使用

    python之sys.exit() os._exit() exit() quit()的简单使用 1>sys.exit() >>> import sys>>> ...

  3. C#HttpWebResponse请求常见的状态码

    成员名称 说明 Continue 等效于 HTTP 状态 100.Continue 指示客户端可能继续其请求. SwitchingProtocols 等效于 HTTP 状态 101.Switching ...

  4. 重装系统时,将MBR分区转为GPT 分区

    摘要 很多同学在重装系统的时候,或多或少都遇到过这样的问题:镜像文件没有问题,软碟通刻录也没有问题,但偏偏就在选择安装系统盘盘符的时候,跳出对话框,提示:Windows无法安装到这个磁盘,选中的磁盘具 ...

  5. WPF DataGrid 绑定DataSet数据 自动生成行号

    1.绑定数据:dataGrid1.ItemsSource = dataSet.Tables[0].DefaultView; 注意:在创建DataGrid 时可以通过AutoGenerateColumn ...

  6. Oracle Pivot学习心得

    今天在做一个查询报表需要将多行的查询结果转换成一行,数据格式如下 ID   Type Parameter Value Machine_NO Operator UpdateTime 1 11111111 ...

  7. SqlServer CTE 递归查询 Oracle递归查询

    在做数据库设计这块,很多时候表的数据模型就是典型的二叉树结构. 于是在查询数据的时候,就涉及到了数据的递归查询. 递归查询分为两种:1.从根节点查询自身以及所有的子节点:2.从子节点查询自身以及所有的 ...

  8. RSA加密算法正确性证明

    RSA加密算法是利用大整数分解耗时非常大来保证加密算法不被破译. 密钥的计算过程为:首先选择两个质数p和q,令n=p*q. 令k为n的欧拉函数,k=ϕ(n)=(p−1)(q−1) 选择任意整数a,保证 ...

  9. C++ 实现网络爬虫

    吐槽 前天心血来潮, 把自己面试经历下了下来. 我觉得自己求职一路来比较心酸, 也付出了比一般人更多的汗水. 本以为写出来, 好歹可以作为一篇励志故事. 得到的评论却是, 语言只是一门工具. ||| ...

  10. C# 判断字符串是否可以转化为数字

    C# 判断字符串是否可以转化为数字 /// <SUMMARY> /// 判断字符串是否可以转化为数字 /// </SUMMARY> /// <PARAM name=&qu ...