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 ...
随机推荐
- 使用 Python 在 Linux 上实现一键回归测试
从代码库迁出代码 —- pexpect 的使用 测试人员从代码库(例如 CVS )迁出代码的过程中,需要手动输入访问密码,而 Python 提供了 Pexpect 模块则能够将手动输入密码这一过程自动 ...
- 【BZOJ 4832 】 4832: [Lydsy2017年4月月赛]抵制克苏恩 (期望DP)
4832: [Lydsy2017年4月月赛]抵制克苏恩 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 275 Solved: 87 Descripti ...
- php版本CKEditor 4和CKFinder安装及配置
下载并解压CKEditor 4和CKFinder CKEditor 4下载地址:https://ckeditor.com/cke4/builder,选择自定义的版本,记得加上中文语言包 CKFinde ...
- Linux 系统及编程相关知识总汇
Linux C function() 参考手册 STL 学习文档 Linux内核
- 10分钟上手python pandas
目录 Environment 开始 对象创建 查看数据 选择 直接选择 按标签选择 按位置选择 布尔索引 设置 缺失数据 操作 统计 应用(apply) 直方图化(Histogramming) 字符串 ...
- java并发基础(五)--- 线程池的使用
第8章介绍的是线程池的使用,直接进入正题. 一.线程饥饿死锁和饱和策略 1.线程饥饿死锁 在线程池中,如果任务依赖其他任务,那么可能产生死锁.举个极端的例子,在单线程的Executor中,如果一个任务 ...
- 如何让xcode自动检查内存泄露
在project-setting中找到 “Run Static Analyzer” 键,然后把值修改为“YES”.这样在编码的时候,xcode就可以自动为我们检查内存泄露了. 原图片:http://b ...
- C#编程(十五)----------只读字段
只读字段 当字段声明中含有 readonly 修饰符时,该声明所引入的字段为只读字段.给只读字段的直接赋值只能作为声明的组成部分出现,或在同一类中的实例构造函数或静态构造函数中出现.(在这些上下文中, ...
- 疑犯追踪第五季/全集Person of Interest迅雷下载
英文全名Person of Interest,第5季(2015)CBS.本季看点:<疑犯追踪>本季剧组暗示Finch可能重建机器,这次他会给机器更多自由(如Root一直要求的那样).或许新 ...
- Wireshark的简介
-------------------------------------------------------------- <Wireshark数据包分析实战>这本书其实还很不错,当时买 ...