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. windows下Anaconda的安装与配置正解

    一.下载anaconda 第一步当然是下载anaconda了,官方网站的下载需要用迅雷才能快点,或者直接到清华大学镜像站下载. 清华大学提供了镜像,从这个镜像下载速度很快,地址: https://mi ...

  2. 转载-对js中new、prototype的理解

    说明:本篇文章是搜集了数家之言,综合的结果,应向数家致谢 说到prototype,就不得不先说下new的过程. 我们先看看这样一段代码: <script type="text/java ...

  3. Maven学习 一 概念介绍

    一 Maven是什么 Maven是一个Apache公司的开源项目,主要有两个作用:(1)是项目构建工具.(2)是用来管理java程序中jar包的依赖. 它包含了一个项目对象模型(Project Obj ...

  4. Java 装箱和拆箱

    1.装箱机制 基础类型引用到其包装类型,这样就可以调用其各种方法. 例如,我们声明: Integer a = 1; 其在编译过程中会自动解释成: Integer a = Integer.valueOf ...

  5. qhfl-4 注册-登录-认证

    认证 任何的项目都需要认证,当用户输入了用户名和密码,验证通过,代表用户登录成功 那HTTP请求是无状态的,下次这个用户再请求,是不可能识别这个用户是否登录的 就要有自己的方式来实现这个认证,用户登录 ...

  6. input标签之外是否一定添加form标签

    原文转载自:https://blog.csdn.net/lamanchas/article/details/78753031 input标签外是否添加form标签需要按情形区分:应用场景的区别:1.所 ...

  7. Unity3D中声音播放

    Unity3D 播放声音需要使用 Audio Source 组件,并且需要 Audio Listener 组件配合,不然无法听到声音.Main Camera 会默认有 Audio Lisetener. ...

  8. 安装php7.2并且整合nginx且分开部署

    1.安装php 7.2 2.php配置 3.nginx配置 4.测试 5.报错与解决 6.利用upstream实现负载均衡 1.安装php 7.2 启动容器: liwangdeMacBook-Air: ...

  9. 《大型网站系统与Java中间件实践》

    读了一下,个人认为最好的部分,就是第四章了. CH04 服务框架 4.2 服务设计与实现 // 获取可用服务地址列表 // 确定调用服务目标机器 // 建立连接(Socket) // 请求序列化 // ...

  10. 利用Go2Shell 实现 Mac Finder 直接shell端打开当前文件夹

    Finder 窗口 ,点击下图所示的按钮(红色框内),即可打开Shell Terminal. 打开后,如图 用法 安装go2shell后,打开finder的application文件夹,找到go2sh ...