Leetcode 17. 电话号码的字母组合(DAY 90) ---- Leetcode Hot 100
发布日期:2021-06-30 22:29:44 浏览次数:2 分类:技术文章

本文共 1563 字,大约阅读时间需要 5 分钟。

文章目录


原题题目


在这里插入图片描述


代码实现(首刷自解)


class Solution {
public: vector
letterCombinations(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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Leetcode 621. 任务调度器(DAY 91) ---- Leetcode Hot 100(给妹妹送奶茶了 嘻嘻 :) )
下一篇:Leetcode 75. 颜色分类(DAY 90) ---- Leetcode Hot 100(今天一天快把半学期的高数赶完了 明天再补补了 力扣每天还是得刷)

发表评论

最新留言

关注你微信了!
[***.104.42.241]2024年04月19日 22时14分19秒