17 Letter Combinations of a Phone Number

/*
String[] mapping = new String[] {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
*/
class Solution {
public:
    vector<string> letterCombinations(string digits) {
        vector<string> ans;
        if(digits.size() == 0) return ans;
        string charmap[10] = {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};        
        vector<string> temp;
        ans.push_back("");
        for(int i = 0; i<digits.size();i++)
        {
            //int num = atoi(digits[i]);
            int num = digits[i]-'0';
            string str = charmap[num];
            for (char c:str)
            {
                for(string s :ans)
                {
                    temp.push_back(s+c);
                }
            }
            ans = temp;
            temp.clear();
        }
        return ans;
    }
};

猜你喜欢

转载自blog.csdn.net/bjzhaoxiao/article/details/80346589