Letter Combinations of a Phone Number leetcode java
题目:
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.
题解:
这道题也是来算一种combination的题,跟 Combinations 和 Word Break II 都比较类似(其实和leetcode里面很多题都相似)。
代码如下:
1 public ArrayList<String> letterCombinations(String digits) {
2 ArrayList<String> result=new ArrayList<String>();
3 if (digits==null)
4 return result;
5
6 String[] keyboard={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
7 StringBuilder current=new StringBuilder();
8
9 int index=0;
buildResult(digits, index, current, keyboard, result);
return result;
}
private void buildResult(String digits, int index, StringBuilder current, String[] keyboard, ArrayList<String> result){
if (index==digits.length()){
result.add(current.toString());
return;
}
int num=digits.charAt(index)-'0';//get integer number
for (int i=0; i<keyboard[num].length(); i++){
current.append(keyboard[num].charAt(i));
buildResult(digits, index+1, current, keyboard, result);
current.deleteCharAt(current.length()-1);
}
}
Refrence:http://rleetcode.blogspot.com/2014/02/letter-combinations-of-phone-number-java.html
Letter Combinations of a Phone Number leetcode java的更多相关文章
- Letter Combinations of a Phone Number - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Letter Combinations of a Phone Number - LeetCode 注意点 可以不用按字典序排序 解法 解法一:输入的数字逐 ...
- Letter Combinations of a Phone Number——LeetCode
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- LeetCode解题报告—— Container With Most Water & 3Sum Closest & Letter Combinations of a Phone Number
1. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a ...
- [LeetCode][Python]17: Letter Combinations of a Phone Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...
- LeetCode: Letter Combinations of a Phone Number 解题报告
Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...
- Leetcode之回溯法专题-17. 电话号码的字母组合(Letter Combinations of a Phone Number)
[Leetcode]17. 电话号码的字母组合(Letter Combinations of a Phone Number) 题目描述: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组 ...
- 【leetcode】Letter Combinations of a Phone Number
Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...
- 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- [LeetCode]Letter Combinations of a Phone Number题解
Letter Combinations of a Phone Number: Given a digit string, return all possible letter combinations ...
随机推荐
- codevs 2291 糖果堆
题目描述 Description [Shadow 1]第一题 WJMZBMR买了很多糖果,分成了N堆,排成一列.WJMZBMR说,如果Shadow能迅速求出第L堆到第R堆一共有多少糖果,就把这些糖果都 ...
- [Android]对BaseAdapter中ViewHolder编写简化(转)
来自博客:http://www.cnblogs.com/tiantianbyconan/p/3642849.html 在Android项目中,经常都会用到ListView这个控件,而相应的Adapte ...
- URAL 1963 Kite 计算几何
Kite 题目连接: http://acm.hust.edu.cn/vjudge/contest/123332#problem/C Description Vova bought a kite con ...
- QThreadPool线程池的使用,线程与Widget通过信号与槽的方式通信。
因为QRunnable类并非继承自QObject,不能使用信号和槽,为了能够使用信号与槽和Widget通信,需要对QRunnable进行封装. 定义一个类QMyRunnable,该类首先继承自QObj ...
- FireDAC 下的 Sqlite [12] - 备忘录(草草结束这个话题了)
该话题的继续延伸主要就是 SQL 的语法了, 草草收场的原因是现在的脑筋已经进入了 IntraWeb 的世界. 相关备忘会随时补充在下面: //连接多个数据库的参考代码: FDConnection1. ...
- JavaScript获取事件对象和目标对象
在JavaScript开发中,经常需要获取触发某个事件的目标对象.让后根据目标对象进行不同的业务处理.下面展示通过JavaScript获取触发事件的事件目标对象.如下: Js代码 1 2 3 4 5 ...
- 用ViewPager实现一个程序引导界面
下面使用ViewPager来实现一个程序引导的demo: 一般来说,引导界面是出现第一次运行时出现的,之后不会再出现.所以需要记录是否是第一次使用程序,办法有很多,最容易想到的就是使用SharedPr ...
- C++11 function
#include <iostream> #include <functional> #include <vector> using namespace std; / ...
- HDU 4081 Qin Shi Huang's National Road System(最小生成树/次小生成树)
题目链接:传送门 题意: 有n坐城市,知道每坐城市的坐标和人口.如今要在全部城市之间修路,保证每一个城市都能相连,而且保证A/B 最大.全部路径的花费和最小,A是某条路i两端城市人口的和,B表示除路i ...
- utf-8与utf-8(无BOM)的区别
BOM——Byte Order Mark,就是字节序标记 在UCS 编码中有一个叫做"ZERO WIDTH NO-BREAK SPACE"的字符,它的编码是FEFF.而FFFE ...