首页 > 其他 > 详细

LeetCode ——电话号码的字母组合

时间:2018-05-21 20:48:11      阅读:244      评论:0      收藏:0      [点我收藏+]

题目如下:

技术分享图片

采用递归的方式,判断是否到达字符串尾部,到达尾部则添加结果到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;
}

 

LeetCode ——电话号码的字母组合

原文:https://www.cnblogs.com/Oscar67/p/9069148.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!