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. MySQL优化(四) 慢查询的定位及优化

    一.SQL语句优化的一般步骤: (1)通过 show status 命令了解各种 SQL 的执行效率: (2)定位执行效率较低的 SQL 语句(重点是 Select): (3)通过 explain 分 ...

  2. chrome升级后出现滚动条无法滚动

    最近升级chrome最新版本后,导致项目中功能页面的局部滚动条无法滚动(心里暗骂了很久),无论怎么滚动都是最外层的滚动条响应... 1.猜想:尼玛google应该不会干事件流混乱这种事,pass: 2 ...

  3. ----这是一个register code----

    这是一个register code,是需要用到<input>标签下的6个标签(?应该是标签喔) 然后附上代码 <html ><head><title>注 ...

  4. 【Git】 GitLab配置优化及汉化

    GitLab配置 1.修改GitLab绑定的域名 a.修改/etc/gitlab/gitlab.rb配置文件,修改成自己的域名 external_url 'http://gitlab.example. ...

  5. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax;问题的解决

    哇,时隔两天时间,终于找到这个问题的解决办法,先看问题 这是我最近写的家庭记账本网页版,按顺序输入点击保存,总是弹出添加失败的提示 顺着找原因,把原因锁定在dao层的sql语句上,反复检查,没有找到一 ...

  6. 关于java poi itext生成pdf文件的例子以及方法

    最近正在做导出pdf文件的功能,所以查了了一些相关资料,发现不是很完善,这里做一些小小的感想,欢迎各位“猿”童鞋批评指正. poi+itext,所需要的jar包有itext-2.1.7.jar,poi ...

  7. cropper,图片剪辑上传工具的使用

    cropper工具是一个功能强,兼容性好的一个图片裁剪和上传工具 GitHub地址:https://github.com/kesixin/Head_Cut_PC <div class=" ...

  8. PowerShell工作流学习-2-工作流运行Powershell命令

    关键点: a)inlineScript 活动具有活动通用参数,但不具有PowerShell 通用参数,且inlineScript 脚本块中的命令和表达式不具有工作流的功能b)默认inlineScrip ...

  9. SDWebImage之SDWebImageManager

    SDWebImageManager是SDWebImage的核心类.它拥有一个SDWebImageCache和一个SDWebImageDownloader属性,分别用于图片的缓存和下载处理.虽然是核心类 ...

  10. 背水一战 Windows 10 (64) - 控件(WebView): 加载指定 HttpMethod 的请求, 自定义请求的 http header, app 与 js 的交互

    [源码下载] 背水一战 Windows 10 (64) - 控件(WebView): 加载指定 HttpMethod 的请求, 自定义请求的 http header, app 与 js 的交互 作者: ...