17. Letter Combinations of a Phone Number

来源:互联网 发布:2016淘宝客导购源码 编辑:程序博客网 时间:2024/06/07 17:32

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.

Subscribe to see whic

static map<char,vector<string>> mm;class Solution {    private:    int n=0;      void init()      {        mm['0']={" "};        mm['1']={"*"};        mm['2']={"a","b","c"};        mm['3']={"d","e","f"};        mm['4']={"g","h","i"};        mm['5']={"j","k","l"};        mm['6']={"m","n","o"};        mm['7']={"p","q","r","s"};        mm['8']={"t","u","v"};        mm['9']={"w","x","y","z"};      }public:    void backtrack(string digits,vector<string>&  r1,int k,string temp)    {        if(temp.size()==digits.size())        {            r1.push_back(temp);            return ;        }        else        {            for(int i=k;i<digits.size();++i)            {                for(int j=0;j<mm[digits[i]].size();++j)                {                    backtrack(digits,r1,i+1,temp+mm[digits[i]][j]);                }            }            return ;        }    }    vector<string> letterCombinations(string digits) {        init();        vector<string>  r1;        if(digits.empty()) return r1;        backtrack(digits,r1,0,"");        return r1;    }};

h companies asked this question

0 0
原创粉丝点击