[LeetCode] Letter Combinations of a Phone Number
2015-06-16 23:57
393 查看
Well, a typical backtracking problem. Make sure you are clear with the following three problems:
What is a partial solution and when is it finished? --- In this problem, the partial solution is a combination and it is finished once it is of the same length as digits.
How to find all the partial solutions? --- In the following code, I use a nested for-loop to traverse all the possible starting elements for each digit.
When to make recursive calls? --- In the following code, once an element is added to the partial solution, we call the function to generate the remaining elements recursively.
Of course, remember to recover to the previous status once a partial solution is done. In the following code, line 18 (comb.resize(comb.length() - 1)) is for this purpose.
The following should be self-explanatory :)
Well, the above recursive backtracking solution is a typical solution. In fact, this problem can also be solved iteratively. Refer to this link for more information. I have rewritten the code below for your reference.
What is a partial solution and when is it finished? --- In this problem, the partial solution is a combination and it is finished once it is of the same length as digits.
How to find all the partial solutions? --- In the following code, I use a nested for-loop to traverse all the possible starting elements for each digit.
When to make recursive calls? --- In the following code, once an element is added to the partial solution, we call the function to generate the remaining elements recursively.
Of course, remember to recover to the previous status once a partial solution is done. In the following code, line 18 (comb.resize(comb.length() - 1)) is for this purpose.
The following should be self-explanatory :)
class Solution { public: vector<string> letterCombinations(string digits) { string mp[] = {" ", " ", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; vector<string> res; if (digits.empty()) return res; string comb; combinations(digits, 0, mp, comb, res); return res; } private: void combinations(string& digits, int start, string mp[], string& comb, vector<string>& res) { if (comb.length() == digits.length()) { res.push_back(comb); return; } for (int i = 0; i < (int)mp[digits[start] - '0'].length(); i++) { comb += (mp[digits[start] - '0'][i]); combinations(digits, start + 1, mp, comb, res); comb.resize(comb.length() - 1); } } };
Well, the above recursive backtracking solution is a typical solution. In fact, this problem can also be solved iteratively. Refer to this link for more information. I have rewritten the code below for your reference.
class Solution { public: vector<string> letterCombinations(string digits) { vector<string> res; if (digits.empty()) return res; string mp[] = {" ", " ", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; res.push_back(""); for (int i = 0; i < (int)digits.length(); i++) { string letters = mp[digits[i] - '0']; vector<string> temp; for (int j = 0; j < (int)letters.length(); j++) for (int k = 0; k < (int)res.size(); k++) temp.push_back(res[k] + letters[j]); swap(res, temp); } return res; } };
相关文章推荐
- Android中如何查找内存泄露
- Java使用Fork/Join框架来并行执行任务
- 我的Java开发学习之旅------>使用循环递归算法把数组里数据数组合全部列出
- 无线相关资源链接
- 获取编辑器两种方法
- Ubuntu 安装 JDK 问题
- QT for Android 环境搭建测试成功!!!!
- testng教程之testng.xml的配置和使用,以及参数传递
- Cocos2d-x 3.x学习笔记(一):开始Cocos2d之旅
- mac版android studio如何使用SVN
- Maximum Gap
- 劣币驱逐良币
- 搜狐面试mark
- Android 获取手机总内存和可用内存等信息
- 从今天开始每天刷一题,并写在这里
- 从今天开始每天刷一题,并写在这里 分类: ACM 2015-06-16 23:52 14人阅读 评论(0) 收藏
- Android中基于Socket的网络通信
- Lua如何设置局部变量值进行调试
- 控制器和应用数据存储
- 关于longest 的一些题目