17.Letter Combinations of a Phone Number

 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 {
private:
    vector<string> res;
    string temp;
public:
    void getRes(string digits,int len,map<char,string> num_map,int index)
    {
        if(index>=len)
        {
            res.push_back(temp);
            return; 
        }
        char   strNum=digits[index];
        string strLetter=num_map[strNum];
        for(int j=0;j<strLetter.size();j++)
        {
            temp.push_back(strLetter[j]);
            getRes(digits,len,num_map,index+1);
            temp.pop_back();
        }
        
    }
    vector<string> letterCombinations(string digits) {
        map<char,string> num_map;
        num_map['0']="";
        num_map['1']="";
        num_map['2']="abc";
        num_map['3']="def";
        num_map['4']="ghi";
        num_map['5']="jkl";
        num_map['6']="mno";
        num_map['7']="pqrs";
        num_map['8']="tuv";
        num_map['9']="wxyz";
        int len=digits.size();
        if(len==0)
            return res;
        getRes(digits,len,num_map,0);
        return res;
    }
};

 

posted @ 2015-06-02 19:08  linqiaozhou  阅读(715)  评论(0)    收藏  举报