LeetCode-Letter Combinations of a Phone Number-电话号码字母组合-DFS

https://oj.leetcode.com/problems/letter-combinations-of-a-phone-number/

简单DFS题。实际上只会出现[2,9]的数字。

int t[11]={0,0,0,3,6,9,12,15,19,22,26};
class Solution {
public:
    int n,m;
    string d;
    vector<string> tot;
    string cur;
    void Solve(int p){
        if (p==n) {
            tot.push_back(cur);
            return;
        }
        int a=d[p]-'0';
        int s=t[a];
        int e=t[a+1];
        for (int i=s;i<e;i++){
            cur[p]=i+'a';
            Solve(p+1);
        }
    }
    vector<string> letterCombinations(string digits) {
        n=digits.size();
        d=digits;
        cur.resize(n,'a');
        Solve(0);
        return tot;
    }
};

  

posted @ 2014-10-11 13:48  zombies  阅读(152)  评论(0编辑  收藏  举报