class Solution { private final String[] map = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; public List<String> letterCombinations(String digits) { List<String> result = new ArrayList<>(); if (digits.length() == 0) { return result; } generateComb(digits.toCharArray(), result, new StringBuilder(), 0); return result; } private void generateComb(char[] digits, List<String> result, StringBuilder current, int index) { if (index == digits.length) { result.add(current.toString()); return; } int pos = (int)(digits[index] - ‘0‘); for (int i = 0; i < map[pos].length(); i++) { current.append(map[pos].charAt(i)); generateComb(digits, result, current, index + 1); current.setLength(current.length() - 1); } } }
时间: 2024-10-23 08:27:10