Letter Combinations of a Phone Number
2015-09-08 00:10
337 查看
Letter Combinations of a Phone Number
Total Accepted: 51210 TotalSubmissions: 200367My Submissions
Question
Solution
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.
class Solution { public: vector<string> letterCombinations(string digits) { vector<string> re; string str = ""; char *tel[] = {"abc","def","ghi","jkl","mno","pqrs","tvu","wxyz"}; int n = digits.length(); if(!n) return re; Combination( re, str, tel, n,digits); return re; } void Combination(vector<string> &re,string str,char * *tel,int n,string digits) { if(n == 0) { re.push_back(str); return; }else { string ori = str; char tmp = digits[digits.length() - n] - '2'; if( tmp == 5 || tmp == 7) //4c次循环 { for( int i = 0; i < 4; i ++) { char ch = tel[tmp][i]; str = ori + ch; Combination( re,str,tel,n-1,digits); } }else//3次循环 { for( int i = 0; i < 3; i ++) { char ch = tel[tmp][i]; str = ori + ch; Combination( re,str,tel,n-1,digits); } } } } };
相关文章推荐
- Spring MVC 前端ajax传入多个参数(包含数组)后端controller的一次性接收
- 20150906全志a33上调通rtl8188eus的过程(sina33)
- 数据结构Java实现06----中缀表达式转换为后缀表达式
- tar 命令
- Neural Networks for Machine Learning by Geoffrey Hinton (7)序列建模与RNN
- REST 服务
- 通过 Linux 命令行连接 Wifi
- 什么情况让程序员处于水深火热中
- 寻网上商城系统开发合作
- github常见操作和常见错误!
- 如何提取淘宝类目
- git@git.oschina.net's password: Permission denied
- Windows tree命令
- 4.html列表标签
- __block在什么情况下使用
- UIcollectionView 加入尾部视图
- httpPost和httpGet的使用示例
- 关情纸尾-----Quartz2D-简介
- Git和GitHub入门教程
- 这是一个半主动被约会的想法