LeetCode 17: Letter Combinations of a Phone Number

发布时间:2014-10-22 14:09:08编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"LeetCode 17: Letter Combinations of a Phone Number",主要涉及到LeetCode 17: Letter Combinations of a Phone Number方面的内容,对于LeetCode 17: Letter Combinations of a Phone Number感兴趣的同学可以参考一下。

Difficulty: 3 Frequency 3 Problem: 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. Solution: class Solution { public: string letters[10] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; vector<string> letterCombinations(string digits) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<string> answer; vector<char> v_letters; DFS (digits, 0, v_letters, answer); return answer; } void DFS(string & digits, int i_level, vector<char> & v_letters, vector<string> & answer) { if (i_level==digits.size()) { string str_letter; for (int i = 0; i<v_letters.size(); i++) { str_letter += v_letters[i]; } answer.push_back(str_letter); return; } for (int i = 0; i<letters[digits[i_level]-'0'].size(); i++) { v_letters.push_back(letters[digits[i_level]-'0'][i]); DFS(digits, i_level+1, v_letters, answer); v_letters.pop_back(); } } }; Notes: Just DFS, It is boring.