[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 ...
随机推荐
- vue file-loader vs url-loader
1.前言 如果我们希望在页面引入图片(包括img的src和background的url).当我们基于webpack进行开发时,引入图片会遇到一些问题. 其中一个就是引用路径的问题.拿backgroun ...
- phaser小游戏框架学习(二)
今天继续学习phaser.js.上周写的学习教程主要内容是创建游戏场景,游戏中的显示对象,按钮对象的使用以及如何在不同屏幕大小中完美适配.这篇博客以介绍游戏榜单的渲染更新为主. 代码地址:https: ...
- POJ3050 -- Hopscotch 简单的dfs搜索
原题链接:http://poj.org/problem?id=3050 (一些文字过会儿再说现在有事儿) #include <cstdio> #include <set> us ...
- STL用法
map.find(key) 获取map容器中指定键值x的元素,如果找到,返回此元素的迭代器,否则返回map::end()的迭代器(即查找到容器的末尾都没有找到此元素).
- 关于在多个UItextield切换焦点
本人对于应用的完美用户体验是这样认为:当一个应用是迎合用户习惯 ,并且在人机交互之中降低用户的学习成本 ,由于应用和人的思维方向一致时,就会有共鸣,这对于程序设计是有益的,因为只要愿意去改变总有优雅的 ...
- bashrc 与 profile 环境变量
profile 和 bashrc profile只在登陆的时候使用一次(当我们使用远程登陆上),而bashrc在每次新打开一个bash的时候都会执行(相当于path环境(登陆上了每次打开终端)) pr ...
- Web安全工程师(进阶)课程表
01-SQL注入漏洞原理与利用 预备知识: 了解HTTP协议,了解常见的数据库.脚本语言.中间件.具备基本的编程语言基础. 授课大纲: 第一章:SQL注入基础 1.1 Web应用架构分析1.2 SQL ...
- spring 和 spirngMvc 中 异常处理
spring 中 的 异常处理 使用的是aspectj @Aspect @Component /** * * @author **** * @createData 2017年7月13日 上午8:36: ...
- visual studio 2013 error: Page '312e8a59-2712-48a1-863e-0ef4e67961fc' not found.
In order to resolve this error do the following : Open Developer Command Prompt for VS 2013 as “Run ...
- vmware正在使用中问题
解决方法:找到vmware目录下的.lck文件,将其删掉即可.