17. Letter Combinations of a Phone Number

来源:互联网 发布:韩子高网络剧演员表 编辑:程序博客网 时间:2024/06/05 07:53

题目:

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

枚举递归

class Solution {public:    map<int,string> m={{2,"abc"},{3,"def"},{4,"ghi"},{5,"jkl"},{6,"mno"},{7,"pqrs"},{8,"tuv"},{9,"wxyz"}};    vector<string> letterCombinations(string digits) {         vector<string> res;        string temp;        if(digits.empty())                      return res;            solve(digits,0,res,temp);        return res;    }        void solve(string digits,int index,vector<string> &res,string temp){        if(index==digits.size())            res.push_back(temp);        int i=digits[index]-'0';        for(int j=0;j<m[i].size();j++){            solve(digits,index+1,res,temp+m[i][j]);        }    }};