CODE 113: Letter Combinations of a Phone Number
来源:互联网 发布:淘宝店铺怎么解封 编辑:程序博客网 时间:2024/05/02 09:39
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.
static char[][] cdigits = { { ' ' }, {}, { 'a', 'b', 'c' },{ 'd', 'e', 'f' }, { 'g', 'h', 'i' }, { 'j', 'k', 'l' },{ 'm', 'n', 'o' }, { 'p', 'q', 'r', 's' }, { 't', 'u', 'v' },{ 'w', 'x', 'y', 'z' } };public ArrayList<String> letterCombinations(String digits) {// Start typing your Java solution below// DO NOT write main() functionif (null == digits || "".equals(digits)) {ArrayList<String> results = new ArrayList<String>();String str = "";results.add(str);return results;}return dfs(digits, 0);}ArrayList<String> dfs(String digits, int i) {if (i >= digits.length()) {return new ArrayList<String>();}int a = Character.digit(digits.charAt(i), 10);if (1 == a) {return dfs(digits, i + 1);}ArrayList<String> results = new ArrayList<String>();for (int j = 0; j < cdigits[a].length; j++) {char c = cdigits[a][j];ArrayList<String> tmps = dfs(digits, i + 1);if (tmps.isEmpty()) {results.add(new StringBuilder().append(c).toString());} else {for (String str : tmps) {str = c + str;results.add(new String(str));}}}return results;}
- CODE 113: Letter Combinations of a Phone Number
- [leet code] Letter Combinations of a Phone Number
- leetcode 日经贴,Cpp code -Letter Combinations of a Phone Number
- Leet Code 17 Letter Combinations of a Phone Number
- Leet Code OJ 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
- 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
- printk使用方法
- traits编程技巧
- ExpandableListView简单用法
- 移除Remove Microsoft Word HTML Tag
- fzu 1894 志愿者选拔(单调队列)
- CODE 113: Letter Combinations of a Phone Number
- static在C和C++中的用法和区别
- 在Window 7 下 安装 window xp
- 23.11.06
- 关于 Linux 的配置文件 /etc/profile 路径出错后相关的命令失效解决方式(如:ls,vi不能用)
- 淘宝搜索算法综述之一:淘宝搜索的特点
- 动态链接库与静态链接库的区别
- 【LTE基础知识】GUTI(Globally Unique Temporary UE Identity)分配
- 设计模式学习_代理模式