当前位置:网站首页>leetcode 17. Letter Combinations of Phone Numbers
leetcode 17. Letter Combinations of Phone Numbers
2022-08-06 03:01:00 【_Liu Xiaoyu】
作者简介:C/C++ 、Golang 领域耕耘者,创作者
个人主页:作者主页
活动地址:CSDN21天学习挑战赛
题目来源: leetcode官网
如果感觉博主的文章还不错的话,还请关注 、点赞 、收藏🧡三连支持一下博主哦~~~
题目描述
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合.答案可以按 任意顺序 返回.
给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母.
示例1:
输入:digits = “23”
输出:[“ad”,“ae”,“af”,“bd”,“be”,“bf”,“cd”,“ce”,“cf”]
示例2:
输入:digits = “”
输出:[]
示例3:
输入:digits = “2”
输出:[“a”,“b”,“c”]
🧡 算法分析
This method is to usedfs
下图解释dfs遍历过程(类似):
算法步骤:
- Iterate over which character appears in the first position
- 深度 + 1, Strings concatenated at the same time + 1, Check whether the length is equal to the length of the original string in the front
- 详见代码
代码实现
class Solution {
public:
vector<string> ans;
string strs[10] = {
"", "", "abc", "def",
"ghi", "jkl", "mno",
"pqrs", "tuv", "wxyz",
};
vector<string> letterCombinations(string digits) {
if(digits.empty()) return ans;
dfs(digits, 0, "");
return ans;
}
void dfs(string s, int u, string path)
{
if(u == s.size()) ans.push_back(path);
else
{
for(auto c : strs[s[u] - '0']) // Traversing what the current bit can take
{
dfs(s, u + 1, path + c);
}
}
}
};
执行结果:
时间复杂度分析
According to the longest string length,dfs时间复杂度为O(4n)
如果觉得对你有帮助的话:
点赞,你的认可是我创作的动力!
🧡 收藏,你的青睐是我努力的方向!
️ 评论,你的意见是我进步的财富!
边栏推荐
- 比亚迪7月份销量爆表,产品结构非常合理
- 网络安全辅助工具:免费MD5解密网站
- Freemodbus 移植过程记录
- BYD's July sales exploded, and the product structure is very reasonable
- 从采集到存储:时序数据库到底怎么处理时间?
- 年金保险养老用合适吗?哪些产品值得入手?
- pcl 点云网络 化 vtk Delaunay 点云网络化
- A tester in 1995, he wouldn't dare to ask for 12K~ Looking at his resume, I have a lot of thoughts...
- js_数组对象改属性名
- Android数据库框架之greenDAO
猜你喜欢

Find the Nth node of the linked list

Android数据库框架之greenDAO

邓晔研究组在微生物功能基因的数据库开发和环境定量方面取得新进展

3D激光SLAM:LIO-SAM整体介绍与安装编译

Log Management Lombok Profile

查找链表第N个节点

A tester in 1995, he wouldn't dare to ask for 12K~ Looking at his resume, I have a lot of thoughts...

浏览器如何清除缓存教程

华为设备配置Smart Link负载分担

How to build a set of invoicing management system (purchasing, sales, inventory integrated management) with low code?
随机推荐
What skills should a test engineer have to get 30k?
关于软件测试计划的面试题,你能答对几个?
相机标定 >> 坐标系转换@内参、外参
币圈再次受攻击损失400万美元,9000多个钱包被破解
FTX exchange listed in Forbes 2022 fintech 50 list
高性能云原生数据对象存储MinIO实战-上
nRF52833-QIAA-R nordic无线收发芯片
详解AUTOSAR:什么是AUTOSAR?(理论篇—1)
九、MFC控件(一)
03.变量
Transformer pytorch实现逐行详解
产业园区实现产业集聚的三大举措
6种可改善软件的可用性测试方法
LeetCode每日两题02:最长回文子串 (均1200道)
Soul递交上市招股书,以技术为基石构建多元社交元宇宙
How to make a pictogram in PPT
GET和POST的区别
Xpansiv acquires APX to expand environmental commodity market infrastructure
Removal control of WPF screenshot control (9) "Imitation WeChat"
二级路由嵌套实现移动端案例