电话号码的字母组合 · 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 ...
随机推荐
- show()是非模式窗体. showDialog()是模式窗体.
show()仅仅是显示出来窗口界面而已```也就是和你执行的结果在同一窗口显示```所显示的窗口可以在后台运行```而showDialog()是一个对话框窗口界面```执行结果以新窗口界面出现```不 ...
- 如何在Oracle官网下载java的JDK最新版本和历史版本
官网上最显眼位置只显示了Java SE的JDK的最新版本下载链接,因为都是英文,如果英文不是很好,寻找之前的JDK版本需要很长时间,而且未必能在那个隐蔽的位置找到之前版本列表. 今天小编来给你详细讲解 ...
- html页面下拉列表中动态添加后台数据(格式化数据,显示出数据的层次感)
html页面下拉列表中动态添加后台数据(格式化数据,显示出数据的层次感) 效果图: 运行原理和技术: 当页面加载完毕,利用jquery向后台发送ajax请求,去后台拼接<select>&l ...
- pandas dataframe 读取 xlsx 文件
refer to: https://medium.com/@kasiarachuta/reading-and-writingexcel-files-in-python-pandas-8f0da449c ...
- CSS为英文和中文字体分别设置不同的字体
font-family的调用方法: div { font-family:Arial,'Times New Roman','Microsoft YaHei',SimHei; font:bold 12px ...
- 不以main为入口的函数
先看一段程序 #include <stdio.h> void test() { printf("Hello Word!\n"); return 0; } 没有main函 ...
- Shell编程时常用的系统文件(转)
10.1 Linux系统目录结构 / 根目录,所有文件的第一级目录 /home 普通用户家目录 /root 超级用户家目录 /usr 用户命令.应用程序等目录 /var 应用数据.日志等目录 /lib ...
- emacs之配置speedbar
安装sr-speedbar,这样的话,speedbar就内嵌到emacs里面了 emacsConfig/speedbar-setting.el (require 'sr-speedbar) (setq ...
- 关于_WIN32_WINNT的含义
在使用一些新版本的API,或者控件的新特性(比如新版的ComCtl32.dll)的时候,你可能会得到“error C2065: undeclared identifier.“这个错误.原因是这些功能是 ...
- java get post 请求
package wzh.Http; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStr ...