运用到了队列的思想,但实际代码直接用list实现了
public List<String> letterCombinations(String digits) {
List<String> resultList = new ArrayList<String>();
//初始化号码对应的英文字母
String[] phoneDic = new String[10];
phoneDic[2] = "abc";phoneDic[3] = "def";phoneDic[4] = "ghi";phoneDic[5] = "jkl";
phoneDic[6] = "mno";phoneDic[7] = "pqrs";phoneDic[8] = "tuv";phoneDic[9] = "wxyz";
//获取参数的个数
char[] phoneNumberChars = digits.toCharArray();
int size = phoneNumberChars.length;
//长度为0直接返回
if (size == 0) return resultList;
//第一个输入号码对应的字符数组,依次填入集合
char[] phoneFirst = phoneDic[phoneNumberChars[0] - '0'].toCharArray();
for (char ch : phoneFirst
) {
resultList.add(String.valueOf(ch));
}
//结果中每个元素的长度
int reLength = 1;
//遍历之后的数字
for (int i = 1; i < size; i++) {
char number = phoneNumberChars[i];
//获取字符对应的号码
int numberInt = number - '0';
//获取数字对应的字母字典
char[] phoneDicChr = phoneDic[numberInt].toCharArray();
reLength ++;
while (resultList.get(0).length() < reLength) {
for (char word : phoneDicChr) {
String tmp = resultList.get(0) + String.valueOf(word);
resultList.add(tmp);
}
resultList.remove(0);
}
}
System.out.println(resultList);
return resultList;
}