17.leetcode Letter Combinations of a Phone Number(meidum)[递归回溯]
来源:互联网 发布:谭浩强c语言第四版如何 编辑:程序博客网 时间:2024/05/16 06:12
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"].
这道题与常见的递归回溯没有区别,但是需要注意的是每个数字对应的字符与ascii之间的转换关系,当字符为7和9时字符个数变成了4,因此后面的计算字符的公式需要 重新定义。
class Solution {public: void getLetter(int start,int n,string &temp,string &digit,vector<string> &result) { if(start == n) { if(temp != "") result.push_back(temp); return ; } int k = 3; for(int i = 0;i<k;i++) { char c = digit[start]; if(c<='1'||c>'9') return ; if(c == '7' ||c == '9') k = 4; char d; if(c == '8' || c == '9') d = c+(c-'2')*2+47+i+1; else d = c+(c-'2')*2+47+i; string bef = temp; temp += d; getLetter(start+1,n,temp,digit,result); temp = bef; } return; } vector<string> letterCombinations(string digits) { int n = digits.size(); vector<string> result; if(n <=0) return result; string temp = ""; getLetter(0,n,temp,digits,result); return result; }};
0 0
- 17.leetcode Letter Combinations of a Phone Number(meidum)[递归回溯]
- LeetCode 17. Letter Combinations of a Phone Number 递归回溯法
- [leetcode][回溯] Letter Combinations of a Phone Number
- LeetCode-17-Letter-Combinations-of-a-Phone-Number 递归+模拟
- 17. Letter Combinations of a Phone Number-回溯算法
- 17. Letter Combinations of a Phone Number 回溯算法
- 17. Letter Combinations of a Phone Number 组合、回溯
- LeetCode: Letter Combinations of a Phone Number
- LeetCode Letter Combinations of a Phone Number
- LeetCode: Letter Combinations of a Phone Number
- [Leetcode] Letter Combinations of a Phone Number
- [LeetCode] Letter Combinations of a Phone Number
- 【leetcode】Letter Combinations of a Phone Number
- LeetCode: Letter Combinations of a Phone Number
- [LeetCode]Letter Combinations of a Phone Number
- LeetCode-Letter Combinations of a Phone Number
- [LeetCode] Letter Combinations of a Phone Number
- LeetCode - Letter Combinations of a Phone Number
- K3.BOS插件开发记录
- anddroid异常处理之UncaughtException
- PowerDesigner逆向工程导入MYSQL数据库总结
- 自动布局之autoresizingMask使用详解(Storyboard&Code)
- OkHttp框架从入门到放弃,解析图片使用Picasso裁剪,二次封装OkHttpUtils,Post提交表单数据
- 17.leetcode Letter Combinations of a Phone Number(meidum)[递归回溯]
- 如何解决磁盘坏道的问题
- Ubuntu 16.04中安装OpenCV 2.4.11
- 使用 pdb 进行调试
- 网页播放全景视频和全景图片
- ListView的动画
- C#TextBox控件拖拽实现获得文件路径
- Cannot change version of project facet Dynamic Web Module to 2.5.
- 一个关于缓存的问题