【回溯】17. Letter Combinations of a Phone Number

本文介绍了一种使用回溯算法生成电话按键对应的所有可能字母组合的方法。通过递归调用,实现了对输入数字串的每个数字对应的字母进行组合,最终得到所有可能的字符串组合。

采用回溯操作:

    vector<string> letterCombinations(string digits) {
     string table[11]={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
     vector<string> res;
     string temp;
     if(digits.size()>0)
         find(res,digits,table,0,temp);
     return res;

    }

 void find(vector<string> &res,string &digits,string *table,int index,string &temp)
 {
 	if(index == digits.size())
 	{
 		res.push_back(temp);
 		return ;
 	}

 	for (int i = 0; i<table[digits[index]-'0'].size(); i++)
	{
        temp = temp + table[digits[index] - '0'][i];
		find(res, digits, table, index + 1, temp);
        temp.erase(temp.size()-1);
	}
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值