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: ...
随机推荐
- 使用jenkins实现持续集成
一.jenkins 介绍 它是一个自动化的周期性的集成测试过程,从检出代码.编译构建.运行测试.结果记录.测试统计等都是自动完成的,无需人工干预: 它需要有专门的集成服务器来执行集成构建: 它需要有代 ...
- tp框架表单验证 及ajax
之前的表单验证都是用js写的,这里也可以使用tp框架的验证.但是两者比较而言还是js验证比较好,因为tp框架验证会运行后台代码,这样运行速度和效率就会下降. 自动验证是ThinkPHP模型层提供的一种 ...
- 玲珑杯 Round #11 (1001 1004 1007)
比赛链接 直接贴代码.. #include<bits/stdc++.h> using namespace std; typedef long long LL; int main() { L ...
- documentsUI源码分析
documentsUI源码分析 本文基于Android 6.0的源码,来分析documentsUI模块. 原本基于7.1源码看了两天,但是Android 7.1与6.0中documentsUI模块差异 ...
- 机器学习(4)Hoeffding Inequality--界定概率边界
问题 假设空间的样本复杂度(sample complexity):随着问题规模的增长导致所需训练样本的增长称为sample complexity. 实际情况中,最有可能限制学习器成功的因素是训练数据的 ...
- FCKEditor在jsp页面中的配置方法
大家在使用博客园或者是在网站上面发表一些东西的时候,往往会发现,输入文字的不是一个简单的文本框,而是一个类似于word的在线编辑环境.这个插件叫FCKEditor,使用这个插件要进行一定程度的配置,下 ...
- String类的方法
String str = "hello"; /* * 1.String当中跟char[]有关系的方法 */ char[] array = str.toCharArr ...
- FaceRank-人脸打分基于 TensorFlow 的 CNN 模型
FaceRank-人脸打分基于 TensorFlow 的 CNN 模型 隐私 因为隐私问题,训练图片集并不提供,稍微可能会放一些卡通图片. 数据集 130张 128*128 张网络图片,图片名: 1- ...
- goroutine 加 channel 代替递归调用,突破递归调用的层级限制
package main import ( "fmt" "github.com/davecgh/go-spew/spew" "github.com/B ...
- 使用递归算法结合数据库解析成java树形结构
使用递归算法结合数据库解析成java树形结构 1.准备表结构及对应的表数据a.表结构: create table TB_TREE ( CID NUMBER not null, CNAME VARCHA ...