用了HashMap,效率还行,不过感觉有更优的解法。后边再考虑优化。
class Solution {
public static List<String> letterCombinations(String digits) {
if(digits.isEmpty())return new ArrayList<String>();
String[] num2 = {
"a","b","c"};
String[] num3 = {
"d","e","f"};
String[] num4 = {
"h","g","i"};
String[] num5 = {
"j","k","l"};
String[] num6 = {
"m","n","o"};
String[] num7 = {
"p","q","r","s"};
String[] num8 = {
"t","u","v"};
String[] num9 = {
"w","x","y","z"};
char[] numbers = digits.toCharArray();
int point = 1;
int length = numbers.length;
List<String> list = new ArrayList<String>();
List<String> result;
HashMap<Character, String[]> info = new HashMap<Character, String[]>();
info.put('2', num2);
info.put('3', num3);
info.put('4', num4);
info.put('5', num5);
info.put('6', num6);
info.put('7', num7);
info.put('8', num8);
info.put('9', num9);
String[] buffer = info.get(numbers[0]);
for(int s = 0 ,l = buffer.length;s<l;s++) {
list.add(buffer[s]);
}
while(point<length) {
result=new ArrayList<String>();
String[] useing = info.get(numbers[point]);
for(int i = 0 , m = list.size();i < m ;i++ ) {
for(int j = 0,k = useing.length; j < k ; j++) {
result.add(list.get(i)+ useing[j]);
}
}
list=result;
point++;
}
return list;
}
}