当前位置: 首页 > ops >正文

Letter Combination of a Phone Number

Introduce

在这里插入图片描述

Problem Analysis (Using “258” as example)

//2   a    b    c
//5   j    k    l
//8   t    u    v

Possible letter combinations:

  • a, j, t (no further options, this is one combination)
  • a, j, u (no further options, another combination)
  • a, j, v (another combination)
  • a, k, t (another combination)

this resembles a depth-first search (DFS) traversal of an n-ary tree.

Solution Approach

To traverse all combinations starting with ‘a’, we need to traverse all subtrees rooted at ‘j’, ‘k’, and ‘l’ (the next level letters for digit ‘5’)

Similarly, to traverse combinations starting with ‘j’, we need to traverse subtrees rooted at ‘t’, ‘u’, and ‘v’ (the next level letter for digit ‘8’)

Recursive implementation

class Solution {
public:vector<string> letterCombinations(string digits) {}
};

We`ll implement a recursive solution:

class Solution {
public:void _letterCombinations(string digits){}vector<string> letterCombinations(string digits) {}
};

Digit-to-Letter Mapping

Since we`re dealing with digit 2-9, we can store the corresponding letters in an string array for O(1) lookup:

class Solution {string tele[10] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; 
public:void _letterCombinations(string digits){}vector<string> letterCombinations(string digits) {}
};

Tree Traversal Implementation

Now we implement the traversal starting from digits[0]:

class Solution {string tele[10] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; 
public:// Traverse all subtrees at level ivoid _letterCombinations(string digits, int i){// Get corresponding lettersstring str = tele[digits[i] - '0'];for (int j = 0; j < str.size(); j++) {// Traverse next level_letterCombinations(digits, i + 1);}}vector<string> letterCombinations(string digits) {// Start traversal from level 0_letterCombinations(digits, 0);}
};

Adding Recursion Boundary Condition

we must add a termination condition for the recursion:

class Solution {string tele[10] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; 
public:// Traverse all subtrees at level ivoid _letterCombinations(string digits, int i){// Recursion boundaryif (i == digits.size()) {return;}// Get corresponding lettersstring str = tele[digits[i] - '0'];for (int j = 0; j < str.size(); j++) {// Traverse next level_letterCombinations(digits, i + 1);}}vector<string> letterCombinations(string digits) {// Start traversal from level 0_letterCombinations(digits, 0);}
};

Storing Results

We need to store the combinations in a vector:

class Solution {string tele[10] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; 
public:// Traverse all subtrees at level ivoid _letterCombinations(string digits, int i, vector<string>& ret){// Recursion boundaryif (i == digits.size()) {return;}// Get corresponding lettersstring str = tele[digits[i] - '0'];for (int j = 0; j < str.size(); j++) {// Traverse next level_letterCombinations(digits, i + 1, ret);}}vector<string> letterCombinations(string digits) {vector<string> result;// Start traversal from level 0_letterCombinations(digits, 0, result);return result; // Don't forget to return!}
};

Building Combinations

We need to build the combinations by passing the current combination down the recursion:

class Solution {string tele[10] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; 
public:// Traverse all subtrees at level ivoid _letterCombinations(string digits, int i, string current, vector<string>& ret){// Recursion boundaryif (i == digits.size()) {ret.push_back(current);return;}// Get corresponding lettersstring str = tele[digits[i] - '0'];for (int j = 0; j < str.size(); j++) {// Traverse next level with updated combination_letterCombinations(digits, i + 1, current + str[j], ret);}}vector<string> letterCombinations(string digits) {vector<string> result;string current;// Start traversal from level 0_letterCombinations(digits, 0, current, result);return result;}
};

Edge Case Handling

Finally, we handle the empty input case:

class Solution {string tele[10] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; 
public:// Traverse all subtrees at level ivoid _letterCombinations(string digits, int i, string current, vector<string>& ret){// Recursion boundaryif (i == digits.size()) {ret.push_back(current);return;}// Get corresponding lettersstring str = tele[digits[i] - '0'];for (int j = 0; j < str.size(); j++) {// Traverse next level with updated combination_letterCombinations(digits, i + 1, current + str[j], ret);}}vector<string> letterCombinations(string digits) {vector<string> result;if (digits.empty()) {return result;}string current;// Start traversal from level 0_letterCombinations(digits, 0, current, result);return result;}
};
http://www.xdnf.cn/news/15764.html

相关文章:

  • Eureka 和 Nacos
  • Ceph存储阈值调整:优化nearfull_ratio参数
  • Vue组件化开发小案例
  • 关于如何同步开发板的时间和现在一样:
  • 地图定位与导航
  • Verilog *2* SPI-立创逻辑派G1测试-1
  • 数据结构:字符串(Strings)
  • IIS部署 .net项目
  • 面试150 课程表Ⅱ
  • Redisson RLocalCachedMap 核心参数详解
  • 从“数字土著”到“数据公民”:K-12数据伦理课程的设计、实施与成效追踪研究
  • Java 大视界 -- 基于 Java 的大数据实时流处理在智能制造生产过程质量实时监控与异常诊断中的应用(352)
  • Gitee 提交信息的规范
  • lvs笔记
  • 教育科技内容平台的用户定位与产品方案:从需求到解决方案的精准匹配
  • Keepalived 监听服务切换与运维指南
  • 基于LSTM的时间序列到时间序列的回归模拟
  • AspectJ 表达式中常见符号说明
  • Baumer工业相机堡盟工业相机如何通过YoloV8深度学习模型实现动物分类(C#源码,UI界面版)
  • 张 关于大语言模型(LLM)置信度研究的经典与前沿论文 :温度缩放;语义熵;自一致性;事实与反思;检索增强;黑盒引导;
  • 微服务学习(六)之分布式事务
  • 商业秘密的法律属性与保护路径探析
  • LeetCode 322. 零钱兑换 LeetCode 279.完全平方数 LeetCode 139.单词拆分 多重背包基础 56. 携带矿石资源
  • 【Docker基础】深入解析Docker-compose核心配置:Services服务配置详解
  • 【学习记录】智能客服小桃(进度更新ing)
  • Elastic Search 8.x 分片和常见性能优化
  • UniApp 自定义导航栏:解决安全区域适配问题的完整实践
  • 当OT遇见IT:Apache IoTDB如何用“时序空间一体化“破解工业物联网数据孤岛困局
  • 【黄山派-SF32LB52】—硬件原理图学习笔记
  • NLP中情感分析与观念分析、价值判断、意图识别的区别与联系,以及四者在实际应用中的协同