当前位置: 首页 > 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 .

相关文章:

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

给定一个仅包含数字 2-9 的字符串&#xff0c;返回所有它能表示的字母组合。答案可以按 任意顺序 返回。 给出数字到字母的映射如下&#xff08;与电话按键相同&#xff09;。注意 1 不对应任何字母。 示例 1&#xff1a; 输入&#xff1a;digits "23" 输出&…...

洛谷P1102 A-B 数对题解

目录 题目A-B 数对题目背景题目描述输入格式输出格式样例 #1样例输入 #1样例输出 #1提示传送门 代码解释亲测 题目 A-B 数对 题目背景 出题是一件痛苦的事情&#xff01; 相同的题目看多了也会有审美疲劳&#xff0c;于是我舍弃了大家所熟悉的 AB Problem&#xff0c;改用 …...

【Linux进行时】进程地址空间

进程地址空间 例子引入&#xff1a; 我们在讲C语言的时候&#xff0c;老师给大家画过这样的空间布局图&#xff0c;但是我们对它不了解 我们写一个代码来验证Linux进程地址空间 #include<stdio.h> #include<assert.h> #include<unistd.h> int g_value100; …...

批量将文件名称符合要求的文件自动复制到新文件夹:Python实现

本文介绍基于Python语言&#xff0c;读取一个文件夹&#xff0c;并将其中每一个子文件夹内符合名称要求的文件加以筛选&#xff0c;并将筛选得到的文件复制到另一个目标文件夹中的方法。 本文的需求是&#xff1a;现在有一个大的文件夹&#xff0c;其中含有多个子文件夹&#x…...

TensorFlow入门(一、环境搭建)

一、下载安装Anaconda 下载地址:http://www.anaconda.comhttp://www.anaconda.com 下载完成后运行exe进行安装 二、下载cuda 下载地址:http://developer.nvidia.com/cuda-downloadshttp://developer.nvidia.com/cuda-downloads 下载完成后运行exe进行安装 安装后winR cmd进…...

90、Redis 的 value 所支持的数据类型(String、List、Set、Zset、Hash)---->Hash 相关命令

本次讲解要点&#xff1a; Hash 相关命令&#xff1a;是指value中的数据类型 启动redis服务器&#xff1a; 打开小黑窗&#xff1a; C:\Users\JH>e: E:>cd E:\install\Redis6.0\Redis-x64-6.0.14\bin E:\install\Redis6.0\Redis-x64-6.0.14\bin>redis-server.exe red…...

我开源了一个加密算法仓库,支持18种算法!登录注册业务可用!

文章目录 仓库地址介绍安装用法SHA512HMACBcryptScryptAESRSAECC 仓库地址 仓库地址&#xff1a;https://github.com/palp1tate/go-crypto-guard 欢迎star和fork&#xff01; 介绍 此存储库包含用 Go 编写的全面的密码哈希库。该库支持多种哈希算法&#xff0c;它允许可定制…...

FPGA设计时序约束二、输入延时与输出延时

目录 一、背景 二、set_input_delay 2.1 set_input_delay含义 2.2 set_input_delay参数说明 2.3 使用样例 三、set_output_delay 3.1 set_output_delay含义 3.2 set_output_delay参数说明 3.3 使用样例 四、样例工程 4.1 工程代码 4.2 时序报告 五、参考资料 一、…...

电阻的基础与应用

文章目录 电阻的基础与应用电阻的介绍与分类电阻介绍电阻的分类碳膜/金属膜电阻厚膜/薄膜电阻功能性电阻&#xff08;光敏/热敏/压敏&#xff09;特殊电阻&#xff08;绕线电阻/水泥电阻/铝壳电阻&#xff09; 电阻的主要厂家与介绍国外厂家VISHAY(威世)KOA(兴亚)Kyocera(京瓷)…...

5.html表格

<table><tr><th>列1标题</th><th>列2标题</th><th>列3标题</th></tr><tr><td>行1列1</td><td>行1列2</td><td>行1列3</td></tr><tr><td>行2列1</td>…...

飞桨EasyDL-Mac本地部署离线SDK-Linux集成Python

前言&#xff1a;本文对使用飞桨EasyDL桌面版实现本地部署物体检测做一下说明 一、训练模型 如何使用飞桨EasyDL桌面版这里就不再赘述&#xff0c;直接参照官方文档进行物体检测模型训练。 飞桨EasyDL桌面版-用零代码开发实现物体检测https://ai.baidu.com/ai-doc/EASYDL/Tl2…...

【kubernetes】Kubernetes中的DaemonSet使用

目录 1 为什么需要DaemonSet2 DaemonSet的Yaml的关键字段3 DaemonSet的使用4 一种自行控制Pod更新的方式5 总结 1 为什么需要DaemonSet Deployment可以用于部署无状态的应用&#xff0c;例如系统的接口层或者逻辑层&#xff0c;而多个Pod可以用于负载均衡和容灾。如果有这样一…...

《 新手》web前端(axios)后端(java-springboot)对接简解

文章目录 <font color red>1.何为前后端对接?2.对接中关于http的关键点2.1. 请求方法2.2. 请求参数设置简解&#xff1a; 3.对接中的跨域(CROS)问题**为什么后端处理跨域尽量在业务之前进行&#xff1f;**3.总结 1.何为前后端对接? “前后端对接” 是指前端和后端两个…...

第七章 查找 十、散列查找

一、哈希表&#xff08;散列表&#xff09; 哈希表的数据元素的关键字与其存储地址直接相关。 二、解决冲突的方法 三、散列表中元素的查找 总共对比了3个关键字&#xff0c;所以查找长度为3. 四、查找效率计算 &#xff08;1&#xff09;成功的概率 需要对比一次的关键字为…...

第一章 C语言知识补充

求字节数运算符&#xff1a;sizeof 强制类型转换运算符&#xff1a;&#xff08;类型&#xff09; 下标运算符&#xff1a;[ ] 函数调用运算符&#xff1a;( ) 算术移位指令 算术移位指令有&#xff1a;算术左移SAL和算术右移SAR。算术移位指令的功能描述如下&#xff1a;…...

【Book And Paper 】

【paper Interactive Segmentation of Radiance Fields 算法设计&#xff1a; 电子版...

计算机竞赛 深度学习疲劳检测 驾驶行为检测 - python opencv cnn

文章目录 0 前言1 课题背景2 相关技术2.1 Dlib人脸识别库2.2 疲劳检测算法2.3 YOLOV5算法 3 效果展示3.1 眨眼3.2 打哈欠3.3 使用手机检测3.4 抽烟检测3.5 喝水检测 4 最后 0 前言 &#x1f525; 优质竞赛项目系列&#xff0c;今天要分享的是 &#x1f6a9; **基于深度学习加…...

代码随想录 动态规划 13

300. 最长递增子序列 给你一个整数数组 nums &#xff0c;找到其中最长严格递增子序列的长度。 子序列 是由数组派生而来的序列&#xff0c;删除&#xff08;或不删除&#xff09;数组中的元素而不改变其余元素的顺序。例如&#xff0c;[3,6,2,7] 是数组 [0,3,1,6,2,2,7] 的子…...

lv6 嵌入式开发-Flappy bird项目

目录 1 项目功能总结 2 知识储备&#xff1a; 3 项目框图 4 Ncurses库介绍 做Flappy bird项目有什么用&#xff1f; 1. 复习、巩固c语言知识 2. 培养做项目的逻辑思维能力 3. 具备开发简单小游戏的能力 学会了Flappy bird项目&#xff0c;贪吃蛇和推房子两款小游戏也可…...

【Java】方法重写

概述 子类中出现了和父类一模一样的方法 当子类需要父类的功能&#xff0c;而功能主体中&#xff0c;子类有自己独特的内容&#xff0c;就可以通过重写父类中的方法&#xff0c;这样即延续了父类的功能&#xff0c;又定义了自己的特有内容 Override 是一个注解&#xff0c;可以…...

观成科技:隐蔽隧道工具Ligolo-ng加密流量分析

1.工具介绍 Ligolo-ng是一款由go编写的高效隧道工具&#xff0c;该工具基于TUN接口实现其功能&#xff0c;利用反向TCP/TLS连接建立一条隐蔽的通信信道&#xff0c;支持使用Let’s Encrypt自动生成证书。Ligolo-ng的通信隐蔽性体现在其支持多种连接方式&#xff0c;适应复杂网…...

铭豹扩展坞 USB转网口 突然无法识别解决方法

当 USB 转网口扩展坞在一台笔记本上无法识别,但在其他电脑上正常工作时,问题通常出在笔记本自身或其与扩展坞的兼容性上。以下是系统化的定位思路和排查步骤,帮助你快速找到故障原因: 背景: 一个M-pard(铭豹)扩展坞的网卡突然无法识别了,扩展出来的三个USB接口正常。…...

AtCoder 第409​场初级竞赛 A~E题解

A Conflict 【题目链接】 原题链接&#xff1a;A - Conflict 【考点】 枚举 【题目大意】 找到是否有两人都想要的物品。 【解析】 遍历两端字符串&#xff0c;只有在同时为 o 时输出 Yes 并结束程序&#xff0c;否则输出 No。 【难度】 GESP三级 【代码参考】 #i…...

【算法训练营Day07】字符串part1

文章目录 反转字符串反转字符串II替换数字 反转字符串 题目链接&#xff1a;344. 反转字符串 双指针法&#xff0c;两个指针的元素直接调转即可 class Solution {public void reverseString(char[] s) {int head 0;int end s.length - 1;while(head < end) {char temp …...

实现弹窗随键盘上移居中

实现弹窗随键盘上移的核心思路 在Android中&#xff0c;可以通过监听键盘的显示和隐藏事件&#xff0c;动态调整弹窗的位置。关键点在于获取键盘高度&#xff0c;并计算剩余屏幕空间以重新定位弹窗。 // 在Activity或Fragment中设置键盘监听 val rootView findViewById<V…...

Python ROS2【机器人中间件框架】 简介

销量过万TEEIS德国护膝夏天用薄款 优惠券冠生园 百花蜂蜜428g 挤压瓶纯蜂蜜巨奇严选 鞋子除臭剂360ml 多芬身体磨砂膏280g健70%-75%酒精消毒棉片湿巾1418cm 80片/袋3袋大包清洁食品用消毒 优惠券AIMORNY52朵红玫瑰永生香皂花同城配送非鲜花七夕情人节生日礼物送女友 热卖妙洁棉…...

LangChain知识库管理后端接口:数据库操作详解—— 构建本地知识库系统的基础《二》

这段 Python 代码是一个完整的 知识库数据库操作模块&#xff0c;用于对本地知识库系统中的知识库进行增删改查&#xff08;CRUD&#xff09;操作。它基于 SQLAlchemy ORM 框架 和一个自定义的装饰器 with_session 实现数据库会话管理。 &#x1f4d8; 一、整体功能概述 该模块…...

宇树科技,改名了!

提到国内具身智能和机器人领域的代表企业&#xff0c;那宇树科技&#xff08;Unitree&#xff09;必须名列其榜。 最近&#xff0c;宇树科技的一项新变动消息在业界引发了不少关注和讨论&#xff0c;即&#xff1a; 宇树向其合作伙伴发布了一封公司名称变更函称&#xff0c;因…...

C++ 设计模式 《小明的奶茶加料风波》

&#x1f468;‍&#x1f393; 模式名称&#xff1a;装饰器模式&#xff08;Decorator Pattern&#xff09; &#x1f466; 小明最近上线了校园奶茶配送功能&#xff0c;业务火爆&#xff0c;大家都在加料&#xff1a; 有的同学要加波霸 &#x1f7e4;&#xff0c;有的要加椰果…...

[ACTF2020 新生赛]Include 1(php://filter伪协议)

题目 做法 启动靶机&#xff0c;点进去 点进去 查看URL&#xff0c;有 ?fileflag.php说明存在文件包含&#xff0c;原理是php://filter 协议 当它与包含函数结合时&#xff0c;php://filter流会被当作php文件执行。 用php://filter加编码&#xff0c;能让PHP把文件内容…...