Leetcode: Letter Combinations of a Phone Number

原创 2016年08月31日 16:39:33
class Solution {
public:
    map<int,vector<char> >ans;
    vector<string>answer;
    vector<string> letterCombinations(string &digits) {
        answer.clear();
        if(digits.length()==0){
            return answer;
        }
        char a='a'-1;
        for(int i=2;i<=7;i++){
            for(int j=1;j<=3;j++)
            ans[i].push_back(++a);
        }
        ans[7].push_back(++a);

        for(int i=8;i<=9;i++){
            for(int j=1;j<=3;j++){
                ans[i].push_back(++a);
            }
        }
        ans[9].push_back(++a);
        int len=digits.length();
         dfs(0,len,digits,"");
       return answer;
        
    }

    void dfs(int dep,int max_dep,string &s,string ans_str){
     // cout<<dep<<" "<<max_dep<<" "<<s<<" "<<ans_str<<endl;
        if(dep==max_dep){
            answer.push_back(ans_str);
            return;
        }
       // cout<<ans[1].size()<<endl;
     //   cout<<ans[s[dep]-'0'].size()<<endl;
        for(int i=0;i<ans[s[dep]-'0'].size();i++){
            dfs(dep+1,max_dep,s,ans_str+ans[s[dep]-'0'][i]);
        }
    }
};

版权声明:本文为博主原创文章,欢迎指教~

相关文章推荐

Letter Combinations of a Phone Number [leetcode]谈谈循环解法的两种思路

本系列博文中有很多两种思路的,其实是因为第一遍刷题的时候有一个想法,第二遍刷题的时候已经忘掉之前的思路了,又有新的想法了。 同时大部分代码我也同时PO到leetcode的对应题目的问答中去了,所以如...

Letter Combinations of a Phone Number - Leetcode

题意:Given a digit string, return all possible letter combinations that the number could represent. ...

[leetcode 18] Letter Combinations of a Phone Number

题目: Given a digit string, return all possible letter combinations that the number could represe...

[LeetCode] 017. Letter Combinations of a Phone Number (Medium) (C++/Java/Python)

[LeetCode] 017. Letter Combinations of a Phone Number (Medium) (C++/Java/Python)
  • hcbbt
  • hcbbt
  • 2015-03-04 15:47
  • 2617

LeetCode Letter Combinations of a Phone Number

题目: Given a digit string, return all possible letter combinations that the number could represe...

[Leetcode]Letter Combinations of a Phone Number

Given a digit string, return all possible letter combinations that the number could represent. A map...

LeetCode:Letter Combinations of a Phone Number

这题没什么算法,就是锻炼编程能力,是很好的一个题目。 import java.util.ArrayList; import java.util.Arrays; import java.uti...

[Java]LeetCode17 Letter Combinations of a Phone Number

[Java]LeetCode17 Letter Combinations of a Phone Number

LeetCode OJ-17-Letter Combinations of a Phone Number

题目:Given a digit string, return all possible letter combinations that the number could represent. A...

LeetCode OJ-17.Letter Combinations of a Phone Number(DFS)

LeetCode OJ-17.Letter Combinations of a Phone Number(DFS解法)题目描述 Given a digit string, return all p...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)