leetcode-Letter Combinations of a Phone Number

来源:互联网 发布:社交行为数据挖掘 编辑:程序博客网 时间:2024/05/22 01:43

iven 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.

void Combination(vector<string> &ret, char *letter, string s){    if(*letter == 0)    {        ret.push_back(s);        return;    }    vector<string> a = {" ","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};    int gap = *letter - '0';    int i = 0;    while(a[gap][i] != 0)    {        Combination(ret, letter+1, s+a[gap][i]);        i++;    }}class Solution {public:    vector<string> letterCombinations(string digits) {        vector<string> ret;        string s;        Combination(ret,&digits[0],s);        return ret;    }};


0 0
原创粉丝点击