[leetcode] 17. Letter Combinations of a Phone Number (medium)
递归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); } } }