LeetCode算法题之Letter Combinations of a Phone Number
2015-03-16 16:24
423 查看
问题描述:
手机键盘上的数字对应着若干个字母,输入一组由数字组成的字符串,对应输出相应的字母的所有可能组合,顺序不限
解题思路:
暴力破解可以得到结果
相当于遍历一棵树,可以使用树的遍历算法DFS
手机键盘上的数字对应着若干个字母,输入一组由数字组成的字符串,对应输出相应的字母的所有可能组合,顺序不限
解题思路:
暴力破解可以得到结果
相当于遍历一棵树,可以使用树的遍历算法DFS
25 / 25 test cases passed. | |
Runtime: 3 ms |
vector<string> numbers_alphabet; class Solution { public: vector<string> DFS(int index, vector<string> result) { string temp = numbers_alphabet[index]; vector<string> tempRes; for(unsigned int i=0; i< temp.length(); ++i) { for(size_t n=0;n< result.size(); ++n) { string newStr = " "; newStr = result + temp[i]; tempRes.push_back(newStr); } } result.clear(); result = tempRes; tempRes.clear(); return result; } vector<string> letterCombinations(string digits) { numbers_alphabet.push_back(""); numbers_alphabet.push_back(""); numbers_alphabet.push_back("abc"); numbers_alphabet.push_back("def"); numbers_alphabet.push_back("ghi"); numbers_alphabet.push_back("jkl"); numbers_alphabet.push_back("mno"); numbers_alphabet.push_back("pqrs"); numbers_alphabet.push_back("tuv"); numbers_alphabet.push_back("wxyz"); vector<string> result; if(digits.empty()){ return result; } string initRes = numbers_alphabet[(int)(digits[0]-'0')]; for(unsigned int i = 0; i<initRes.length(); ++i) { string t = ""; result.push_back(t+initRes[i]); } for(unsigned int j=1; j<digits.length(); ++j) { result = DFS((int)(digits[j]-'0'),result); } return result; } };
相关文章推荐
- 【LeetCode-面试算法经典-Java实现】【017-Letter Combinations of a Phone Number (电话号码上的单词组合)】
- LeetCode力扣之Letter Combinations of a Phone Number
- [leetCode 解题报告]080. Letter Combinations of a Phone Number
- [LeetCode] Letter Combinations of a Phone Number
- leetcode——Letter Combinations of a Phone Number 手机按键字母组合(AC)
- [LeetCode] 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解题报告
- leetcode 题解 || Letter Combinations of a Phone Number 问题
- LeetCode 17 - Letter Combinations of a Phone Number
- LeetCode17——Letter Combinations of a Phone Number(手机几个按键对应的字母(符号)组合)
- 算法分析与设计——LeetCode Problem.17 Letter Combinations of a Phone Number
- LeetCode 17 Letter Combinations of a Phone Number 递归与DFS的使用
- LeetCode 17 Letter Combinations of a Phone Number 递归与DFS的使用
- [LeetCode] Letter Combinations of a Phone Number 解题报告
- leetcode -- Letter Combinations of a Phone Number -- 重点
- LeetCode 17 Letter Combinations of a Phone Number
- [LeetCode] Letter Combinations of a Phone Number 解题报告