1. 程式人生 > >LeetCode 17: Letters Combination of a Phone Number

LeetCode 17: Letters Combination of a Phone Number

res length arr gen number code git pos lee

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); } } }

LeetCode 17: Letters Combination of a Phone Number