当前位置:网站首页>LeetCode_17_电话号码的字母组合
LeetCode_17_电话号码的字母组合
2022-07-17 15:11:00 【Fitz1318】
题目链接
题目描述
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。

示例 1:
输入:digits = "23"
输出:["ad","ae","af","bd","be","bf","cd","ce","cf"]
示例 2:
输入:digits = ""
输出:[]
示例 3:
输入:digits = "2"
输出:["a","b","c"]
提示:
0 <= digits.length <= 4digits[i]是范围['2', '9']的一个数字。
解题思路
一个数字对应这三个字母,那么答案就是从给定数字对应的字母里面挑选组合。那么将组合问题抽象成树形结构,每次从集合中选取元素,可选择的范围随着选择的进行而收缩,调整可选择的范围。图中可以发现n相当于树的宽度,k相当于树的深度。图中每次搜索到了叶子节点,我们就找到了一个结果

回溯法三部曲
- 递归函数的参数和返回值
digits:题目给的numString:数字对应映射的字母num:用来记录遍历到第几个数字path:用来存放符合条件的结果ans:用来存放符合条件的结果的集合
- 回溯函数终止条件
- 到达叶子节点,也就是
path这个数组的大小达到k,说明已经找到了一个子集大小为k的组合了,在图中path存的就是根节点到叶子节点的路径了 - 此时用
ans数组将结果保存起来,并终止本层递归
- 到达叶子节点,也就是
- 单层逻辑
- 用
for循环进行横向遍历,具体来说就是每次从startIndex开始遍历,然后用path保存取到的节点i
- 用
AC代码
class Solution {
public List<String> letterCombinations(String digits) {
List<String> ans = new ArrayList<>();
StringBuilder path = new StringBuilder();
if (digits == null || digits.length() == 0) {
return ans;
}
String[] numString = {
"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
backTracing(digits, numString, 0, path, ans);
return ans;
}
private static void backTracing(String digits, String[] numString, int num, StringBuilder path, List<String> ans) {
//终止条件
if (num == digits.length()) {
ans.add(path.toString());
return;
}
String str = numString[digits.charAt(num) - '0'];
for (int i = 0; i < str.length(); i++) {
path.append(str.charAt(i));
backTracing(digits, numString, num + 1, path, ans);
path.deleteCharAt(path.length() - 1);
}
}
}
边栏推荐
- Wechat applet cloud development 1 - Database
- 02 - 3. Différences entre les pointeurs et les références
- The type of MySQL index (single column index, combined index, BTREE index, clustered index, etc.)
- 【无标题】cv 学习1转换
- Mpu9250 ky9250 attitude, angle module and mpu9250 MPL DMA comparison
- TiFlash 性能调优
- MySQL cannot be started? Relevant components missing? System upgrade? Component mismatch? Start reinstalling MySQL
- Will causal learning open the next generation of AI? Chapter 9 Yunji datacanvas officially released the open source project of ylarn causal learning
- SPI service discovery mechanism
- Bet Net is a good thing
猜你喜欢

TCP congestion control details | 7 Surpass TCP

C # build a system based on WPF entry project of net5

Cv02 Roge matrix, rotation vector, angle

Round table record: fireside dialogue -- how to realize innovation in Web3
![[PostgreSQL] PostgreSQL 15 optimizes distinct](/img/7c/89d05171902dd88bd2b5c352c3614f.png)
[PostgreSQL] PostgreSQL 15 optimizes distinct

【嵌入式单元测试】C语言单元测试框架搭建

JVM hook hooks function

【PostgreSQL 】PostgreSQL 15对distinct的优化

Hot discussion: my husband is 34 years old this year and wants to read a doctoral degree. What should I do in the future to do scientific research?

Redis Distributed cache - Redis Cluster
随机推荐
466-82(3、146、215)
TS solves the problem that the type file of the imported plug-in does not exist
机器人开发--机器人资料汇总
C # build a system based on WPF entry project of net5
Introduction to common distributed locks
565. Array nesting: regular simulation questions
Total number of blocking and waiting in jconsole thread panel (RPM)
夢想CMS 前臺搜索SQL注入
03-1. Inline function, auto keyword, typeID, nullptr
Bet Net is a good thing
zabbix代理服务器配置
Unchangeable status quo
百度文档翻译api
JVM钩子hooks函数
2022 National latest fire-fighting facility operator (intermediate fire-fighting facility operator) simulation test questions and answers
MySQL cannot be started? Relevant components missing? System upgrade? Component mismatch? Start reinstalling MySQL
动态内存分配问题
SQL union operator
Similarities and differences between OA system and MES system
翻墙后看什么?最热门的国外网站——翻墙网址导航