LeetCode Letter Combinations of a Phone Number
2016-01-04 21:02
369 查看
题目:
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.
![](http://upload.wikimedia.org/wikipedia/commons/thumb/7/73/Telephone-keypad2.svg/200px-Telephone-keypad2.svg.png)
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.
题意:
给定一个电话的键盘,然后输入一个字符串,其中字符串上的数字表示的是电话键盘上对应的字母,那么僵所有对应的情况输出。
题解:
此题采用先将第一个数字对应的那些个字母保存进一个临时空间中,然后是第二个数字对应的那些字母,依次将第二个数字对应的字母遍历放到刚才第一个临时空间中。直接看代码。
public List<String> letterCombinations(String digits)
{
List<String> list = new ArrayList<String>();
int length = digits.length();
if(length == 0 || digits == null)
return list;
String[] map = new String[10];
map[0] = "";
map[1] = "";
map[2] = "abc";
map[3] = "def";
map[4] = "ghi";
map[5] = "jkl";
map[6] = "mno";
map[7] = "pqrs";
map[8] = "tuv";
map[9] = "wxyz";
list.add(""); //一开始输入一个空
for(int i = 0; i < digits.length(); i++)
{
List<String> temp = new ArrayList<String>(); //临时变量
String chars = map[digits.charAt(i) - '0'];
for(int j = 0; j < chars.length(); j++)
{
for(int k = 0; k < list.size(); k++)
{
temp.add(list.get(k) + chars.charAt(j)); //这里是关键
}
}
list = temp;
}
return list;
}
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.
![](http://upload.wikimedia.org/wikipedia/commons/thumb/7/73/Telephone-keypad2.svg/200px-Telephone-keypad2.svg.png)
Input:Digit string "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.
题意:
给定一个电话的键盘,然后输入一个字符串,其中字符串上的数字表示的是电话键盘上对应的字母,那么僵所有对应的情况输出。
题解:
此题采用先将第一个数字对应的那些个字母保存进一个临时空间中,然后是第二个数字对应的那些字母,依次将第二个数字对应的字母遍历放到刚才第一个临时空间中。直接看代码。
public List<String> letterCombinations(String digits)
{
List<String> list = new ArrayList<String>();
int length = digits.length();
if(length == 0 || digits == null)
return list;
String[] map = new String[10];
map[0] = "";
map[1] = "";
map[2] = "abc";
map[3] = "def";
map[4] = "ghi";
map[5] = "jkl";
map[6] = "mno";
map[7] = "pqrs";
map[8] = "tuv";
map[9] = "wxyz";
list.add(""); //一开始输入一个空
for(int i = 0; i < digits.length(); i++)
{
List<String> temp = new ArrayList<String>(); //临时变量
String chars = map[digits.charAt(i) - '0'];
for(int j = 0; j < chars.length(); j++)
{
for(int k = 0; k < list.size(); k++)
{
temp.add(list.get(k) + chars.charAt(j)); //这里是关键
}
}
list = temp;
}
return list;
}
相关文章推荐
- 关系型数据库管理系统简介
- 把重载的那些消息都看看,熟悉一下功能
- Synchronized原理
- python 代码片段19
- 【ThinkPHP3.2.3】学习ThinkPHP笔记:搭建环境(1)
- unity的TRANSFORM_TEX详解
- 以梦为马
- Mac 使用技巧
- ios之修饰符(public、internal、private)
- cocos2d-x3.2下使用Umeng 64位SDK注意事项
- UIViewController控制器的生命周期
- mfc的 windows消息处理
- js常见问题之为什么点击弹出的i总是最后一个
- 判断JavaScript对象为null或者属性为空的方法
- 文本框控件字体,背景色都可以单独设置。
- linux shell if语句
- 小光棍数(南阳oj458)(水题)
- SecureCRT显示中文和语法高亮
- LVS Nginx HAProxy 优缺点
- gcc