【Leetcode】Letter Combinations of a Phone Number算法解析
来源:互联网 发布:李兴华java实战经典 编辑:程序博客网 时间:2024/05/29 02:37
用栈的思想。
第一种方法,设置两个string栈,每次从栈A取出string,加上一个char后放入栈B(注意放入栈B的是多个string,因为加char有超过一种加法例如数字1对应可以加'a'或'b'或'c'),直到栈A为空。栈A为空后把栈A和栈B swap一下。这样下一次循环还是从栈A去,往栈B放。
共循环n次,n是phone number的长度。
代码如下:
class Solution {public: vector<string> letterCombinations(string digits) { vector<string> stack1, stack2; int max = digits.size(); stack1.push_back(string("")); for(int i=0; i<max; i++) { while(!stack1.empty()) { string top = stack1.back(); stack1.pop_back(); char digit = digits[i]; switch (digit) { case '2': stack2.push_back(top + 'a'); stack2.push_back(top + 'b'); stack2.push_back(top + 'c'); break; case '3': stack2.push_back(top + 'd'); stack2.push_back(top + 'e'); stack2.push_back(top + 'f'); break; case '4': stack2.push_back(top + 'g'); stack2.push_back(top + 'h'); stack2.push_back(top + 'i'); break; case '5': stack2.push_back(top + 'j'); stack2.push_back(top + 'k'); stack2.push_back(top + 'l'); break; case '6': stack2.push_back(top + 'm'); stack2.push_back(top + 'n'); stack2.push_back(top + 'o'); break; case '7': stack2.push_back(top + 'p'); stack2.push_back(top + 'q'); stack2.push_back(top + 'r'); stack2.push_back(top + 's'); break; case '8': stack2.push_back(top + 't'); stack2.push_back(top + 'u'); stack2.push_back(top + 'v'); break; case '9': stack2.push_back(top + 'w'); stack2.push_back(top + 'x'); stack2.push_back(top + 'y'); stack2.push_back(top + 'z'); break; } } swap(stack1, stack2); } return stack1; }};
第二种方法只用一个栈,即从这个栈取也往这个栈里放,取出来以后看这个string的长度,决定往里添加哪个digit对应的letter。例如输入数字是9893654,取出来一个string是sah,即长度为3,说明需要添加第4个digit对应的letter,即9893654中的3对应的letter。
但是要对输入的phone number进行一些处理——先在phone number里去掉0和1,因为这两个数没有对应任何字母。
代码如下:
class Solution {public: vector<string> letterCombinations(string digits) { vector<string> res; vector<string> stack; string::iterator pend = remove(digits.begin(), digits.end(), '0'); pend = remove(digits.begin(), pend, '1'); // 输入的digits中去除0和1 int max = pend - digits.begin(); stack.push_back(string("")); while(!stack.empty()) { string top = stack.back(); stack.pop_back(); int len = top.size(); if(len == max) { res.push_back(top); continue; } char digit = digits[len]; switch (digit) { case '2': stack.push_back(top + 'a'); stack.push_back(top + 'b'); stack.push_back(top + 'c'); break; case '3': stack.push_back(top + 'd'); stack.push_back(top + 'e'); stack.push_back(top + 'f'); break; case '4': stack.push_back(top + 'g'); stack.push_back(top + 'h'); stack.push_back(top + 'i'); break; case '5': stack.push_back(top + 'j'); stack.push_back(top + 'k'); stack.push_back(top + 'l'); break; case '6': stack.push_back(top + 'm'); stack.push_back(top + 'n'); stack.push_back(top + 'o'); break; case '7': stack.push_back(top + 'p'); stack.push_back(top + 'q'); stack.push_back(top + 'r'); stack.push_back(top + 's'); break; case '8': stack.push_back(top + 't'); stack.push_back(top + 'u'); stack.push_back(top + 'v'); break; case '9': stack.push_back(top + 'w'); stack.push_back(top + 'x'); stack.push_back(top + 'y'); stack.push_back(top + 'z'); break; } } return res; }};
0 0
- 【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
- 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
- Fedora14 64位环境下minicom使用方法
- C#中关于字符串和类型转换的一些注意点
- 使用Maven下载Spring
- php-screw php代码加密工具用法(整理)
- Java中abstract和interface的区别
- 【Leetcode】Letter Combinations of a Phone Number算法解析
- 第16周项目2--用指针玩字符串(示例)
- 《Effective C++》学习笔记——条款20
- 鹅鹅鹅
- storm解除twitter4jar包依赖的方法
- asp.net弹出层扩展研究
- ios绘图教程(原文http://www.cocoachina.com/industry/20140115/7703.html)
- Unity学习之查询基础和连接数据库
- 1412101621-hd-排名