Leetcode 17. 电话号码的字母组合(DAY 90) ---- Leetcode Hot 100
发布日期:2021-06-30 22:29:44
浏览次数:2
分类:技术文章
本文共 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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
关注你微信了!
[***.104.42.241]2024年04月19日 22时14分19秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
ubuntu中配置任意应用的快捷键
2021-07-03
ln 命令 硬链接
2021-07-03
使用鼠标中键在vim下赋值文本到其它编辑器
2021-07-03
chkconfig使用和级别介绍
2021-07-03
snort 笔记1 ----- 3种模式简介
2021-07-03
ubuntu 快捷键
2021-07-03
linux 根目录下文件夹分析
2021-07-03
ubuntu tar备份
2021-07-03
My notes about backup to ubuntu
2021-07-03
linux 查看分区和文件大小
2021-07-03
IP TCP UDP 结构
2021-07-03
Not using PCAP_FRAMES 解释(snort中)
2021-07-03
数字信号处理——FIR滤波器设计
2021-07-03
技术转管理?这些“坑”你要绕道走
2021-07-03
领域驱动设计(DDD)前夜:面向对象思想
2021-07-03
Ubuntu 14.04 安装TM2009/QQ
2021-07-03
Ubuntu 14.04 安装VMware
2021-07-03
如何解决Linux下USB设备节点名不固定问题
2021-07-03
Camera驱动调试小记
2021-07-03
linux内核input子系统解析
2021-07-03