电话号码的字母组合 · 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 ...
随机推荐
- 收集前端UI框架 持续更新中....
1. elementUI 饿了么出品 基于Vue2 http://element.eleme.io/#/zh-CN 2. ZUI 开源HTML5跨屏框架 (2018年1月4日更新)一个基于 Boot ...
- VC++ 6.0 C8051F340 MFC programming note
/************************************************************************************** * VC++ 6.0 C ...
- Codeforces 1006C:Three Parts of the Array(前缀和+map)
题目链接:http://codeforces.com/problemset/problem/1006/C (CSDN又改版了,复制粘贴来过来的题目没有排版了,好难看,以后就截图+题目链接了) 题目截图 ...
- 进程的proc文件系统信息
一.实验代码 #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include &l ...
- java设计模式--七大原则
2016-05-14 20:45:38 设计模式到底是五大.六大还是七大,一直傻傻分不清楚,网上总是有说那些原则可以归为一个,我纠结了半天,突然发现自己是舍本逐末了,只要清楚这些原则的设计思想,其他的 ...
- time函数及其用法
Linux下三种时间 st_atime:文件中的数据最后被访问的时间 Time when file data was last accessed. Changed by the ...
- baby用品
新生嬰兒用品清單 1.哺育用品: 大奶瓶:6支,240ml左右.選擇PC材質耐高溫120度,可消毒:玻璃材質建議選用印刷安全無鉛材料,可消毒. 小奶瓶:2-3支,120ml左右.寬口徑/一般口徑(喝水 ...
- redis 操作大全 PHP-redis中文文档
转自 : http://www.cnblogs.com/weafer/archive/2011/09/21/2184059.html phpredis是php的一个扩展,效率是相当高有链表排序功能, ...
- 峰Spring4学习(7)spring对JDBC的支持
第一节: 工程结构: 1)student.java: package com.cy.model; public class Student { private int id; private Stri ...
- appium启动APP时避免重新安装的问题
from appium import webdriverfrom time import sleepimport os #获取apk的绝对路径desired_cups = {}#设备平台desired ...