17. 电话号码的字母组合(回溯)

题目链接:https://leetcode-cn.com/classic/problems/letter-combinations-of-a-phone-number/description/

题目大意:就是2到9这8个数字,每个数字分别有一个字符子串,然后用户输入一系列数字串,让你输出数字串中的每个数字都派出一个字符的所有搭配。

思路:很简单,dfs的思路。对每个数字的字符子串的所有字符进行遍历。
return_begin(x)获得数字字符x对应字符子串的头字符
return_end(x) 获得数字字符x对应字符子串的尾字符
dfs(int now,string str,string digit) now是str的字符个数 ,str是当前搭配的字符,digit是用户输入的数字字符串

代码:

class Solution
{
public:
    //dfs方法
    vector<string> ans;
    char return_begin(char x)
    {
        if(x=='2')
            return 'a';
        if(x=='3')
            return 'd';
        if(x=='4')
            return 'g';
        if(x=='5')
            return 'j';
        if(x=='6')
            return 'm';
        if(x=='7')
            return 'p';
        if(x=='8')
            return 't';
        if(x=='9')
            return 'w';
        else 
        return ' ';
    }

    char return_end(char x)
    {
        if(x=='2')
            return 'c';
        if(x=='3')
            return 'f';
        if(x=='4')
            return 'i';
        if(x=='5')
            return 'l';
        if(x=='6')
            return 'o';
        if(x=='7')
            return 's';
        if(x=='8')
            return 'v';
        if(x=='9')
            return 'z';
        else 
        return ' ';
    }
    void dfs(int now,string str,string digit)
    {
        int len=digit.length();
        if(now==len)
        {
            ans.push_back(str);
            return;
        }
        if(digit[now]<='1'||(digit[now]>'9'))
            return;
        char begin=return_begin(digit[now]);//得到起点
        char end=return_end(digit[now]);//得到终点
        for(char x=begin; x<=end; x++)
        {
            dfs(now+1,str+x,digit);
        }
    }
    vector<string> letterCombinations(string digits)
    {
        string s="";
        if(digits=="")
            return ans;
        else{
        dfs(0,s,digits);
        }
        return ans;
    }
};

另一种:递推法
思路:我们用三层循环去写,
第一层:digits的所有数字
第二层:ans存的所有暂时字符串,注意这里要先把ans最前面的字符串s删除,然后s与第三层的字符遍历相加,存入ans
第三层:第一层当前遍历到的数字相对应的所有字符
注意:vector容器的大小是可变的,所以在for循环中不要使用vector.size()这类方法,否则会发生程序错误。

class Solution
{
public:
    vector<string> letterCombinations(string digits)
    {
        vector<string> ans;
        if(digits=="")
            return ans;
        else
        {
            ans.push_back("");//这里是非常必要的
            vector<string> dict(10);
            dict[2] = "abc";
            dict[3] = "def";
            dict[4] = "ghi";
            dict[5] = "jkl";
            dict[6] = "mno";
            dict[7] = "pqrs";
            dict[8] = "tuv";
            dict[9] = "wxyz";
            for(int i=0; i<digits.size(); i++)
            {
                int size=ans.size();//ans的大小需要去用变量去存,因为for里面的语句会改变ans的大小
                for(int h=0; h<size; h++)
                {
                    string cur=ans[0];
                    ans.erase(ans.begin());
                    int t=digits[i]-'0';
                    for(int j=0; j<dict[t].size(); j++)
                    {
                        ans.push_back(cur+dict[t][j]);
                    }
                }
            }
            return ans;
        }
        
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值