题目描述:
    image.png
    输入输出描述:
    image.png
    解析:该题在LeetCode上是一个Medium题,像这种组合题,一般考虑回溯法,不过这次代码用的是哈希表

    1. class Solution {
    2. public List<String> letterCombinations(String digits) {
    3. //方法:哈希表
    4. HashMap<Character, String> hashMap = new HashMap<Character, String>(){
    5. {
    6. put('2',"abc");
    7. put('3',"def");
    8. put('4',"ghi");
    9. put('5',"jkl");
    10. put('6',"mno");
    11. put('7',"pqrs");
    12. put('8',"tuv");
    13. put('9',"wxyz");
    14. }
    15. };
    16. ArrayList<String> res = new ArrayList<>();
    17. if(digits.length()==0) return res;
    18. res.add("");
    19. for (char c : digits.toCharArray()) {
    20. ArrayList<String> tmp = new ArrayList<>();
    21. String s = hashMap.get(c);
    22. for (char t : s.toCharArray()) {
    23. for (String re : res) {
    24. tmp.add(re+t);
    25. }
    26. }
    27. res=tmp;
    28. }
    29. return res;
    30. }
    31. }