Letter Combinations of a Phone Number
来源:互联网 发布:小米5刷机端口被关闭 编辑:程序博客网 时间:2024/06/06 02:02
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.
本题速度差别主要体现在建立字典地方法,下边是两种方法:第一种beats 50%,第二种beats 8%。
public class Solution { public List<String> letterCombinations(String digits) { List<String> result = new ArrayList<String>(); String[] map = new String[] { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" }; char[] tmp = new char[digits.length()]; if(digits.length() == 0) return result; rec(digits, 0, tmp, map, result); return result; } public void rec(String digits, int index, char[] tmp, String[] map, List<String> result){ if(index == digits.length()){ result.add(new String(tmp)); return; } char tmpChar = digits.charAt(index); for(int i = 0; i < map[tmpChar - '0'].length(); i++){ tmp[index] = map[tmpChar - '0'].charAt(i); rec(digits, index + 1, tmp, map, result); } }}
public class Solution { public List<String> letterCombinations(String digits) { ArrayList<String> result=new ArrayList<>();if(digits.length()==0)return result;HashMap<Integer, List<Character>> hashMap=new HashMap<>();for(int i=2;i<10;i++){ArrayList<Character> tem=new ArrayList<>();if(i==7){tem.add('p');tem.add('q');tem.add('r');tem.add('s');}else if (i==8) {tem.add('t');tem.add('u');tem.add('v');}else if (i==9) {tem.add('w');tem.add('x');tem.add('y');tem.add('z');}else {for(int j=0;j<3;j++){tem.add((char)((i-2)*3+'a'+j));}}hashMap.put(i, tem);}doFor(hashMap, result, new StringBuilder(), digits, 0); return result; } void doFor(Map<Integer, List<Character>> map,List<String> list,StringBuilder builder,String string,int i){for(char c:map.get(string.charAt(i)-'0')){builder.append(c);if(i==string.length()-1){list.add(builder.toString());}else {doFor(map, list, builder, string, i+1);}builder.setLength(builder.length()-1);} }}
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
- 常见错误,记录下
- CSS3 背景 background-size
- [模板]spfa+邻接表
- 深入理解C++11 第二章兼容性稳定性 学习笔记
- iOS 断点调试小技巧
- Letter Combinations of a Phone Number
- 计算机基础二之计算机文化与计算机思维基础
- Spring读取配置文件
- 2016-8-19 封装一套基础框架,方便开发Spring+SpringMVC+MyBatis+BootStarp
- @RequestMapping 参数详解
- Android使用Gradle命令动态传参完成打包,不需要修改代码
- 微信开发功能
- TCP/IP四层模型
- C++ 数据处理(二)---进制、数据类型