首页 > 其他 > 详细

17. 电话号码的字母组合

时间:2020-03-15 18:45:56      阅读:79      评论:0      收藏:0      [点我收藏+]
 1 //DFS问题一直很难
 2 class Solution 
 3 {
 4     void dfs(string digits,vector<vector<char>>& d,vector<string> &res,int cur,string& temp)
 5     {
 6         if(cur == digits.size()) 
 7         {
 8             res.push_back(temp);
 9             return;
10         }
11 
12         for(auto a : d[digits[cur] - 0])
13         {
14             temp.push_back(a);
15             dfs(digits,d,res,cur + 1,temp);
16             temp.pop_back();
17         }
18     }
19 public:
20     vector<string> letterCombinations(string digits) 
21     {
22         vector<vector<char>> d(10);
23         d[0] = { };
24         d[1] = {};
25         d[2] = {a,b,c};
26         d[3] = {d,e,f};
27         d[4] = {g,h,i};
28         d[5] = {j,k,l};
29         d[6] = {m,n,o};
30         d[7] = {p,q,r,s};
31         d[8] = {t,u,v};
32         d[9] = {w,x,y,z};
33         vector<string> res;
34         if(digits.empty()) return res;
35         string temp;
36         dfs(digits,d,res,0,temp);
37         return res;
38     }
39 };

 

17. 电话号码的字母组合

原文:https://www.cnblogs.com/yuhong1103/p/12499182.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!