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"].
分析过程
- 题目归类:
递归 - 题目分析:
观察到这种题目都是与第二个数加到一起,所以可用递归来进行,递归中用for来遍历所有的情况,然后每种情况先将字符加到string中,然后进行递归(去除一位),在递归结束后,string删除该位。
推出条件当String 的长度为1时,加入到StringList中。 - 边界分析:
- 空值分析
- 循环边界分析
- 方法分析:
- 数据结构分析
- 状态机
- 状态转移方程
- 最优解
- 测试用例构建
代码实现
class Solution {
List<String> list = new ArrayList<>();
String string = new String();
public List<String> letterCombinations(String digits) {
if(digits==null||digits.length()==0)
return list;
int l = digits.length();
char ch = digits.charAt(0);
if(l==1){
switch(ch){
case '2':
for(int i = 0; i <3;i++){
string += (char)(i+'a');
list.add(new String(string));
string=string.substring(0,string.length()-1);
}
return list;
case '3':
for(int i = 0; i <3;i++){
string += (char)(i+'d');
list.add(new String(string));
string=string.substring(0,string.length()-1);
}
return list;
case '4':
for(int i = 0; i <3;i++){
string += (char)(i+'g');
list.add(new String(string));
string=string.substring(0,string.length()-1);
}
return list;
case '5':
for(int i = 0; i <3;i++){
string += (char)(i+'j');
list.add(new String(string));
string=string.substring(0,string.length()-1);
}
return list;
case '6':
for(int i = 0; i <3;i++){
string += (char)(i+'m');
list.add(new String(string));
string=string.substring(0,string.length()-1);
}
return list;
case '7':
for(int i = 0; i <4;i++){
string += (char)(i+'p');
list.add(new String(string));
string=string.substring(0,string.length()-1);
}
return list;
case '8':
for(int i = 0; i <3;i++){
string += (char)(i+'t');
list.add(new String(string));
string=string.substring(0,string.length()-1);
}
return list;
case '9':
for(int i = 0; i <4;i++){
string += (char)(i+'w');
list.add(new String(string));
string=string.substring(0,string.length()-1);
}
return list;
}
}
switch(ch){
case '2':
for(int i = 0; i <3;i++){
string += (char)(i+'a');
letterCombinations(digits.substring(1,digits.length()));
string=string.substring(0,string.length()-1);
}
return list;
case '3':
for(int i = 0; i <3;i++){
string += (char)(i+'d');
letterCombinations(digits.substring(1,digits.length()));
string=string.substring(0,string.length()-1);
}
return list;
case '4':
for(int i = 0; i <3;i++){
string += (char)(i+'g');
letterCombinations(digits.substring(1,digits.length()));
string=string.substring(0,string.length()-1);
}
return list;
case '5':
for(int i = 0; i <3;i++){
string += (char)(i+'j');
letterCombinations(digits.substring(1,digits.length()));
string=string.substring(0,string.length()-1);
}
return list;
case '6':
for(int i = 0; i <3;i++){
string += (char)(i+'m');
letterCombinations(digits.substring(1,digits.length()));
string=string.substring(0,string.length()-1);
}
return list;
case '7':
for(int i = 0; i <4;i++){
string += (char)(i+'p');
letterCombinations(digits.substring(1,digits.length()));
string=string.substring(0,string.length()-1);
}
return list;
case '8':
for(int i = 0; i <3;i++){
string += (char)(i+'t');
letterCombinations(digits.substring(1,digits.length()));
string=string.substring(0,string.length()-1);
}
return list;
case '9':
for(int i = 0; i <4;i++){
string += (char)(i+'w');
letterCombinations(digits.substring(1,digits.length()));
string=string.substring(0,string.length()-1);
}
return list;
}
return list;
}
}
效率提高
虽然很快做出来了,但是效率不高,效率高的做法是:
看了别人的做法,方法是一样的,但是可以将数据直接保存到static String[] MAP = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
,然后用 for (char c : MAP[digits.charAt(index) - '0'].toCharArray())来获得。