LeetCode 0017 - Letter Combinations of a Phone Number

2021-08-11 10:17:30 浏览数 (1)

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.

代码语言:javascript复制
Input:Digit string "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

Note:

Although the above answer is in lexicographical order, your answer could be in any order you want.

Solution

代码语言:javascript复制
class Solution {
private:
    vector<string>mp = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};    
public:
    vector<string> letterCombinations(string digits) {
        vector<string> res;
        if(digits.empty())
            return res;
        res.push_back("");
        for(int i = 0; digits[i]; i  ){
            vector<string> tmp;
            for(int j = 0; mp[digits[i]-'0'][j]; j  )
                for(int k = 0; k < res.size(); k  )
                    tmp.push_back(res[k]   mp[digits[i] - '0'][j]);
            res = tmp;
        }
        return res;
    }
};

0 人点赞