码迷,mamicode.com
首页 > 其他好文 > 详细

Letter Combinations of a Phone Number

时间:2015-09-10 16:05:38      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:

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"].

 1  vector<string> letterCombinations(string digits) {
 2       string trans[] = {"", " ", "abc", "def", "ghi", "jkl",  
 3         "mno", "pqrs", "tuv", "wxyz"};  
 4       vector<string> set;      
 5       string seq;  
 6       Generater(trans, digits, 0, seq, set);  
 7       return set;  
 8     }  
 9     void Generater(string trans[], string& digits,   
10       int deep, string& result, vector<string>& set)  
11     {  
12       if(deep == digits.size())  
13       {  
14         set.push_back(result);  
15         return;  
16       }  
17       int curDig = digits[deep] - 48;  
18       for(int i =0; i < trans[curDig].size(); i++)  
19       {  
20         result.push_back(trans[curDig][i]);  
21         Generater(trans, digits, deep+1, result, set);  
22         result.resize(result.size() -1);  
23       }  
24     }

 

Letter Combinations of a Phone Number

标签:

原文地址:http://www.cnblogs.com/hexhxy/p/4797923.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!