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. ubuntu,day3,awk, vim的使用

     本节内容 : 1,awk 2,vim 1,awk # 命令行调用方式 awk [-F field-separator] 'commands' input-file(s) cat /etc/passw ...

  2. java多线程系列7 高级同步工具(1)信号量Semaphore

    Semaphore叫做信号量 可以控制某个资源可被同时访问的个数, acquire() 获取一个许可,得到许可才能执行后面的代码,如果没有就等待. release() 释放一个许可. 当信号量的只允许 ...

  3. C#部分试题实例

    1.在C#中,下列选项中自定义方法的语句错误的是().(选择一项) 正确答案:AD 解析:本题考查自定义方法的定义及调用.A项void是无返回值类型,D项定义方法的时候没有写返回值类型:故选AD. 2 ...

  4. Django高级篇三。restful的解析器,认证组件,权限组件

    一.rest=framework之解析器 1)解析器作用. 根据提交的数据.只解析某些特定的数据.非法数据不接收,为了系统安全问题 比如解析的数据格式有 有application/json,x-www ...

  5. scp复制文件到指定端口

    1.scp基本格式 scp file user@host:/dir 2.scp复制文件到指定端口 scp默认连接的端口是22端口,如果ssh不是使用标准的22端口则使用-P(P大写)指定: scp - ...

  6. Codeforces 1077C Good Array 坑 C

    Codeforces 1077C Good Array https://vjudge.net/problem/CodeForces-1077C 题目: Let's call an array good ...

  7. fortran77读写文本文档

    PROGRAM WRITETEXT IMPLICIT NONE INTEGER,PARAMETER :: NE=!fortran90 语法定义变量 DOUBLE PRECISION A(,),B(,) ...

  8. python闭包和延迟绑定

    一.什么是闭包: 1.函数内定义函数. 2.外函数的返回时内函数的引用. 3.内函数使用外函数的局部变量(至少一个). 1 def outfunc(): 2 for num in range(4): ...

  9. EF6学习笔记(四) 弹性连接及命令拦截调试

    EF6学习笔记总目录:ASP.NET MVC5 及 EF6 学习笔记 - (目录整理) 本章原文地址:Connection Resiliency and Command Interception 原文 ...

  10. 仿boost::any的泛型指针类any的实现

    在boost库中,any是一种特殊容器,只能容纳一个元素,但这个元素可以是任意的类型----int.double.string.标准容器或者任何自定义类型.程序可以用any保存任意的数据,也可以在任何 ...