Leetcode 17. 电话号码的字母组合(DAY 90) ---- Leetcode Hot 100
发布日期:2021-06-30 22:29:44
浏览次数:3
分类:技术文章
本文共 1563 字,大约阅读时间需要 5 分钟。
文章目录
原题题目
代码实现(首刷自解)
class Solution { public: vectorletterCombinations(string digits) { vector ret,temp; if(!digits.size()) return ret; vector > nums(8); char tempchr = 'a'; for(int i=0;i<8;++i) { for(int j=0;j<3;++j) nums[i].emplace_back(tempchr++); if(i==5 || i==7) nums[i].emplace_back(tempchr++); } for(const auto& num:digits) { temp.clear(); if(ret.empty()) ret.emplace_back(""); for(const auto& str:ret) { for(const auto& chr:nums[num-'2']) temp.emplace_back(str+chr); } ret = temp; } return ret; }};
代码实现(二刷自解双百DFS C++)
class Solution { public: void depthfirstsearch(vector& ret,const vector & str,const string& digits,string& temp,int pos) { if(pos == digits.size()) { if(pos) ret.emplace_back(temp); return; } for(const auto& chr:str[digits[pos]-'2']) { string tempstr = temp+chr; depthfirstsearch(ret,str,digits,tempstr,pos+1); } return; } vector letterCombinations(string digits) { vector ret; vector str{ "abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; string temp; depthfirstsearch(ret,str,digits,temp,0); return ret; }};
转载地址:https://love6.blog.csdn.net/article/details/115875361 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
逛到本站,mark一下
[***.202.152.39]2024年04月15日 13时11分34秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
YbtOJ——字符串处理【例题2】移位包含
2019-04-30
YbtOJ——广度搜索【例题2】山峰和山谷
2019-04-30
异步提交数据
2019-05-01
DVWA——Command Injection
2019-05-01
跨站请求伪造(CSRF)
2019-05-01
DVWA——文件包含
2019-05-01
HTTP:学习笔记一
2019-05-01
DVWA—brute force
2019-05-01
Web 4-24
2019-05-01
出租车计价器设计VHDL
2019-05-01
HTTP:学习笔记二
2019-05-01
汽车尾灯控制电路设计VHDL课程设计
2019-05-01
网上购物商店java web课程设计
2019-05-01
windows命令提示符运行.cpp文件
2019-05-01
生物化学·笔记 1-2章节
2019-05-01
生物化学·笔记 3章
2019-05-01
生化重点 生化重点
2019-05-01
医学细胞生物学复习资料
2019-05-01
大一医用生物学重点
2021-07-04
2021-06-22
2021-07-04