您的位置:首页 > 其它

LeetCode-17. Letter Combinations of a Phone Number

2017-02-12 16:24 483 查看
一、问题描述

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"].

二、解题思路

参考LeetCode论坛中的解题思路,使用LinkedList模拟FIFO队列。

三、代码如下

public class Solution {
public List<String> letterCombinations(String digits) {
String[][] d2l={{""},{""},{"a","b","c"},{"d","e","f"},{"g","h","i"},{"j","k","l"},{"m","n","o"},{"p","q","r","s"},{"t","u","v"},{"w","x","y","z"}};
LinkedList<String> result=new LinkedList<String>();
if(digits==null || digits.length()==0)
return result;
result.add("");
for(int i=0;i<digits.length();i++){
int temp=((int)digits.charAt(i))-48;
while(result.peek().length()==i){
String t=result.remove();
for(int j=0;j<d2l[temp].length;j++){
result.add(t+d2l[temp][j]);
}
}
}
return result;
}
}
代码中涉及到char字符转换为int的转化方法,是时候整理一下不同类型的变量相互转化的问题了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: