[抄题]:

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. linux内存查看工具

    这里帮你总结了一下Linux下查看内存使用情况的多种方法~ 在做 Linux 系统优化的时候,物理内存是其中最重要的一方面.自然的,Linux 也提供了非常多的方法来监控宝贵的内存资源的使用情况.下面 ...

  2. 工作中比较重要的经验分享-2016-bypkm

    工作中总有一些经验能让人记忆深刻,能让人终生受用,相比技术而言,经验是宝贵的.无价的.在我的博客中,主要是技术类的博文,那些东西是相对死板的,价值也相对低廉.今天就记录一下我在工作中一次比较重要的经验 ...

  3. js 判断哪个获得焦点

    if(document.activeElement.id="txtIdHouse") { }   var xx = document.activeElement.id; xx就是现 ...

  4. PyalgoTrade 绘图(七)

    PyAlgoTrade使得绘制策略执行变得非常简单 from pyalgotrade import strategy from pyalgotrade.technical import ma from ...

  5. 6-20 No Less Than X in BST(20 分)

    You are supposed to output, in decreasing order, all the elements no less than X in a binary search ...

  6. Oracle按时间段分组统计

    想要按时间段分组查询,首先要了解level,connect by,oracle时间的加减. 关于level这里不多说,我只写出一个查询语句: ----level 是一个伪例 ---结果: 关于conn ...

  7. CCFlow SDK模式开发(有比较详细的代码,以服务的形式与ccflow数据库进行数据交互)

    http://www.cnblogs.com/s0611163/p/3963142.html 需求: 1.业务数据要保存在我们自己的数据库里     2.CCFlow有保存草稿的功能,但是领导要求每个 ...

  8. SpringCloud初体验:一、Eureka 服务的注册与发现

    Eureka :云端服务发现,一个基于 REST 的服务,用于定位服务,以实现云端中间层服务发现和故障转移. Eureka 可以大致理解为 房产中介 和 房东 的关系,房东想让租客租房子,首先要把房子 ...

  9. iSCSI 协议

    iSCSI 协议 iSCSI协议结构 如同任何一个协议一样,iSCSI也有一个清晰的层次结构,根据OSI模型,iSCSI的协议栈自顶向下一共可以分为五层,如图所示: SCSI层:根据应用发出的请求建立 ...

  10. java之压缩流(ZipOutputStream)

    一.文件压缩,是很有必要的,我们在进行文件,传输过程中,很多时候都是,都是单个文件单个文件发送接收,但是当数据量特别大,或者文件数量比较多的时候,这个时候就可以考虑文件压缩. 二.优势:文件压缩过后, ...