Leetcode 17. Letter Combinations of a Phone Number(水)

17. Letter Combinations of a Phone Number
Medium

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

Note:

Although the above answer is in lexicographical order, your answer could be in any order you want.

 1 class Solution {
 2 public:
 3     vector<string> letterCombinations(string digits) {
 4         map<char,string> test;
 5         test['2'] = "abc";
 6         test['3'] = "def";
 7         test['4'] = "ghi";
 8         test['5'] = "jkl";
 9         test['6'] = "mno";
10         test['7'] = "pqrs";
11         test['8'] = "tuv";
12         test['9'] = "wxyz";
13         vector<string> fa_;
14         vector<string> chil_;
15         fa_.push_back("");
16         for(int i = 0; i < digits.length(); i++){
17             chil_.clear();
18             for(int j = 0; j < fa_.size(); j++){
19                 //printf("%d %s",test[digits[i]].length());
20                 for(int k = 0; k < test[digits[i]].length(); k++){
21                     //printf("%s",test[digits[i]][k]);
22                     chil_.push_back(fa_[j]+test[digits[i]][k]);
23                 }
24             }
25             fa_ = chil_;
26         }
27         return chil_;
28     }
29 };
原文地址:https://www.cnblogs.com/shanyr/p/11518389.html