Leetcode Week11

来源:互联网 发布:中国跨境电子商务数据 编辑:程序博客网 时间:2024/05/22 15:04

/*

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

*/

class Solution {public:    vector<string> letterCombinations(string digits) {        vector<string> res;        string charmap[10] = {"0","1","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};         if(digits.size()==0) return res;        res.push_back("");        for(int i=0;i<digits.size();i++)        {            vector<string> temp;            string chars = charmap[digits[i]-'0'];            for(int c=0;c<chars.size();c++)            {                for(int j=0;j<res.size();j++)                {                    temp.push_back(res[j]+chars[c]);                }            }            res = temp;        }        return res;            }};


0 0
原创粉丝点击