letter-combinations-of-a-phone-number
来源:互联网 发布:男生挂件知乎 编辑:程序博客网 时间:2024/06/03 08:41
给一个数字字符串,每个数字代表一个字母,请返回其所有可能的字母组合。
下图的手机按键图,就表示了每个数字可以代表的字母。
您在真实的面试中是否遇到过这个题?Yes
样例
给定 "23"
返回 ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]
class Solution {public: vector<string> letterCombinations(string& digits) { vector<string> res; if(digits.empty()) return res; vector<string>dic(10); dic[0]=" "; dic[1]="1"; dic[2]="abc"; dic[3]="def"; dic[4]="ghi"; dic[5]="jkl"; dic[6]="mno"; dic[7]="pqrs"; dic[8]="tuv"; dic[9]="wxyz"; return helper(digits,0,dic); } vector<string> helper(string& digits,int begin,vector<string>&dic){ vector<string>res; if(digits.empty()) return vector<string>(); if(begin==digits.size()) return vector<string>(); int digit=digits[begin]-'0'; string tmp=dic[digit]; int n=tmp.size(); for(int i=0;i<n;i++){ string ttmp; ttmp.push_back(tmp[i]); vector<string>next=helper(digits,begin+1,dic); int m=next.size(); if(m==0){ res.push_back(ttmp); }else{ for(int j=0;j<m;j++){ res.push_back(ttmp+next[j]); } } } return res; }};
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
- JBOSS+JPA的增删改查操作
- iOS exit函数深入浅出
- failed: Error during WebSocket handshake: Unexpected response code: 200
- Jinja2 example for generating a local file using a template
- 欢迎使用CSDN-markdown编辑器
- letter-combinations-of-a-phone-number
- Fedoa22 解决oracle 12c Could not initialize class sun.awt.X11.XToolkit
- iOS加载webview时出现内存泄露导致闪退
- Android中Textview显示带html文本二-------【Textview显示本地图片】
- Dynamics CRM 窗体Javascript获取时间字段值的准确转换
- Oracle 的sql优化提高性能
- Scalaz(10)- Monad:就是一种函数式编程模式-a design pattern
- initORA12C.ora 配置
- 大写标识符