Letter Combinations of a Phone Number
来源:互联网 发布:我的淘宝展现词是0个 编辑:程序博客网 时间:2024/06/09 16:54
iven a digit string, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below.
Input:Digit string "23"Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
对“0”的情况特殊处理;对digits对应的字符打表;然后递归枚举求解
class Solution {public: vector<char> temp; vector< vector<char> > ans; string s[9] = {"","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; void get_str(string digits, int index, int count) { if(temp.size() == count) { ans.push_back(temp); return; } if(index >= count) return; int i ,j; for(i = 0;i<s[digits[index]-'0'-1].length();i++) { temp.push_back(s[digits[index]-'0'-1][i]); get_str(digits,index+1,count); temp.pop_back(); } } vector< string > letterCombinations(string digits) { int i,j = 0; string num; vector< string > ans1; int len = 0; if(digits == "") return ans1; for(i = 0;i<digits.length();i++) { if(digits[i] != '0') { num= num +digits[i]; len++; } } get_str(num,0,len); for(i = 0;i<ans.size();i++) { string temp1; for(j = 0;j<ans[i].size();j++) temp1 = temp1 + ans[i][j]; ans1.push_back(temp1); } return ans1; }};
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
- LeetCode18:Letter Combinations of a Phone Number
- 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
- 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] Letter Combinations of a Phone Number
- Letter Combinations of a Phone Number
- LeetCode - Letter Combinations of a Phone Number
- LeetCode:Letter Combinations of a Phone Number
- C#属性和索引器
- ARM基础系列——ARM指令集
- HDU 迷瘴 (贪心)
- OpenCV Tutorial CVPR 2015
- C语言中关于%d的使用方法
- Letter Combinations of a Phone Number
- libsvm matlab使用总结
- New XAMPP security concept
- spirng核心接口
- 黑马程序员<Java笔记<GUI 网络编程反射>>
- WebService
- LeakCanary开源项目(使用及原理github项目文档的翻译)使用LeakCanary检测安卓中的内存泄漏(实战)
- Struts2----带参数转发到JSP
- YTU 2897: E--外星人供给站(贪心)