Letter Combinations of a Phone Number
2015-06-14 22:11
302 查看
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.
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; } };
相关文章推荐
- Letter Combinations of a Phone Number
- libsvm matlab使用总结
- HDU 迷瘴 (贪心)
- 硬盘那点事儿
- ARM基础系列——ARM指令集
- java学习笔记(六):关于iterator.next()
- 什么是垃圾回收(转)
- xcode6下使用autolayout+sizeclass实践
- 路径规划算法学习网址
- Access Toke调用受保护的API
- HDU 2191 珍惜现在,感恩生活 (多重背包)
- Java基础之基础语法
- 关于Android四大组件之一Service
- 面向对象程序设计
- vdbench使用
- iOS开发--Objective-C之KVC
- 主从复制
- CMarkup类中的方法说明
- Require.js、Angular.js整合
- HDU 悼念512汶川大地震遇难同胞——老人是真饿了 (贪心)