Letter Combinations of a Phone Number

发布时间:2016-12-11 0:42:01 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"Letter Combinations of a Phone Number",主要涉及到Letter Combinations of a Phone Number方面的内容,对于Letter Combinations of a Phone Number感兴趣的同学可以参考一下。

Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]. Note: Although the above answer is in lexicographical order, your answer could be in any order you want. 本题有点像深度遍历所有路径! //注意的地方: 1、0和1无对应字母 2、7,9对应4个字母,其他3个。故而需取相应大小。 vector<string> letterCombinations(string digits) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. string str[10]={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};//前俩个为空! vector<string> result; string ans; letter_dfs(digits,str,0,ans,result); return result; } void letter_dfs(const string digits,const string *str,int index,string ans,vector<string> &result) { if(index == digits.size()) { result.push_back(ans); return ; } else { for(int i=0;i<str[digits[index]-'0'].size();i++)//大小的确定 { ans.push_back(str[digits[index]-'0'][i]); letter_dfs(digits,str,index+1,ans,result); ans.pop_back(); //以上可以用一句代替:(简化的话修改相应形参) //letter_dfs(digits,str,index+1,ans+str[digits[index]-'0'][i],result);//技巧很强! } } }

上一篇:boost库的添加
下一篇:

相关文章

相关评论