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

leetcode[17]Letter Combinations of a Phone Number

时间:2015-02-10 14:44:37      阅读:181      评论: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"].

Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.

class Solution {
public:
void createMap(map<char,string> &dMap)
{
    dMap[0]="";
    dMap[1]="";
    dMap[2]="abc";
    dMap[3]="def";
    dMap[4]="ghi";
    dMap[5]="jkl";
    dMap[6]="mno";
    dMap[7]="pqrs";
    dMap[8]="tuv";
    dMap[9]="wxyz";
    return;
}
void dfsDigits(int curDeep, vector<string> &res, string currStr, const string digits, map<char,string> dMap)
{
    if (curDeep==digits.size())
    {
        res.push_back(currStr);
        return;
    }    
    for (int i=0; i<dMap[digits[curDeep]].size();i++)
    {
        dfsDigits(curDeep+1, res, currStr+dMap[digits[curDeep]][i], digits, dMap);
    }
}
vector<string> letterCombinations(string digits) 
{
    vector<string> res;
    const int len=digits.size();
    map<char,string> dMap;
    createMap(dMap);
    dfsDigits(0, res, "", digits, dMap);
    return res;
}
};

 

leetcode[17]Letter Combinations of a Phone Number

标签:

原文地址:http://www.cnblogs.com/Vae98Scilence/p/4283677.html

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