当前位置:网站首页>每日一题-电话号码的字母组合-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);
}
}
}
边栏推荐
猜你喜欢

二、自动配置之底层注解

数控直流电源

多边形等分

Redis设计与实现(第二部分):单机数据库的实现

PID详解

【UiPath2022+C#】UiPath 循环

教你如何封装功能组件和页面组件
![[Pytorch study notes] 9. How to evaluate the classification results of the classifier - using confusion matrix, F1-score, ROC curve, PR curve, etc. (taking Softmax binary classification as an example)](/img/ac/884d8aba8b9d363e3b9ae6de33d5a4.png)
[Pytorch study notes] 9. How to evaluate the classification results of the classifier - using confusion matrix, F1-score, ROC curve, PR curve, etc. (taking Softmax binary classification as an example)

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

电子产品量产工具(3)- 文字系统实现
随机推荐
表情捕捉的指标/图像的无参考质量评价
Jupyter notebook选择不同的Anaconda环境作为内核运行
【ts】typescript高阶:映射类型与keyof
原型版本管理
深度学习系列(二)优化器 (Optimization)
网络ID,广播地址,掩码位数计算
Thread handler handle IntentServvice handlerThread
最简单的防抖节流理解法
深度学习系列(一)简介、线性回归与成本函数
You should write like this
TinyFlashDB:一种超轻量的可纠错的通用单片机flash存储方案
Redis集群(docker版)——从原理到实战超详细
LeetCode刷题之第86题
读论文- pix2pix
Redis设计与实现(第二部分):单机数据库的实现
Facial Motion Capture 调研
【UiPath2022+C#】UiPath If条件语句
ECCV2022 | RU & Google propose zero-shot object detection with CLIP!
【ts】typescript高阶:模版字面量类型
八、请求处理之自定义类型参数绑定原理