首页 > 其他 > 详细

Letter Combinations of a Phone Number

时间:2015-03-05 16:40:36      阅读:325      评论: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.

  

  这道题不算难,想想就出来了。

solution:

vector<string> letterCombinations(string digits) {
    int n = digits.size();
    vector<string> res;
    if(n == 0)
        return res;
    res.push_back("");
    string numap[] = {"#","#","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
    for (int i = 0;i < n;++i)
    {
        vector<string> tmp;
        for(int j = 0;j < res.size();++j)
            for(int k = 0;k < numap[digits[i]-0].size();++k)
                tmp.push_back(res[j] + numap[digits[i]-0][k]);
        res = tmp;
    }
    return res;
}

  此题还有其他解法,《编程之美》中也有相似题目,暂时先这样了。

Letter Combinations of a Phone Number

原文:http://www.cnblogs.com/gattaca/p/4315884.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!