Letter Combinations of a Phone Number
来源:互联网 发布:office fix it 2007 编辑:程序博客网 时间:2024/06/06 16:30
-----QUESTION-----
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"].
-----SOLUTION-----
class Solution {public: vector<string> letterCombinations(string digits) { digitLetter.clear(); result.clear(); createMap(); string str = ""; dfs(digits, 0, str); return result; } void dfs(const string &digits, int depth, string &str) { if(depth == digits.length()) { result.push_back(str); return; } int num = digits[depth]-'0'; for(int j = 0; j < digitLetter[num].length(); j++) { str+=digitLetter[num][j]; dfs(digits, depth+1, str); str.erase(str.end()-1); } } void createMap() { digitLetter[2] = "abc"; digitLetter[3] = "def"; digitLetter[4] = "ghi"; digitLetter[5] = "jkl"; digitLetter[6] = "mno"; digitLetter[7] = "pqrs"; digitLetter[8] = "tuv"; digitLetter[9] = "wxyz"; }private: map<int,string> digitLetter; vector<string> result;};
0 0
- LeetCode: Letter Combinations of a Phone Number
- LeetCode Letter Combinations of a Phone Number
- LeetCode: Letter Combinations of a Phone Number
- [Leetcode] Letter Combinations of a Phone Number
- [LeetCode] Letter Combinations of a Phone Number
- LeetCode18:Letter Combinations of a Phone Number
- Letter Combinations of a Phone Number
- 【leetcode】Letter Combinations of a Phone Number
- LeetCode: Letter Combinations of a Phone Number
- Letter Combinations of a Phone Number
- Letter Combinations of a Phone Number
- [LeetCode]Letter Combinations of a Phone Number
- LeetCode-Letter Combinations of a Phone Number
- Letter Combinations of a Phone Number
- [LeetCode] Letter Combinations of a Phone Number
- Letter Combinations of a Phone Number
- LeetCode - Letter Combinations of a Phone Number
- LeetCode:Letter Combinations of a Phone Number
- java 创建WebSservice
- Openfire 中SASL的认证方式之:PLAIN,DIGEST-MD5,anonymous
- Android界面刷新
- python sqlserver 2
- Win7下Oracle11g完全删除
- Letter Combinations of a Phone Number
- ios开发笔记—get请求和post请求简单代码
- 搜索可以让你更深入地了解用户的语言习惯
- android开发步步为营之47:使用viewflipper实现屏幕的切换
- response.setContentType()的String参数及对应类型
- java垃圾回收机制
- 4Sum
- sql update批量替换指定字符笔记
- Textkit