标签:graphic http tput present ida abc button 基础 into
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.
关键就是两个循环,在原有X的基础上,比如新加数字3,则Xd Xe Xf
class Solution { public: vector<string> letterCombinations(string digits) { vector<string> result; if(digits.empty()) return vector<string>(); vector<string> v = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; result.push_back(""); // add a seed for the initial case for(int i = 0 ; i < digits.size(); ++i) { int num = digits[i]-‘0‘; if(num < 0 || num > 9) break; string candidate = v[num]; if(candidate.empty()) continue; vector<string> tmp; for(int j = 0 ; j < candidate.size() ; ++j) { for(int k = 0 ; k < result.size() ; ++k) { tmp.push_back(result[k] + candidate[j]); } } result = tmp; } return result; } };
Explanation with sample input "123"
Initial state:
Stage 1 for number "1":
Stage 2 for number "2":
Stage 3 for number "3":
Finally, return result.
【Leetcode】17、Letter Combinations of a Phone Number
标签:graphic http tput present ida abc button 基础 into
原文地址:https://www.cnblogs.com/shiganquan/p/9395367.html