Leetcode 17. 电话号码的字母组合

简单题,暴力枚举即可

class Solution {
public:
    vector<string> ans;
    string s[8] = { "abc","def","ghi","jkl","mno","pqrs","tuv","wxyz" };
    void dfs(int index, string &str, string &digits) {
        if (index == digits.size()) { ans.push_back(str); return; }
        for (auto x : s[digits[index] - '2'])
            str.push_back(x), dfs(index + 1, str, digits), str.pop_back();
    }
    vector<string> letterCombinations(string digits) {
        ans.clear();
        if (digits.empty()) return ans;
        string sss = "";
        dfs(0, sss, digits);
        return ans;
    }
};

你可能感兴趣的:(LeetCode)