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"].

解答:



我的答案:回溯,时间慢 2ms

class Solution {
private:
vector<string> vec{"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
int number = 0;
vector<string> vs;
string digit;

public:
    void combine(string str, int l){
        if(l == number){
            vs.push_back(str);
            return;
        }
        int nums = digit[l]-'0';
        string alpha = vec[nums];
        for(int k = 0; k < alpha.size(); k++){
            combine(str+alpha[k],l+1);
        }
    }


    vector<string> letterCombinations(string digits) {
        int len = digits.size();
        string s;
        if(len < 1)
        return vs;
        number = len;
        digit = digits;
        combine(s,0);
        return vs;
    }
};


2.别人的代码 快,0ms   

对别人优秀的代码致敬!

vector<string> letterCombinations(string digits) {
    vector<string> result;
    if(digits.empty()) return vector<string>();
    static const vector<string> v = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
    result.push_back("");   // add a seed for the initial case
    for(int i = 0 ; i < digits.size(); ++i) {
        int num = digits[i]-'0';
        if(num < 0 || num > 9) break;
        const string& candidate = v[num];
        if(candidate.empty()) continue;
        vector<string> tmp;
        for(int j = 0 ; j < candidate.size() ; ++j) {
            for(int k = 0 ; k < result.size() ; ++k) {
                tmp.push_back(result[k] + candidate[j]);
            }
        }
        result.swap(tmp);
    }
    return result;
}

Explanation with sample input "123"

Initial state:

  • result = {""}

Stage 1 for number "1":

  • result has {""}
  • candiate is "abc"
  • generate three strings "" + "a", ""+"b", ""+"c" and put into tmp, tmp = {"a", "b","c"}
  • swap result and tmp (swap does not take memory copy)
  • Now result has {"a", "b", "c"}

Stage 2 for number "2":

  • result has {"a", "b", "c"}
  • candidate is "def"
  • generate nine strings and put into tmp, "a" + "d", "a"+"e", "a"+"f", "b" + "d", "b"+"e", "b"+"f", "c" + "d", "c"+"e", "c"+"f"
  • so tmp has {"ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf" }
  • swap result and tmp
  • Now result has {"ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf" }

Stage 3 for number "3":

  • result has {"ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf" }
  • candidate is "ghi"
  • generate 27 strings and put into tmp,
  • add "g" for each of "ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"
  • add "h" for each of "ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"
  • add "h" for each of "ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"
  • so, tmp has {"adg", "aeg", "afg", "bdg", "beg", "bfg", "cdg", "ceg", "cfg" "adh", "aeh", "afh", "bdh", "beh", "bfh", "cdh", "ceh", "cfh" "adi", "aei", "afi", "bdi", "bei", "bfi", "cdi", "cei", "cfi" }
  • swap result and tmp
  • Now result has {"adg", "aeg", "afg", "bdg", "beg", "bfg", "cdg", "ceg", "cfg" "adh", "aeh", "afh", "bdh", "beh", "bfh", "cdh", "ceh", "cfh" "adi", "aei", "afi", "bdi", "bei", "bfi", "cdi", "cei", "cfi" }

你可能感兴趣的:(LeetCode,C++,backtracking)