import java.util.*;

/**
* Source : https://oj.leetcode.com/problems/letter-combinations-of-a-phone-number/
*
* Created by lverpeng on 2017/7/10.
*
* 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.
* */
public class LetterCombinationOfaPhoneNumber { private Map<Character, String[]> map = new HashMap<Character, String[]>(){{
put('0', new String[]{"", "", "", ""});
put('1', new String[]{"", "", "", ""});
put('2', new String[]{"a", "b", "c", ""});
put('3', new String[]{"d", "e", "f", ""});
put('4', new String[]{"g", "h", "i", ""});
put('5', new String[]{"j", "k", "l", ""});
put('6', new String[]{"m", "n", "o", ""});
put('7', new String[]{"p", "q", "r", "s"});
put('8', new String[]{"t", "u", "v", ""});
put('9', new String[]{"w", "x", "y", "z"});
}}; /**
* 广度优先
* 先计算出
*
* @param numStr
* @return
*/
public String[] letterCombination (String numStr) {
List<String> result = new ArrayList<String>(); for (int i = 0; i < numStr.length(); i++) {
List<String> currentStrArr = new ArrayList<String>();
if (result.size() == 0) {
for (int j = 0; j < 4 && !"".equals(map.get(numStr.charAt(i))[j]); j++) {
currentStrArr.add(map.get(numStr.charAt(i))[j] + "");
}
} else {
for (String str : result) {
for (int j = 0; j < 4 && !"".equals(map.get(numStr.charAt(i))[j]); j++) {
currentStrArr.add(str + map.get(numStr.charAt(i))[j]);
}
}
}
result = currentStrArr;
} return result.toArray(new String[result.size()]);
} /**
* 深度优先
*
* @param numStr
* @param index
* @param result
* @param str
* @return
*/
public String letterConbinationByDFS (String numStr, int index, List<String> result, String str) {
if (index >= numStr.length()) {
return str;
}
char ch = numStr.charAt(index); for (int j = 0; j < 4; j++) {
if (map.get(ch)[j].equals("")) {
return "";
}
str += map.get(ch)[j] + ""; str = letterConbinationByDFS(numStr, index + 1, result, str);
if (!str.equals("")) {
result.add(str);
str = str.substring(0, str.length() - 1);
}
}
return str;
} public static void main(String[] args) {
LetterCombinationOfaPhoneNumber letterCombinationOfaPhoneNumber = new LetterCombinationOfaPhoneNumber();
System.out.println(Arrays.toString(letterCombinationOfaPhoneNumber.letterCombination("23")));
List<String> list = new ArrayList<String>();
letterCombinationOfaPhoneNumber.letterConbinationByDFS("23", 0 , list, "");
System.out.println(Arrays.toString(list.toArray(new String[list.size()])));
}
}

leetcode — letter-combinations-of-a-phone-number的更多相关文章

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

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

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

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

  3. [LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合

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

  4. LeetCode——Letter Combinations of a Phone Number

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

  5. [LeetCode] Letter Combinations of a Phone Number

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

  6. [LeetCode] Letter Combinations of a Phone Number(bfs)

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

  7. LeetCode Letter Combinations of a Phone Number (DFS)

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

  8. [LeetCode] Letter Combinations of a Phone Number 回溯

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

  9. LeetCode Letter Combinations of a Phone Number 电话号码组合

    题意:给一个电话号码,要求返回所有在手机上按键的组合,组合必须由键盘上号码的下方的字母组成. 思路:尼玛,一直RE,题意都不说0和1怎么办.DP解决. class Solution { public: ...

  10. leetcode Letter Combinations of a Phone Number python

    class Solution(object): def letterCombinations(self, digits): """ :type digits: str : ...

随机推荐

  1. Python开发——【循环】语句

    while循环 while 条件: # 要执行的循环体 # 如果条件为真,那么循环体则执行 # 如果条件为假,那么循环体不执行 死循环 count = 0 while True:# 条件永远为真 pr ...

  2. Python开发——数据类型【运算符】

    算数运算符 比较运算符 赋值运算符 逻辑运算符 成员运算符

  3. 基于ASP.NET的高校辅导员工作管理系统的设计与实现--论文随笔(四)

    一.基本信息 标题:基于ASP.NET的高校辅导员工作管理系统的设计与实现 时间:2017 出版源:南通理工学院 关键词:ASP.NET; SQL Server; 高校; 管理系统; 辅导员; 二.研 ...

  4. unbuntu 安装 teamviewer

    下载 teamviewer 安装包 使用 dpkg 安装 deb 安装包 使用 sudo apt-get install -f 解决依赖问题

  5. Python:每日一题004

    题目: 输入某年某月某日,判断这一天是这一年的第几天? 程序分析: 以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于2时需考虑多加一天 个人的思路及 ...

  6. LOJ-10100(割点个数)

    题目链接:传送门 思路: 就是求割点的个数,直接Tarjan算法就行. 注意输入格式(判断比较水). #include<iostream> #include<cstdio> # ...

  7. Spring配置Bean,为属性赋值

    SayHello的实体类: package com.langchao; /** * @ClassName: SayHello * @description: * @author: ZhangYawei ...

  8. 虚拟机 django 端口无法连接

    我的虚拟机django服务器为192.168.27.100,使用启动命令python manage.py runserver 9001启动后,发现笔记本电脑的游览器无法连接 python@qinhan ...

  9. git diff 理解

    0. 理解 git diff 返回信息 1. 命令 $ git diff README.md 2. 返回信息,注解 diff --git a/README.md b/README.md ## 1. 表 ...

  10. 关于Runtime.getRuntime().exec()产生阻塞的2个陷阱

    本文来自网易云社区 背景 相信做java服务端开发的童鞋,经常会遇到Java应用调用外部命令启动一些新进程来执行一些操作的场景,这时候就会使用到Runtime.getRuntime().exec(), ...