classSolution{
Map<String, String> phone =newHashMap<String, 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");}};
List<String> output =newArrayList<String>();publicvoidbacktrack(String combination, String next_digits){// if there is no more digits to checkif(next_digits.length()==0){// the combination is done
output.add(combination);}// if there are still digits to checkelse{// iterate over all letters which map // the next available digit
String digit = next_digits.substring(0,1);
String letters = phone.get(digit);for(int i =0; i < letters.length(); i++){
String letter = phone.get(digit).substring(i, i +1);// append the current letter to the combination// and proceed to the next digitsbacktrack(combination + letter, next_digits.substring(1));}}}public List<String>letterCombinations(String digits){if(digits.length()!=0)backtrack("", digits);return output;}}
作者:LeetCode
链接:https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/solution/dian-hua-hao-ma-de-zi-mu-zu-he-by-leetcode/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。