【LeetCode】017. Letter Combinations of a Phone Number

题目: 

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

A mapping of digit to letters (just like on the telephone buttons) is given below.

Input:Digit string "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

  

题解:

Solution 1 ()

class Solution {
public: 
    void dfs(string digits, int level, vector<string>& vv, string& s, vector<string> dict){
        if(level >= digits.size()) {
            vv.push_back(s);
            return;
        }
        string tmp = dict[digits[level] - '2'];
        for(int i=0; i<tmp.size(); ++i) {
            s.push_back(tmp[i]);
            dfs(digits, level+1, vv, s, dict);
            s.pop_back();
        }
    }
    vector<string> letterCombinations(string digits) {
        if(digits.empty()) return vector<string>();
        vector<string> dict = {"abc","def","ghi","jkl",
                                "mno","pqrs","tuv","wxyz"};
        vector<string> vv;
        string s;
        dfs(digits, 0, vv, s, dict);
        return vv;                
    }
};

 

posted @ 2017-05-07 21:15  Vincent丶丶  阅读(204)  评论(0)    收藏  举报