17. Letter Combinations of a Phone Number(46.02%)
来源:互联网 发布:php判断时间范围 编辑:程序博客网 时间:2024/06/06 03:30
题目 :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”].
理解:输入数字字符串,输出数字键盘代表的字母的所有组合,一个数字可能代表多个字母,但是这些字母不能同时出现。用递归解决。唯一不太好的地方就是,数字键盘1表示语音拨号,它是没有代表任何一个字母的,这个我直接返回空集合了。
import java.util.ArrayList;import java.util.List;public class Solution { public List<String> letterCombinations(String digits) { ArrayList<String> finalStrings = new ArrayList<>(); if (digits == null || digits.equals("")) { return finalStrings; } char[] digitArray = digits.toCharArray(); indexArray(digitArray, 0, "", finalStrings); return finalStrings; } public void indexArray(char[] digitArray, int index, String tempSingle, ArrayList<String> finalArray) { if (index > digitArray.length-1) { return; } char[] characters = numForCharacters(digitArray[index]); if (characters != null) { if (index == digitArray.length-1) { for (char c : characters) { finalArray.add(tempSingle+c); } } else { for (char c : characters) { indexArray(digitArray, index+1, tempSingle+c, finalArray); } } } } public char[] numForCharacters(char num) { char[] characters = null; switch (num) { case '0': characters = new char[] {' '}; break; case '1': break; case '2': characters = new char[] {'a','b','c'}; break; case '3': characters = new char[] {'d','e','f'}; break; case '4': characters = new char[] {'g','h','i'}; break; case '5': characters = new char[] {'j','k','l'}; break; case '6': characters = new char[] {'m','n','o'}; break; case '7': characters = new char[] {'p','q','r','s'}; break; case '8': characters = new char[] {'t','u','v'}; break; case '9': characters = new char[] {'w','x','y','z'}; break; default: break; } return characters; }}
0 0
- 17. Letter Combinations of a Phone Number(46.02%)
- 17.Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- Sybase backup and restore
- Android IntentService完全解析 当Service遇到Handler
- Android开发中adb启动失败adb连接异常的解决办法
- mongodb数据结构-树
- Qt组件之文本框类
- 17. Letter Combinations of a Phone Number(46.02%)
- 史上最全远程推送详解之极光推送
- jar包安装到本地库 maven
- RAID基础知识
- Dao批处理数据库
- C# 浅拷贝与深拷贝区别
- SQL注入攻击
- hiho 1 最长回文子串
- JAVA反射机制