Leetcode - Letter Combination Of A Phone Number
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.看到这个题目,我们第一部想到的就是要按照数字存储相对应的字母。这样的话,显然array是一个比较好的选择。根据测试, 数字1和0都是对应空 “”。
2.因为是不同的组合,所以我们要从不同数字的对应字母里分别找出一个字母进行组合,这个就和leetcode里combination的题目很相似,应该用recursion来解
解题思路如下:
比如说我们输入23:
那么按照我们的习惯, 我们从2中的abc选一个a 再到3中的def选一个d 好,完成ab,放入list里面, 返回
再到3中的def选一个e 好,完成ae,放入list里面 ,返回
再到3............f ...... af............ 返回,好了def里面我们已经结束了,返回到上一层的abc
我们从2中的abc选一个b
...............d .......bd............. 返回
..............以此类推..................
一直到我们遍历完abc
每当我们完成一个和数字组合相同的字符串 我们都存入list 并且return 并且返回到上层继续加入下一个属于同数字的字母
从上述我们可以看出recursion的逻辑
public List<String> letterCombinations(String digits) {
String[] letters = new String[]{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
List<String> result = new ArrayList<>();
if (digits.length() == 0) {
return result;
}
formCombination(result, "", letters, 0, digits);
return result;
}
void formCombination(List<String> result, String current, String[]letters, int index, String digits) {
if (index >= digits.length() && current.length() == digits.length()) {
result.add(current);
return;
}
int digit = Integer.parseInt(digits.substring(index, index + 1));
char[] arr = letters[digit].toCharArray();
for (int j = 0; j < arr.length; j++) {
formCombination(result, current + arr[j], letters, index + 1, digits);
}
}
Leetcode - Letter Combination Of A Phone Number的更多相关文章
- LeetCode: Letter Combinations of a Phone Number 解题报告
Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...
- [LeetCode]Letter Combinations of a Phone Number题解
Letter Combinations of a Phone Number: Given a digit string, return all possible letter combinations ...
- [LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- LeetCode——Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- [LeetCode] Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- [LeetCode] Letter Combinations of a Phone Number(bfs)
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- LeetCode Letter Combinations of a Phone Number (DFS)
题意 Given a digit string, return all possible letter combinations that the number could represent. A ...
- [LeetCode] Letter Combinations of a Phone Number 回溯
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- LeetCode Letter Combinations of a Phone Number 电话号码组合
题意:给一个电话号码,要求返回所有在手机上按键的组合,组合必须由键盘上号码的下方的字母组成. 思路:尼玛,一直RE,题意都不说0和1怎么办.DP解决. class Solution { public: ...
随机推荐
- window10(64bit)+VS2010编译ACE_TAO源码库
1.下载 ACE+TAO下载地址:http://download.dre.vanderbilt.edu/previous_versions/ VS2010下载地址:https://pan.baidu. ...
- win7下elasticsearch5.0 安装head插件
项目开发用到了ES,5.X版本的,然而网上好多的安装资料都不能用,全是之前的老版本,今天弄了一上午终于完事了,总结一下安装的步骤. 1.安装NodeJs 去官网https://nodejs.org/e ...
- 原生js简单实现双向数据绑定原理
根据对象的访问器属性去监听对象属性的变化,访问器属性不能直接在对象中设置,而必须通过 defineProperty() 方法单独定义. 访问器属性的"值"比较特殊,读取或设置访问器 ...
- request.setcharacterencoding()和request.setcontenttype(“html/css;charset”)的格式区别
1.request.setCharacterEncoding()是设置从request中取得的值或从数据库中取出的值 指定后可以通过getParameter()则直接获得正确的字符串,如果不指定,则默 ...
- Java字符串格式化记录
最近打log的时候用到了字符串的格式化. Java中String格式化和C语言的很类似.把情况都列出来,以后好查询. public static void main(String[] args) { ...
- 苹果APP发布
1 发布方式 苹果发布上架有两种方式,一种是上传到苹果商店,一种是挂在web服务器上扫描下载,下面分别介绍这两种发布方式 1.1 上传AppStore 1. 用公司账号或者个人开发账号生成上架.p12 ...
- 走进安卓的重灾区----video
html5的video已经出来很久了.在ios上使用基本上没什么毛病,但是安卓下就是一个重灾区了,各种体验差.这几天搞了安卓的兼容,简直是要吐血.所以特意总结了一些强势的坑点. 先看一下常用的一些属性 ...
- RobotFramwork安装报错name 'execfile' is not defined
安装RobotFramwork的时候,提示了这个?是什么原因呢? 本机装的是python3.6: 经官方回复得知识因为python的版本不兼容该模块的安装. 官方认定版本是2.7,所以这里推荐大家玩p ...
- HTML基础知识(未完待续)
一.HTML编辑工具:Sublime Text 二.HTML实体字符:1.( 空格): : 2.(<) <: 3.(>)>: 4.(&)&a ...
- Oracle的sessions和processes的数计算公式
Oracle的sessions和processes的数计算公式 原作者链接地址:http://blog.csdn.net/zengmuansha/article/details/7581771 Ora ...