LeetCode 17: Letter Combinations of a Phone Number
来源:互联网 发布:sql left join where 编辑:程序博客网 时间:2024/06/05 04:24
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.
- 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
- 【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
- 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
- 【leetcode】Letter Combinations of a Phone Number
- [LeetCode] Letter Combinations of a Phone Number
- leetcode: Letter Combinations of a Phone Number
- 修改一行SQL代码 性能提升了100倍
- 黑马程序员——.NET笔记9 面向对象(第二季教程)
- 阿里巴巴一道算法题
- 黑马程序员——.NET笔记10 面向对象版聊天机器人
- hadoop在CentOS上的安装部署
- LeetCode 17: Letter Combinations of a Phone Number
- arm-linux-gcc
- Windows和Linux下的Sleep()函数小结
- 黑马程序员————高新技术————类加载器
- POJ-3345 Bribing FIPA(Tree dp + 背包)
- who命令的编写——登录与注销__who, whoami, who am i
- hdu 3486 Interviewe 二分+RMQ
- ZOJ Problem Set - 1061
- 链表操作(从尾到头打印链表,使用栈结构实现)