共计 668 个字符,预计需要花费 2 分钟才能阅读完成。
class Solution {public List<String> letterCombinations(String digits) {Map<Character, String> phone = new HashMap<Character, String>();
phone.put('2', "abc");
phone.put('3', "def");
phone.put('4', "ghi");
phone.put('5', "jkl");
phone.put('6', "mno");
phone.put('7', "pqrs");
phone.put('8', "tuv");
phone.put('9', "wxyz");
List<String> output = new ArrayList<String>();
for(int i = 0; i < digits.length(); i++) {String letter = phone.get(digits.charAt(i));
List<String> temp = new ArrayList<String>();
for(int j = 0; j < letter.length(); j++) {if(output.size() == 0) {temp.add(letter.substring(j, j+1));
}
else {for(String s : output) {temp.add(s+letter.substring(j, j+1));
}
}
}
output.clear();
output.addAll(temp);
temp.clear();}
return output;
}
}
正文完