zoukankan      html  css  js  c++  java
  • LeetCode ——*的字母组合

    题目如下:

    采用递归的方式,判断是否到达字符串尾部,到达尾部则添加结果到result。

    代码如下:

    map<char, string> cs{ { '2', "abc" }, { '3', "def" }, { '4', "ghi" }, { '5', "jkl" }, { '6', "mno" },
    { '7', "pqrs" }, { '8', "tuv" }, { '9', "wxyz" } };
    vector<string> result;
    void combine(const string &s,string tmp, int index)
    {
        string current = cs[s[index]];
        if (index == s.size() - 1)
        {
            for (int i = 0; i < current.size(); i++)
                result.push_back(tmp + current[i]);
            return;
        }
        for (int i = 0; i < current.size(); i++)
            combine(s, tmp+current[i], index + 1);
    
    }
    vector<string> letterCombinations(string digits) {
        result.clear();
        int length = digits.size();
        if (length < 1)
            return vector<string>();
        combine(digits, "", 0);
        return result;
    }
  • 相关阅读:
    SCUT
    SCUT
    SCUT
    ???
    Codeforces
    SCUT
    SCUT
    SCUT
    SCUT
    2019牛客暑期多校训练营(第八场)
  • 原文地址:https://www.cnblogs.com/Oscar67/p/9069148.html
Copyright © 2011-2022 走看看