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的更多相关文章

  1. LeetCode: Letter Combinations of a Phone Number 解题报告

    Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...

  2. [LeetCode]Letter Combinations of a Phone Number题解

    Letter Combinations of a Phone Number: Given a digit string, return all possible letter combinations ...

  3. [LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  4. LeetCode——Letter Combinations of a Phone Number

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  5. [LeetCode] Letter Combinations of a Phone Number

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  6. [LeetCode] Letter Combinations of a Phone Number(bfs)

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  7. LeetCode Letter Combinations of a Phone Number (DFS)

    题意 Given a digit string, return all possible letter combinations that the number could represent. A ...

  8. [LeetCode] Letter Combinations of a Phone Number 回溯

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  9. LeetCode Letter Combinations of a Phone Number 电话号码组合

    题意:给一个电话号码,要求返回所有在手机上按键的组合,组合必须由键盘上号码的下方的字母组成. 思路:尼玛,一直RE,题意都不说0和1怎么办.DP解决. class Solution { public: ...

随机推荐

  1. 剥析surging的架构思想

    1.前言   前面第一篇阐述了采用基于.NET CORE微服务架构,应用surging服务端与客户端之间进行通信的简单示例以及对于surging服务化框架简单介绍.在这篇文章中,我们将剥析surgin ...

  2. SQL server数据库备份还原问题备忘(亲测有效)

    问题一:SQL server数据库备份还原方法 http://www.cnblogs.com/zgqys1980/archive/2012/07/04/2576382.html 问题二:无法执行 BA ...

  3. Java基础语法<十二> 泛型程序设计

    1 意义 泛型程序设计意味着编写的代码可以被很多不同类型的对象所重用. 常见应用 : ArrayList 2 K T V E ? object等的含义 类型变量使用大写形式 E – Element ( ...

  4. PC-lint集成于SourceInsight 范例以及简单分析;提高代码的健壮性;

    写代码之际突然想起了pc-lint这个"古董级"的代码静态分析工具;   下午机房的服务器歇菜了,没法调试游戏,刚好抽出时间来研究一下pc-lint集成在SourceInsight ...

  5. [bzoj 2243]: [SDOI2011]染色 [树链剖分][线段树]

    Description 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同一段),如“ ...

  6. Selenium模拟JQuery滑动解锁

    滑动解锁一直做UI自动化的难点之一,我补一篇滑动解锁的例子,希望能给初做Web UI自动化测试的同学一些思路. 首先先看个例子. https://www.helloweba.com/demo/2017 ...

  7. UglifyJS-- 对你的js做了什么

    也不是闲着没事去看压缩代码,但今天调试自己代码的时候发现有点意思.因为是自己写的,虽然压缩了,格式化之后还是很好辨认.当然作为min的首要准则不是可读性,而是精简.那么它会尽量的缩短代码,尽量的保持一 ...

  8. ES6 变量、常量声明总结

    较之前ES5,新颁布在声明上有改变 一.var  对比  let 1.作用域不同 let只在命令所在的代码块 {} 里有效 ES5只有全局作用域和函数作用域,没有块级作用域,带来很多不合理的场景,比如 ...

  9. Android中的内容提供者

    Android中的内容提供者 为什么需要内容提供者 为了跨程序访问数据.试想如果在App-1中创建了一个私有数据库,App-2是不能直接访问的.因为权限不够,虽然可以使用chmod 777来修改权限, ...

  10. SMBLoris windows拒绝服务漏洞

    在美国拉斯维加斯举行的2017年度DEF CON黑客大会上,安全研究人员公布了Windows系统上的一个长达20年没有发现的漏洞,该漏洞名为"SMBLoris",黑客可以轻松的使用 ...