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);
}
}
}
} }

最新文章

  1. javascript之闭包理解以及应用场景
  2. JQuery判断元素是否存在
  3. Ratatype - 在线打字教程,提高打字速度
  4. 在Eclipse中使用MAT分析Android程序内存使用状况(转)
  5. css清楚浮动的几种常用方法
  6. UNIX/Linux网络编程基础:图解TCP/IP协议栈
  7. C# 打印文件
  8. Android--WebView控件
  9. Topcoder SRM 648 (div.2)
  10. for 的多重循环--java
  11. Android开发系列之adb常用命令
  12. ThreadLocal类分析
  13. hdu 1011 Starship Troopers(树形DP入门)
  14. VS Code 调试 Angular 和 TypeScript 的配置
  15. BZOJ4627 前缀和 + 权值线段树
  16. Python11/23--mysql用户管理/pymysql
  17. MyBatis从入门到放弃三:一对一关联查询
  18. 前端打包工具之fis3的初级使用
  19. ES6学习笔记(一)-变量的解构赋值
  20. centos安装openssl

热门文章

  1. 你好,C++(12)怎样管理多个类型同样性质同样的数据?3.6 数组
  2. Java 实现 SSH 协议的客户端登录认证方式--转载
  3. 关于Xcode的Other Linker Flags
  4. RT: np - new sbt project generation made simple(r)
  5. 请输出in.txt文件中的2 4 6 8 9 10 12行
  6. require.js的使用的坑!
  7. struts2与spring整合问题,访问struts2链接时,spring会负责创建Action
  8. addEventListener之handleEvent
  9. 使用EF实现数据库的增删改查
  10. 2.2.5 NIO.2 Path 和 Java 已有的 File 类