leetcode131:letter-combinations-of-a-phone-number

题目描述

给出一个仅包含数字的字符串,给出所有可能的字母组合。
数字到字母的映射方式如下:(就像电话上数字和字母的映射一样)
Input:Digit string "23"Output:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
注意:虽然上述答案是按字典序排列的,但你的答案可以按任意的顺序给出

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.

示例1

输入

复制
"23"

输出

复制
["ad","ae","af","bd","be","bf","cd","ce","cf"]



class Solution {
private:
    map<char,string> mp={{'2',"abc"},{'3',"def"},{'4',"ghi"},{'5',"jkl"},{'6',"mno"},{'7',"pqrs"},{'8',"tuv"},{'9',"wxyz"}};
    vector<string> res;
    string temp;
public:
    void combine(string &digits,int start){//回溯法
        if(start==digits.size()){
            res.push_back(temp);return;
        }
        for(int i=0;i<mp[digits[start]].size();i++){
            temp+=mp[digits[start]][i];
            combine(digits,start+1);
            temp.pop_back();
        }
    }
    vector<string> letterCombinations(string digits) {
        combine(digits,0);
        return res;
    }
};
posted on 2020-08-01 10:56  滚雪球效应  阅读(152)  评论(0编辑  收藏  举报