当前位置:网站首页>每日一题-电话号码的字母组合-0717
每日一题-电话号码的字母组合-0717
2022-08-05 05:17:00 【菜鸡程序媛】
题目
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。
思路
- 采用DFS算法,不撞南墙不回头,回头的时候记得清除当下的状态(回溯)
- 从开始的数字第一个字符开始,找第二个数字的第一个字符……,最后一个数字的第一个字符,长度=digits.length(),代表一个组合形成
- 最后一个数字的第二个字符,又代表一个组合形成……最后一个数字的最后一个字符,又代表一个组合形成
- 递归开始往回找,找到倒数第二个数字的第二个字符,倒数第一个数字的第一个字符……因为字母都是唯一的,所以不考虑去重问题,直到找出所有组合
代码
class Solution {
public String[] letters = {
"", "*", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
public List<String> letterCombinations(String digits) {
List<String> res = new LinkedList<>();
if(digits == null || digits.length() == 0)
return res;
recur(digits, 0, res, new StringBuilder());
return res;
}
private void recur(String digits, int index, List<String> res, StringBuilder sb){
if(sb.length() == digits.length()){
res.add(sb.toString());
return;
}
// 当前的字符减去0,获取在字符串数组中对应的下标
int i = digits.charAt(index) - '0';
String str = letters[i];
for(int j = 0; j < str.length(); j ++){
sb.append(str.charAt(j));
recur(digits, index + 1, res, sb);
sb.deleteCharAt(sb.length() - 1); //将刚拼接好的字符删除
}
}
}
进一步思考
如果在“23”组合下,需要达到「ad, ae, af, be, bf, cf」这样的效果,可以改变一下递归的方式,如下
package 回溯;
import java.util.LinkedList;
import java.util.List;
public class test {
public static void main(String[] args){
String digits = "23";
test sol = new test();
System.out.println(sol.letterCombinations(digits));
}
/** * * @param digits * @return */
public String[] letters = {
"", "*", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
public List<String> letterCombinations(String digits) {
List<String> res = new LinkedList<>();
if(digits == null || digits.length() == 0)
return res;
recur(digits,0, 0, res, new StringBuilder());
return res;
}
private void recur(String digits, int cur, int index, List<String> res, StringBuilder sb){
if(sb.length() == digits.length()){
res.add(sb.toString());
return;
}
int j = digits.charAt(cur) - '0';
String str = letters[j];
for(int i = index; i < str.length(); i ++){
sb.append(str.charAt(i));
recur(digits, cur + 1, i, res, sb);
sb.deleteCharAt(sb.length() - 1);
}
}
}
边栏推荐
猜你喜欢
![[Database and SQL study notes] 10. (T-SQL language) functions, stored procedures, triggers](/img/b9/06b90160c962a25a3cc44731afb6dc.png)
[Database and SQL study notes] 10. (T-SQL language) functions, stored procedures, triggers
![[Pytorch study notes] 10. How to quickly create your own Dataset dataset object (inherit the Dataset class and override the corresponding method)](/img/71/f82e76085f9d8e6610f6f817e2148f.png)
[Pytorch study notes] 10. How to quickly create your own Dataset dataset object (inherit the Dataset class and override the corresponding method)

WCH系列芯片CoreMark跑分

电子产品量产工具(1)- 显示系统实现

Thread handler handle IntentServvice handlerThread

CVPR2020 - 自校准卷积
![[Database and SQL study notes] 9. (T-SQL language) Define variables, advanced queries, process control (conditions, loops, etc.)](/img/7e/566bfa17c5b138d1f909185721c735.png)
[Database and SQL study notes] 9. (T-SQL language) Define variables, advanced queries, process control (conditions, loops, etc.)

发顶会顶刊论文,你应该这样写作

C语言的一些小常识

面向小白的深度学习代码库,一行代码实现30+中attention机制。
随机推荐
【22李宏毅机器学习】课程大纲概述
伪RTOS-ProroThread在CH573芯片上的移植
MaskDistill - Semantic segmentation without labeled data
ACL 的一点心得
物联网-广域网技术之NB-IoT
十、视图解析原理与源码分析
GIS面试问题
网工必用神器:网络排查工具MTR
亲身实感十多年的面试官面试的题目
基于STM32F4的FFT+测频率幅值相位差,波形显示,示波器,时域频域分析相关工程
[Kaggle project actual combat record] Steps and ideas sharing of a picture classification project - taking leaf classification as an example (using Pytorch)
dataframe 常用操作
A deep learning code base for Xiaobai, one line of code implements 30+ attention mechanisms.
【UiPath2022+C#】UiPath If条件语句
如何组织一场安全、可靠、高效的网络实战攻防演习?
LeetCode刷题之第1024题
The University of Göttingen proposed CLIPSeg, a model that can perform three segmentation tasks at the same time
深度学习系列(二)优化器 (Optimization)
C语言—三子棋的实现
网管日记:故障网络交换机快速替换方法