leetcode 17. Letter Combinations of a Phone Number

Given a string containing digits from 2-9 inclusive, 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. Note that 1 does not map to any letters.

在这里插入图片描述

Example:

Input: “23”
Output: [“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf”].

这个题可能是在考 for 循环吧…

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

在这里插入图片描述

posted @ 2020-05-20 10:38  winechord  阅读(67)  评论(0编辑  收藏  举报