[LeetCode][Java] Letter Combinations of a Phone Number
来源:互联网 发布:淘宝发布宝贝没有项 编辑:程序博客网 时间:2024/04/28 03:49
题目:
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.
题意:
给定一个数字字符串,返回这个数字能代表的所有的字母的组合。
数字对字母的映射如下图所示(就在电话的按键中)
Input:Digit string "23"Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].返回的结果可以按照任意的顺序。
算法分析:
* 思路:
*比如“234”这个字符串,我可以先将0...1的所有排列找到-->{"a", "b", "c"}
*再进一步将0...2的所有排列找到-->{"ad", "ae","af", "bd", "be", "bf", "cd", "ce", "cf"}
*如此循环...直到字符串末尾。实现如下
AC代码:
public class Solution { public ArrayList<String> letterCombinations(String digits) { ArrayList<String> res=new ArrayList<String>(); String charmap[] = {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; if (digits.length()==0) return res; res.add(""); for (int i = 0; i < digits.length(); i++) { ArrayList<String> tempres=new ArrayList<String>(); String chars = charmap[digits.charAt(i) - '0']; for (int c = 0; c < chars.length();c++) { for (int j = 0; j < res.size();j++) tempres.add(res.get(j)+chars.charAt(c)); } res = tempres; } 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
- 【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
- 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
- 【leetcode】Letter Combinations of a Phone Number
- [LeetCode] Letter Combinations of a Phone Number
- leetcode: Letter Combinations of a Phone Number
- 怎样合理地定义用户流失
- 求任意多边形的面积--差称法
- Scala语法要点
- 关于LiseView ScrollView 问题
- JAVA判断字符串是否为数字
- [LeetCode][Java] Letter Combinations of a Phone Number
- java.lang.NoClassDefFoundError: org.hibernate.cfg.Configuration.setXXXX解决方法
- vs2010自动生成的文件解析
- Linux建立文件目录链接的方法
- 输出字符串与数字时的小差异
- 文章标题
- 输入字符串反序输出
- 小胖学PHP总结3-----PHP的循环语句
- poj 1195 Mobile phones (二维树状数组)