[抄题]:

Given a digit string excluded 01, 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.

给定 "23"

返回 ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]

[暴力解法]:

时间分析:

空间分析:

[思维问题]:

  1. 以为是树中分治型的DFS, 其实是图中枚举型的DFS,写法都不一样,应该是for循环,没有概念

[一句话思路]:

DFS中嵌套DFS控制总体变量的改变,图中枚举型的for循环控制局部变量的改变。

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 主函数里传入的字符串一开始应该是“”空串,表示dfs的开始。以前写过但是不理解
  2. digits.charAt(x) - '0'; 可以把输入的字符变成数字来使用,没用过
  3. dfs中不需要画蛇添足地解释l是什么,形式参数是自带的,之前不理解。//int l = digits.length();

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

枚举型还是DFS嵌套DFS,不过里面有循环

[复杂度]:Time complexity: O(3^n) Space complexity: O(n)

新建一个链表 空间复杂度还是n, 就地使用原来的链表 空间复杂度是1

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

22. Generate Parentheses 涉及到穷举,用dfs回溯法,忘了

[代码风格] :

public class Solution {
/**
* @param digits: A digital string
* @return: all posible letter combinations
*/
List<String> ans = new LinkedList<>(); public List<String> letterCombinations(String digits) {
//corner case
if (digits.length() == 0) {
return ans;
}
String[] phone = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
dfs(0, digits.length(), "", digits, phone);
return ans;
} //bfs
//exit and execute
private void dfs (int x, int l, String str, String digits, String[] phone) {
//int l = digits.length();
if (x == l) {
ans.add(str);
return;
} int d = digits.charAt(x) - '0';
for (char c : phone[d].toCharArray()) {
dfs(x + 1, l, str + c, digits, phone);
}
}
}

电话号码的字母组合 · Letter Combinations of a Phone Number的更多相关文章

  1. Leetcode之回溯法专题-17. 电话号码的字母组合(Letter Combinations of a Phone Number)

    [Leetcode]17. 电话号码的字母组合(Letter Combinations of a Phone Number) 题目描述: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组 ...

  2. [Swift]LeetCode17. 电话号码的字母组合 | Letter Combinations of a Phone Number

    Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...

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

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

  4. 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

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

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

  6. LeetCode解题报告—— Container With Most Water & 3Sum Closest & Letter Combinations of a Phone Number

    1.  Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a ...

  7. 69. Letter Combinations of a Phone Number

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

  8. 【leetcode】Letter Combinations of a Phone Number

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

  9. [LeetCode][Python]17: Letter Combinations of a Phone Number

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...

随机推荐

  1. 转:HTTP Get请求URL最大长度

    转自:http://blog.csdn.net/M_ChangGong/article/details/5764711 各浏览器HTTP Get请求URL最大长度并不相同,几类常用浏览器最大长度及超过 ...

  2. Jenkins上War文件部署实战

    War文件部署 1.jenkins需要安装Deploy Plugin插件:在[系统管理]-[插件管理]下,如果没有安装,则在可选插件下找到该插件,然后安装(如图是1.5版本安装好的截图) 2.在构建的 ...

  3. 最大流之dinic

    先用bfs预处理出层次图,然后在层次图上用dfs找增广路径,理论复杂度O(n*n*m) const int INF=0xfffffff ; struct node { int s,t,cap,nxt ...

  4. UML图 之 活动图 (汇总版)

    ============================================================ 摘自: https://blog.csdn.net/tigaoban/arti ...

  5. Python selenium chrome 环境配置

    Python selenium chrome 环境配置 一.参考文章: 1. 记录一下python easy_install和pip安装地址和方法 http://heipark.iteye.com/b ...

  6. 6款实用的硬盘、SSD固态硬盘、U盘、储存卡磁盘性能测试工具

    一.检测工具名称汇总 HDTune ATTO Disk Benchmark CrystalDiskMark AS SSD Benchmark Parkdale CrystalDiskInfo 二.各项 ...

  7. SQL Server2008 R2开启远程连接总结

    ============================== SQL Server2008 R2开启远程连接(最全总结) ============================== 安装过程:适用W ...

  8. Centos 6 下安装 erlang 手记

    基于openfire的IM项目已经成功上线,接下来的计划准备開始调研 ejabberd.  ejabberd  是基于erlang开发的.那么就先从搭建 erlang环境開始吧. 选择的操作系统为Ce ...

  9. php in_array的坑

    今天做一个根据用户充值领礼包码的活动,遇到一个问题部分用户领礼包时会一直提示“系统错误,请稍后再试”,这是什么情况,一开始以为接口出错了,一番排查后发现了问题所在,是in_array坑了~~~ 情况大 ...

  10. emacs之配置etags

    emacsConfig/etags-setting.el (require 'auto-complete-etags) (setq ac-sources (append '(ac-source-eta ...