leetcode 17 Letter Combinations of a Phone Number
来源:互联网 发布:aes解密算法 编辑:程序博客网 时间:2024/04/30 12:17
问题
https://leetcode.com/problems/letter-combinations-of-a-phone-number/
解法
回溯法, 递归
class Solution {public: void lc(vector<string>& ret, char* s, int sCnt, const char* digits, int depth) { if (digits[depth] == 0) { if (sCnt== 0) return; s[sCnt] = 0; string now(s); ret.push_back(now); return; } if (digits[depth] < '2') { lc(ret, s, sCnt, digits, depth+1); return; } char start, end; switch(digits[depth]) { case '7': start = 'p'; end = 't'; break; case '8': start = 't'; end = 'w'; break; case '9': start = 'w'; end = 'z'+1; break; default: start = (digits[depth]-'2') * 3+ 'a'; end = start +3; break; } for (char c = start; c<end; c++) { s[sCnt] = c; lc(ret, s, sCnt+1, digits, depth+1); } } vector<string> letterCombinations(string digits) { char s[digits.size()+1]; vector<string> ret; lc(ret, s, 0, digits.c_str(), 0); return ret; }};
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
- 【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
- 第三次实验,本月有几天
- C++作业3——本月有几天、定期存款利息计算器、多分数段函数求值
- MATLAB读取文件夹下所有图片
- 代理模式(java语言实现)
- liunx centos 如何添加一个新的网卡
- leetcode 17 Letter Combinations of a Phone Number
- 什么是面向对象 什么是面向过程 面向对象和面向过程的区别
- Some Advices
- 01背包问题:初入动规坑
- iOS---选中cell时变cell颜色
- Android之值得开发者(安卓)必备的十八大开发资源
- 第七周上机实践项目——项目2-友元类
- 使用XFire+Spring构建Web Service(一)——helloWorld篇
- C 开发学习中的问题