当前位置: 代码迷 >> 综合 >> [leetcode] 17. Letter Combinations of a Phone Number (medium)
  详细解决方案

[leetcode] 17. Letter Combinations of a Phone Number (medium)

热度:3   发布时间:2024-01-05 01:02:33.0

递归DFS

    class Solution {Map<Character, String> mapping = new HashMap<>();public List<String> letterCombinations(String digits) {List<String> res = new ArrayList<>();if (digits.isEmpty())return res;mapping.put('2', "abc");mapping.put('3', "def");mapping.put('4', "ghi");mapping.put('5', "jkl");mapping.put('6', "mno");mapping.put('7', "pqrs");mapping.put('8', "tuv");mapping.put('9', "wxyz");char[] cdigits=digits.toCharArray();helper(cdigits, "", res);return res;}void helper(char[] cdigits, String curStr, List<String> res) {if (curStr.length() == cdigits.length) {res.add(curStr);return;}String curLetters = mapping.get(cdigits[curStr.length()]);for (char c : curLetters.toCharArray()) {helper(cdigits, curStr+c, res);}}}

 

  相关解决方案