Letter Combinations of a Phone Number——LeetCode
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的更多相关文章
- Letter Combinations of a Phone Number - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Letter Combinations of a Phone Number - LeetCode 注意点 可以不用按字典序排序 解法 解法一:输入的数字逐 ...
- Letter Combinations of a Phone Number leetcode java
题目: Given a digit string, return all possible letter combinations that the number could represent. A ...
- [LeetCode][Python]17: Letter Combinations of a Phone Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...
- 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 ...
- Leetcode之回溯法专题-17. 电话号码的字母组合(Letter Combinations of a Phone Number)
[Leetcode]17. 电话号码的字母组合(Letter Combinations of a Phone Number) 题目描述: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组 ...
- 【leetcode】Letter Combinations of a Phone Number
Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...
- 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- LeetCode: Letter Combinations of a Phone Number 解题报告
Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...
- [LeetCode]Letter Combinations of a Phone Number题解
Letter Combinations of a Phone Number: Given a digit string, return all possible letter combinations ...
随机推荐
- Android 网络框架Volley的使用
Volley简介 在平时的开发过程中,我们的应用几乎总是在和网络打交道, 在android下的网络编程一般都是基于Http协议的 ,常见的是HttpURLConnection和HttpClient 两 ...
- 模板-->中国剩余定理[互质版本]
如果有相应的OJ题目,欢迎同学们提供相应的链接 相关链接 所有模板的快速链接 扩展欧几里得extend_gcd模板 poj_1006_Biorhythms,my_ac_code 简单的测试 None ...
- bzoj 1034 (田忌赛马++)
/* 这类题的最优策略: 自己最好的干掉对方最好的 或者 自己最差的干掉对方最差的 不能的话 用自己最差的 对阵对方最好的 这样是最优的 实现嘛 搞两个队列跑一跑 */ #include<ios ...
- ADLINK 8158控制程序-连续运动(VB.NET)
运动平台:日脉的二维运动平台(一个旋转平台和一个滑动平台) 开发环境:VS2010 + .NET Framework + VB.NET 使用文件:pci_8158.vb motion_8158_2D. ...
- 【开源java游戏框架libgdx专题】-07-文件处理
介绍:文件处理在不同平台的文件管理是略有差异的 Desktop(Windows,Linux,Mac OS X等等):在桌面系统中,文件系统是一个大块的内存.文件可以通过当前的工作目录或者绝对路径被引用 ...
- 自定义鼠标Cursor转变成图片
今天无意做到项目遇到一个好玩的事情,就是当我鼠标移到一个链接上面,并不是像正常那样出现一个小手,而是变成一个小十字架, 下面看图当时第一眼看到总感觉哪里不对,噢噢噢噢 这样的 小手没了,居然是一个图片 ...
- 关于asp.net中cookie在调试过程中读写正常发布后乱码问题
最近在做的项目发布后出现了乱码的问题,既然出现了乱码很大的可能性是跟编码有关系,所以首先的解决方案就是重新对cookie进行编码, 在写入的cookie的时候编码,在读取的时候解码 在写入cookie ...
- c - 递归年龄
/* 题目:有 5 个人坐在一起,问第五个人多少岁?他说比第 4 个人大 2 岁.问第 4 个人岁数,他说比第3 个人大 2 岁.问第三个人,又说比第 2 人大两.问第 2 个人,说比第一个人大两岁. ...
- .net判断用户使用的是移动设备还是PC
using System.Text.RegularExpressions;//头部引入正则的命名空间 //为了加强准确性,防止支持wap的浏览器如opera,加入操作系统验证.openwave|后为p ...
- iOS-点击视图,视图背景颜色随机更改
一.效果图 二.代码 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the v ...