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"].
这道题不难,但是我觉得写代码的时候有点绕。。。
class Solution { public: vector<string> letterCombinations(string digits) { string d[] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; vector<string> v={""}; if(digits.empty()) return v={}; //3个for循环让我脑袋晕 for(int i=0;i<digits.size();i++){ vector<string> tem; for(int j=0;j<v.size();j++){ for(int k=0;k<d[digits[i]-'0'].size();k++) tem.push_back(v[j]+d[digits[i]-'0'][k]); } v = tem; } return v; } };
Mark一波,过几天再看。。