Letter Combinations of a Phone Number
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"].
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.
class Solution { void dsf(string keyStrs[],string& digits,int digitsSize,int pos,vector<string>& res,string &oneOfRes) { if(digitsSize == 0){ return ; } if(pos==digitsSize){ res.push_back(oneOfRes); return; } string keyStr=keyStrs[digits[pos] - '0']; int keyStrSize = keyStr.size(); for(int j=0;j<keyStrSize;j++){ oneOfRes.push_back(keyStr[j]); dsf(keyStrs,digits,digitsSize,pos+1,res,oneOfRes); oneOfRes.pop_back(); } } public: vector<string> letterCombinations(string digits) { string keyStrs[]={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; vector<string> res; string oneOfRes; int digitsSize = digits.size(); dsf(keyStrs,digits,digitsSize,0,res,oneOfRes); return res; } };
写者:zengzy
出处: http://www.cnblogs.com/zengzy
标题有【转】字样的文章从别的地方转过来的,否则为个人学习笔记