LeetCode (17)Letter Combinations of a Phone Number
来源:互联网 发布:程序员联合网 编辑:程序博客网 时间:2024/06/05 11:29
(17)Letter Combinations of a Phone Number
题目:通过所给字符串,返回按下数字可能返回的字符串组合,数字和字符的关系如同手机键盘一样。
例子:
输入:字符串为“23”。输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]。
首先想到的就是队列,不停地通过队列的每一组字符串相加获得下一组字符串,然后再把下一组字符串压入队列中,这样能够实现整个字符串的所有可能性的罗列。
当然在C++中vector有着类似队列和栈的功能,每一次拿出v.back()做基础字符串,然后v.pop_back(),直到v.empty(),将做好的新字符串放到v_temp中,最后v=v_temp,慢慢将所有的字符串处理完成就可以了。
下面是代码:
class Solution {public: vector<string> letterCombinations(string digits) { int len_str = digits.size(); char c; string word[10] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; int i,j; vector<string> s; int len = 0; for(i = 0; i < len_str; i ++){ c = digits[i]; if(c == '1'){ } else if(c == '0'){ vector<string> temp_v; if(len == 0){ temp_v.push_back(" "); s = temp_v; len ++; } else{ while(!s.empty()){ string str_temp = ""; str_temp += s.back(); s.pop_back(); str_temp += " "; temp_v.push_back(str_temp); } s = temp_v; } } else if(c == '2'||c == '3'||c == '4'||c == '5'||c == '6'||c == '8'){ vector<string> temp_v; if(len == 0){ string str_temp = ""; string s1 = str_temp; s1.append(word[c - '0'].substr(0,1)); temp_v.push_back(s1); string s2 = str_temp; s2.append(word[c - '0'].substr(1,1)); temp_v.push_back(s2); string s3 = str_temp; s3.append(word[c - '0'].substr(2,1)); temp_v.push_back(s3); s = temp_v; len ++; } else{ while(!s.empty()){ string str_temp = ""; str_temp += s.back(); s.pop_back(); string s1 = str_temp; s1.append(word[c - '0'].substr(0,1)); temp_v.push_back(s1); string s2 = str_temp; s2.append(word[c - '0'].substr(1,1)); temp_v.push_back(s2); string s3 = str_temp; s3.append(word[c - '0'].substr(2,1)); temp_v.push_back(s3); } s = temp_v; } } else if(c == '7'||c == '9'){ vector<string> temp_v; if(len == 0){ string str_temp = ""; string s1 = str_temp; s1.append(word[c - '0'].substr(0,1)); temp_v.push_back(s1); string s2 = str_temp; s2.append(word[c - '0'].substr(1,1)); temp_v.push_back(s2); string s3 = str_temp; s3.append(word[c - '0'].substr(2,1)); temp_v.push_back(s3); string s4 = str_temp; s4.append(word[c - '0'].substr(3,1)); temp_v.push_back(s4); s = temp_v; len ++; } else{ while(!s.empty()){ string str_temp = ""; str_temp += s.back(); s.pop_back(); string s1 = str_temp; s1.append(word[c - '0'].substr(0,1)); temp_v.push_back(s1); string s2 = str_temp; s2.append(word[c - '0'].substr(1,1)); temp_v.push_back(s2); string s3 = str_temp; s3.append(word[c - '0'].substr(2,1)); temp_v.push_back(s3); string s4 = str_temp; s4.append(word[c - '0'].substr(3,1)); temp_v.push_back(s4); } s = temp_v; } } } return s; }};
0 0
- LeetCode (17)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
- 【leetcode】Letter Combinations of a Phone Number
- js控制文字一个一个的显示出来
- C++11—迭代器类实现
- 【矩阵快速幂】 hdu5667 Sequence 、nyoj1000 又见斐波那契数列
- spring boot整合security
- [积跬成千]wpf基础之Visibility与Opacity的区别
- LeetCode (17)Letter Combinations of a Phone Number
- C语言中static用法
- 数字图像处理]常见噪声的分类与Matlab实现
- fileURLWithPath与 URLWithString的区别
- 想要提高网站的访问量 就先要仔细分析网站的kpi
- 面试感悟----一名3年工作经验的程序员应该具备的技能
- 【模拟】洛谷 P1000 超级玛丽游戏
- 给图片添加蒙版
- js实现添加、删除ul行