题目描述:
输入输出描述:
解析:该题在LeetCode上是一个Medium题,像这种组合题,一般考虑回溯法,不过这次代码用的是哈希表
class Solution {
public List<String> letterCombinations(String digits) {
//方法:哈希表
HashMap<Character, String> hashMap = new HashMap<Character, String>(){
{
put('2',"abc");
put('3',"def");
put('4',"ghi");
put('5',"jkl");
put('6',"mno");
put('7',"pqrs");
put('8',"tuv");
put('9',"wxyz");
}
};
ArrayList<String> res = new ArrayList<>();
if(digits.length()==0) return res;
res.add("");
for (char c : digits.toCharArray()) {
ArrayList<String> tmp = new ArrayList<>();
String s = hashMap.get(c);
for (char t : s.toCharArray()) {
for (String re : res) {
tmp.add(re+t);
}
}
res=tmp;
}
return res;
}
}