[LeetCode17]Letter Combinations of a Phone Number
来源:互联网 发布:国外类似于知乎的网站 编辑:程序博客网 时间:2024/05/16 01:50
Given 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"].
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.
The idea is to use recursion. DFS slove it.
Java
public class LetterCombinationPhoneNum {ArrayList<String> res;StringBuffer seq;public void getletterComba(String digits, int level, String[] trans){if(level == digits.length()){res.add(seq.toString());return;}int digit = digits.charAt(level)-48;for(int i=0;i<trans[digit].length();i++){seq.append(trans[digit].charAt(i));getletterComba(digits, level+1, trans);seq.deleteCharAt(level);}}public List<String> letterCombinations(String digits) {res = new ArrayList<>();seq = new StringBuffer(); String []trans = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; getletterComba(digits, 0, trans); return res; }}
c++
public: void getLetterCombination(string &seq, string &digits, int level, vector<string> &result,string trans[]){ if(level == digits.size()){ result.push_back(seq); return; } int group = digits[level] - 48; for(int i=0; i<trans[group].size(); i++){ seq.push_back(trans[group][i]); getLetterCombination(seq,digits,level+1,result,trans); seq.resize(seq.size()-1); }}vector<string> letterCombinations(string digits) { string trans[] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; vector<string> result; string seq; getLetterCombination(seq,digits,0,result,trans); return result;}
0 0
- [LeetCode17]Letter Combinations of a Phone Number
- LeetCode17:Letter Combinations of a Phone Number
- leetcode17 Letter Combinations of a Phone Number
- LeetCode17:Letter Combinations of a Phone Number
- leetcode17---Letter Combinations of a Phone Number
- LeetCode17. Letter Combinations of a Phone Number
- leetcode17:Letter Combinations of a Phone Number
- LeetCode17. Letter Combinations of a Phone Number
- leetcode17. Letter Combinations of a Phone Number
- leetcode17 Letter Combinations of a Phone Number
- [Java]LeetCode17 Letter Combinations of a Phone Number
- LeetCode17. Letter Combinations of a Phone Number可能是世界上最简明的解法了
- LeetCode17 17. Letter Combinations of a Phone Number My Submissions QuestionEditorial Solution
- LeetCode17 Letter Combinations of a Phone Number(Python and Java)
- LeetCode17——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
- nyoj 37 回文字符串 【DP】
- git 查看远程分支、本地分支、删除本地分支
- 码农札记--2014年5月
- php普通用户创建数据库失败
- 《二》CC2540和CC2541上实现ANCS(Apple Notification Center Service)
- [LeetCode17]Letter Combinations of a Phone Number
- GIT查看、删除、重命名远程分支和TAG
- Hadoop系列 之Terasort
- 配置虚拟主机跟主机通讯(断网情况下依旧通讯成功)
- nyoj 7 街区最短路径问题 【数学】
- 安卓圆角图片
- 超详细~ win7 下配置 Qt5.1 for Android
- s3c2440串口文档
- 协同过滤算法及其实现代码