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"].answer:class Solution {public: vector<string> letterCombinations(string digits) { vector<string> letter = {"0","1","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; vector<string> result1,result2; result1.push_back(""); for(int i = 0; i < digits.length(); i ++){ result2.clear(); vector<string>(result2).swap(result2); int index = digits[i] - '0'; string temp = letter[index]; for(int j = 0; j < temp.length(); j ++){ for(int k = 0; k < result1.size(); k ++){ result2.push_back(result1[k] + temp[j]); } } result1 = result2; } return result2; }};
新闻热点
疑难解答