1. 程式人生 > >leetcode --17. Letter Combinations of a Phone Number

leetcode --17. Letter Combinations of a Phone Number

題目:https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/

程式碼:

class Solution {
public:
    vector<string> letterCombinations(string digits) {
        vector<string> res;
        if(digits.size()==0) return res;
        string m[10] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        res.push_back("");
        for(int i =0;i<digits.size();i++){
            vector<string> temp;
            string t = m[digits[i]-'0'];
            for(int j =0;j<t.size();j++)
                for(int k=0;k<res.size();k++)
                    temp.push_back(res[k]+t[j]);
            res = temp;
        }
        return res;
    }
};