[leetcode 17]Letter Combinations of a Phone Number
2015-09-02 16:00
651 查看
1 题目:
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.
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.
2 思路
好吧,想了半天,想不出来,参考别人的。
当时想着就是怎么每次循环,再一个字符串后面加一个字符。
看到别人的代码,处理的很好。
https://leetcode.com/discuss/24431/my-java-solution-with-fifo-queue
主要在ans.peek().length()==i、String t= ans.remove、链表上。
3 代码:
仿别人代码做的。
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.
2 思路
好吧,想了半天,想不出来,参考别人的。
当时想着就是怎么每次循环,再一个字符串后面加一个字符。
看到别人的代码,处理的很好。
https://leetcode.com/discuss/24431/my-java-solution-with-fifo-queue
主要在ans.peek().length()==i、String t= ans.remove、链表上。
3 代码:
仿别人代码做的。
public List<String> letterCombinations(String digits) { String[] maps = {" ","1","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; LinkedList<String> result = new LinkedList<String>(); if (digits.isEmpty()) { return result; } result.add(""); for (int i = 0; i < digits.length(); i++) { int index = Character.getNumericValue(digits.charAt(i)); String num = maps[index]; while (result.peek().length() == i) { String pre = result.removeFirst(); for (Character character : num.toCharArray()) { result.add(pre + character); } } } return result; }
相关文章推荐
- 由xib生成UIView的问题
- 数据库索引及基本优化入门
- Day02--笔记
- log4j2 与 spring mvc整合
- POJ 1125 Stockbroker Grapevine
- Ruby Rails在windows下开发环境的搭建
- HDU 4749-Parade Show(KMP变形)
- 关于处理字符串编码:escape、encodeURI、encodeURICompone
- Storm: monitor storm with supervisor
- 学习笔记 SQLite数据库应用
- 拷贝文件中有01.这样的行符号为新的文件
- MVC 上传下载
- 后缀数组 - poj3294 Life Forms
- 采用Tokyo Cabinet的一些问题
- 如何检索Android设备的唯一ID
- Google I/O 2015 为 Android 开发者带来了哪些福利?
- Build Your Programming Technical Skills
- Windows下mysql忘记root密码的解决方法
- [.Net MVC] 过滤器以及异常处理
- android退出程序