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

【Leetcode】 17. 电话号码的字母组合

给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。

给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母
que
示例 1:

输入digits = "23"
输出["ad","ae","af","bd","be","bf","cd","ce","cf"]

示例 2:

输入digits = ""
输出[]

示例 3:

输入digits = "2"
输出["a","b","c"]

提示:

0 <= digits.length <= 4

digits[i] 是范围 ['2', '9'] 的一个数字。


AC:

/** @lc app=leetcode.cn id=17 lang=cpp** [17] 电话号码的字母组合*/// @lc code=start
class Solution {
private:const string letterMap[10] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz",};
public:string s;vector<string> result;void backtracking(const string& digits, int index) {if(digits.size() == index) {result.push_back(s);return ;}int digit = digits[index] - '0';string letter = letterMap[digit];for(int i = 0; i < letter.size(); i++){s.push_back(letter[i]);backtracking(digits, index + 1);s.pop_back();}}vector<string> letterCombinations(string digits) {s.clear();result.clear();if(digits.size() == 0)return result;backtracking(digits, 0);return result;}
};
// @lc code=end

AC
The function uses a private member variable letterMap to map each digit to its corresponding set of letters. The letterMap is an array of strings, where each string represents the set of letters corresponding to a digit. For example, letterMap[2] is the string “abc”, which represents the letters ‘a’, ‘b’, and ‘c’ corresponding to the digit 2.

The function uses a recursive backtracking approach to generate all possible letter combinations. The backtracking function takes two arguments: the input string of digits and an index representing the current position in the input string. The function first checks if the current position is equal to the length of the input string. If so, it adds the current combination of letters to the result vector and returns.

If the current position is less than the length of the input string, the function retrieves the set of letters corresponding to the current digit using the letterMap array. It then iterates over each letter in the set and adds it to the current combination of letters. The function then recursively calls itself with the updated combination of letters and the next index. After the recursive call returns, the function removes the last letter from the combination of letters to backtrack and try the next letter in the set.

The letterCombinations function initializes the result vector and calls the backtracking function with the input string and an initial index of 0. If the input string is empty, the function returns an empty vector.

Overall, this implementation is a simple and efficient way to generate all possible letter combinations of a given string of digits using a backtracking algorithm. One possible way to improve the code would be to add error checking to ensure that the input string only contains digits. Additionally, the variable names could be more descriptive to make the code easier to read and understand.


How does the backtracking algorithm work in generating all possible letter combinations?

The backtracking algorithm used in this code works by recursively generating all possible letter combinations of a given string of digits. The algorithm uses a private member variable letterMap to map each digit to its corresponding set of letters.

The backtracking function takes two arguments: the input string of digits and an index representing the current position in the input string. The function first checks if the current position is equal to the length of the input string. If so, it adds the current combination of letters to the result vector and returns.

If the current position is less than the length of the input string, the function retrieves the set of letters corresponding to the current digit using the letterMap array. It then iterates over each letter in the set and adds it to the current combination of letters. The function then recursively calls itself with the updated combination of letters and the next index. After the recursive call returns, the function removes the last letter from the combination of letters to backtrack and try the next letter in the set.

The letterCombinations function initializes the result vector and calls the backtracking function with the input string and an initial index of 0 . If the input string is empty, the function returns an empty vector.

Overall, the backtracking algorithm works by generating all possible letter combinations of a given string of digits by recursively iterating over each digit and its corresponding set of letters. The algorithm uses a simple and efficient approach to generate all possible combinations of letters, and is a common technique used in many other combinatorial problems .

http://www.lryc.cn/news/180496.html

相关文章:

  • 洛谷P1102 A-B 数对题解
  • 【Linux进行时】进程地址空间
  • 批量将文件名称符合要求的文件自动复制到新文件夹:Python实现
  • TensorFlow入门(一、环境搭建)
  • 90、Redis 的 value 所支持的数据类型(String、List、Set、Zset、Hash)---->Hash 相关命令
  • 我开源了一个加密算法仓库,支持18种算法!登录注册业务可用!
  • FPGA设计时序约束二、输入延时与输出延时
  • 电阻的基础与应用
  • 5.html表格
  • 飞桨EasyDL-Mac本地部署离线SDK-Linux集成Python
  • 【kubernetes】Kubernetes中的DaemonSet使用
  • 《 新手》web前端(axios)后端(java-springboot)对接简解
  • 第七章 查找 十、散列查找
  • 第一章 C语言知识补充
  • 【Book And Paper 】
  • 计算机竞赛 深度学习疲劳检测 驾驶行为检测 - python opencv cnn
  • 代码随想录 动态规划 13
  • lv6 嵌入式开发-Flappy bird项目
  • 【Java】方法重写
  • 艺术表现形式
  • PHP 反序列化漏洞:手写序列化文本
  • react.js在visual code 下的hello World
  • CocosCreator3.8研究笔记(二十四)CocosCreator 动画系统-动画编辑器实操-关键帧实现动态水印动画效果
  • 第1篇 目标检测概述 —(3)YOLO系列算法
  • SpringBoot整合数据库连接
  • uni-app:canvas-绘制图形4(获取画布宽高,根据画布宽高进行图形绘制)
  • EM@坐标@函数@图象的对称和翻折变换
  • Python之json模块
  • 机器学习---BP算法
  • 继苹果、联发科后,传高通下一代5G芯片将由台积电以3纳米代工