[LeetCode]17. Letter Combinations of a Phone Number电话号码的字母组合
Given a string containing digits from 2-9 inclusive, 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. Note that 1 does not map to any letters.
![]()
Example:
Input: "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.
这题要求列出所有可能古德字符组合,我们先把1-9的字符列出来,然后从digits中一次取出数字代表的字符,然后做笛卡尔积的处理
这里的难点在于2个数的时候好处理,2层的循环就能解决,但是3个数,4个数怎么做呢?
4个数时,我们需要先求出2个数的笛卡尔积,然后再去处理第3个,第4个
所以我们在2层循环上,加上一层控制,在外部选定完i后,我们要在原来的字符串基础上加上第i个数字代表的字符,我们每次把list(0)的元素remove出来,然后加上
新的字符再添加到list的末尾,这样我们就可以根据list(0)的长度判断这一轮字符有没有全部加上,就像[b,c,ad,ae,af]这样,此时i=1,list.get(0).length()=1,这就说明还有这轮还没加完
直到[ad, ae, af, bd, be, bf, cd, ce, cf],此时i=1但list.get(0).length()=2,说明这轮结束,回到外层
class Solution {
public List<String> letterCombinations(String digits) {
List<String> list = new ArrayList<String>();
if(digits.isEmpty()) return list;
String[] str = new String[] {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
list.add("");
String temp = null;
for(int i = 0; i < digits.length(); i++) {
int k = Integer.parseInt(digits.charAt(i)+"");
while(list.get(0).length() == i)
{
temp = list.remove(0);
for(int j = 0; j < str[k].length(); j++){
list.add(temp + str[k].charAt(j));
}
}
}
return list;
}
}
[LeetCode]17. Letter Combinations of a Phone Number电话号码的字母组合的更多相关文章
- [LeetCode] 17. Letter Combinations of a Phone Number 电话号码的字母组合
Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...
- 【LeetCode】17. Letter Combinations of a Phone Number 电话号码的字母组合
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:电话号码, 字母组合,回溯法,题解,leetcode, ...
- Leetcode 17. Letter Combinations of a Phone Number(水)
17. Letter Combinations of a Phone Number Medium Given a string containing digits from 2-9 inclusive ...
- [LintCode] Letter Combinations of a Phone Number 电话号码的字母组合
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- [leetcode 17]Letter Combinations of a Phone Number
1 题目: Given a digit string, return all possible letter combinations that the number could represent. ...
- Java [leetcode 17]Letter Combinations of a Phone Number
题目描述: Given a digit string, return all possible letter combinations that the number could represent. ...
- Leetcode 17.——Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- [leetcode]17. Letter Combinations of a Phone Number手机键盘的字母组合
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...
- [LeetCode] 17. Letter Combinations of a Phone Number ☆☆
Given a digit string, return all possible letter combinations that the number could represent. A map ...
随机推荐
- 树形DP--求树上任意两点间距离和
例题:HDU2376 HDU6446(2018CCPC网络赛) 思路:求任意两点间距离和可以转换为->路径长度乘经过路径次数的和. 求经过次数:设这条边两端的点,被经过的次数分别为A和B,那 ...
- web.xml 有什么用?(Java框架)
1.每个javaEE工程中都有web.xml文件,那么它的作用是什么呢?它是每个web.xml工程都必须的吗? 一个web中可以没有web.xml文件,也就是说,web.xml文件并不是web工程 ...
- ASP前端控件设置只读不要用enabled
会导致后台取不到这个控件的值,应该用readonly
- ConcurrentHashMap原理详解
参考链接:https://www.cnblogs.com/chengxiao/p/6842045.html https://www.cnblogs.com/ITtangtang/p/3948786.h ...
- 高仿JDK动态代理 底层源码实现
动态代理实现思路 实现功能:通过Proxy.newProxyInstance返回代理对象 1.创建一个处理业务逻辑的接口,我们也和JDK一样,都使用InvocationHandler作为接口名,然后接 ...
- 数据结构1:数据结构与算法C语言版分析概述
本节开始将带领大家系统地学习数据结构,作为一门计算机专业大二学生的必修课程,该课程面对的目标人群为初步具备基本编程能力和编程思想的程序员(大一接触了 C 语言或者 C++).通过系统地学习数据结构,可 ...
- 安装 zookeeper
https://www.w3cschool.cn/zookeeper/zookeeper_cli.html ZooKeeper是一种分布式协调服务,用于管理大型主机.在分布式环境中协调和管理服务是一个 ...
- C语言值拷贝传递机制
当参数是常量,变量,或表达式时,传递的数据就是这些数据对象所具有的内容,这种方式称为数值参数传递方式(简称传值方式).如果函数调用时所传递的实参是数据对象在内存中的存储单元的首地址值,这种方式称为地址 ...
- 2015苏州大学ACM-ICPC集训队选拔赛(1) 1007
连通图 Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other) Total Submissio ...
- ajax加载菊花loading效果
Ajax异步请求的时候,一般都会利用一个动态的gif小图片来制作一个Ajax Loading,以便增加用户体验. 这里我们可以使用Spin.js,该js脚本压缩后5k,可以不用任何图片,任何外部CSS ...