LeetCode-17 Letter Combinations of a Phone Number

题目链接

递归穷举

class Solution {
public:
    string s[10] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
    vector<string> res;
    
    vector<string> letterCombinations(string digits) 
    {
        if (digits.length() == 0) return res;
        string temp = "";
        f(digits, 0, temp);
        
        return res;
    }
    
    void f(string digits, int n, string temp)
    {
        if (n == digits.length())
        {
            res.push_back(temp);
            return;
        }
        int index = digits[n] - '0';
        for (int i = 0; i < s[index].length(); i++)
        {
            temp += s[index][i];
            f(digits, n + 1, temp);
            temp.erase(temp.begin() + temp.length() - 1);
        }
    }
};

猜你喜欢

转载自blog.csdn.net/qq_30986521/article/details/80929482