Letter combinations of a phone number
来源:互联网 发布:修改电脑mac地址 编辑:程序博客网 时间:2024/06/05 15:44
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.
分析:
典型的 DFS
class Solution {public: vector<string> letterCombinations(string digits) { vector<string> dict(10), res; dict[2] = "abc"; dict[3] = "def"; dict[4] = "ghi"; dict[5] = "jkl"; dict[6] = "mno"; dict[7] = "pqrs"; dict[8] = "tuv"; dict[9] = "wxyz"; string path; DFS(0, digits, path, dict, res); return res; }private: void DFS(int cur, string& digits, string& path, vector<string> &dict, vector<string>&res) { // exit if(cur == digits.size()){ res.push_back(path); return; } int index = digits[cur] - '0'; for(int i=0; i<dict[index].size(); ++i) { path.push_back(dict[index][i]); DFS(cur+1, digits, path, dict, res); path.pop_back(); } }};
1 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
- IPv4地址(二)
- rhel6.4 简单创建自己的http web 界面服务遇到的问题和解决方法
- VS2010 更改MFC标题栏图标和生成的执行文件图标
- 简明python教程学习笔记(二)
- 2014-04-02第14周周三工作日志: error C2377: 'BOOL' : redefinition; typedef cannot be overloaded with any othe
- Letter combinations of a phone number
- 小菜准备约会—装饰模式
- onvif规范的实现:onvif开发常用调试方法 和常见的segmentation fault错误
- Linux对NUMA技术的支持。
- 企业服务总线(ESB)
- sql server常用操作
- Qt中int转换成QString
- 发动机转速和曲轴位置传感器作用、安装及类型
- 黑苦荞茶真的有那么好吗