回溯Leetcode 17 Letter Combinations of a Phone Number

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/shey666/article/details/80786923

Leetcode 17

Letter Combinations of a Phone Number

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

猜你喜欢

转载自blog.csdn.net/shey666/article/details/80786923