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. Unity3D 移动摇杆处理

    using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Eve ...

  2. 通过类名或者jar名查询所在jar包

    一.问题 例如我想查看一下FilterSecurityInterceptor的源码,但是我不知道它在maven依赖中的哪个jar包中 二.解决方案 http://www.findmaven.net/ ...

  3. Annotation 标注

    1.画出基本图 当图线中某些特殊地方需要标注时,我们可以使用 annotation. matplotlib 中的 annotation 有两种方法, 一种是用 plt 里面的 annotate,一种是 ...

  4. The best way to use Xtool X100 PAD2 for FEM programming

    Look here: XTOOL X100 PAD2 is new FEM programming. Possible to use Xtool X100 PAD2 for FEM programmi ...

  5. Oracle DBLINk的使用

    Oracle中自带了DBLink功能,它的作用是将多个oracle数据库逻辑上看成一个数据库,也就是说在一个数据库中可以操作另一个数据库中的对象,例如我们新建了一个数据database1,我们需要操作 ...

  6. Latex命令

    .tex代码中 |   在pdf文档中 空一行  代表回车,下一行空两格 // 代表回车,下一行顶格

  7. python之路(六)-函数相关

    在没有学习函数之前我们的程序是面向过程的,不停的判断,不停的循环,同样的代码重复出现在我们的代码里.函数可以更好的提高我们的 代码质量,避免同样的代码重复出现,而只需要在用的时候调用函数即可执行.此为 ...

  8. docker安装redis 指定配置文件且设置了密码

    ---------首先,所有docker的命令,都可以用 docker help 来查询,这个挺好的,我反正记不住辣么多命令呀.   1.直接pull 官方镜像吧.没啥说的,这样方便省事.如果你非要用 ...

  9. SOPC与 hello world

    本次设计实验源码位于:http://download.csdn.net/detail/noticeable/9922865 实验目的:通过系统的搭建进一步了解FPGA的SOPC开发流程,并借此了姐pl ...

  10. 轮播图js编写

    //面向对象 function Left() { this.index = 0; this.lefthover = $('#left-content'); this.listenhover(); th ...