leetcode_17电话号码的组合

1. 题意

输出电话号码对应的字母左右组合
电话号码的组合

2. 题解

回溯

class Solution {
public:
    void gen_res(vector<string> &res, vector<string> &s_m,
     string &digits, string &t, size_t depth) {
        if (depth == digits.size()) {
            if ( !t.empty())
                res.push_back(t);
            return ;
        }

        int k = digits[depth] - '0';
        int k_len = s_m[k].size();

        for ( int i = 0;i < k_len; ++i) {
            t.push_back(s_m[k][i]);
            gen_res(res, s_m, digits, t, depth + 1);
            t.pop_back();
        }

    }

    vector<string> letterCombinations(string digits) {
        vector<string> s_m = { "","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};

        vector<string> res;

        string t;
        gen_res(res, s_m, digits, t, 0 );

        return res;
    }
};

你可能感兴趣的:(leetcode,leetcode,算法)