电话号码的字母组合 · Letter Combinations of a Phone Number
[抄题]:
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"]
[暴力解法]:
时间分析:
空间分析:
[思维问题]:
- 以为是树中分治型的DFS, 其实是图中枚举型的DFS,写法都不一样,应该是for循环,没有概念
[一句话思路]:
DFS中嵌套DFS控制总体变量的改变,图中枚举型的for循环控制局部变量的改变。
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 主函数里传入的字符串一开始应该是“”空串,表示dfs的开始。以前写过但是不理解
- digits.charAt(x) - '0'; 可以把输入的字符变成数字来使用,没用过
- 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的更多相关文章
- Leetcode之回溯法专题-17. 电话号码的字母组合(Letter Combinations of a Phone Number)
[Leetcode]17. 电话号码的字母组合(Letter Combinations of a Phone Number) 题目描述: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组 ...
- [Swift]LeetCode17. 电话号码的字母组合 | Letter Combinations of a Phone Number
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...
- [LintCode] Letter Combinations of a Phone Number 电话号码的字母组合
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- LeetCode: Letter Combinations of a Phone Number 解题报告
Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...
- 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 ...
- 69. Letter Combinations of a Phone Number
Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...
- 【leetcode】Letter Combinations of a Phone Number
Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...
- [LeetCode][Python]17: Letter Combinations of a Phone Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...
随机推荐
- 转:HTTP Get请求URL最大长度
转自:http://blog.csdn.net/M_ChangGong/article/details/5764711 各浏览器HTTP Get请求URL最大长度并不相同,几类常用浏览器最大长度及超过 ...
- Jenkins上War文件部署实战
War文件部署 1.jenkins需要安装Deploy Plugin插件:在[系统管理]-[插件管理]下,如果没有安装,则在可选插件下找到该插件,然后安装(如图是1.5版本安装好的截图) 2.在构建的 ...
- 最大流之dinic
先用bfs预处理出层次图,然后在层次图上用dfs找增广路径,理论复杂度O(n*n*m) const int INF=0xfffffff ; struct node { int s,t,cap,nxt ...
- UML图 之 活动图 (汇总版)
============================================================ 摘自: https://blog.csdn.net/tigaoban/arti ...
- Python selenium chrome 环境配置
Python selenium chrome 环境配置 一.参考文章: 1. 记录一下python easy_install和pip安装地址和方法 http://heipark.iteye.com/b ...
- 6款实用的硬盘、SSD固态硬盘、U盘、储存卡磁盘性能测试工具
一.检测工具名称汇总 HDTune ATTO Disk Benchmark CrystalDiskMark AS SSD Benchmark Parkdale CrystalDiskInfo 二.各项 ...
- SQL Server2008 R2开启远程连接总结
============================== SQL Server2008 R2开启远程连接(最全总结) ============================== 安装过程:适用W ...
- Centos 6 下安装 erlang 手记
基于openfire的IM项目已经成功上线,接下来的计划准备開始调研 ejabberd. ejabberd 是基于erlang开发的.那么就先从搭建 erlang环境開始吧. 选择的操作系统为Ce ...
- php in_array的坑
今天做一个根据用户充值领礼包码的活动,遇到一个问题部分用户领礼包时会一直提示“系统错误,请稍后再试”,这是什么情况,一开始以为接口出错了,一番排查后发现了问题所在,是in_array坑了~~~ 情况大 ...
- emacs之配置etags
emacsConfig/etags-setting.el (require 'auto-complete-etags) (setq ac-sources (append '(ac-source-eta ...