Given a digit string, return all possible letter combinations that the number could represent.

Solution: 注意0,1,7,9

    char[][] chars = {{'a','b','c'},{'d','e','f'},{'g','h','i'},{'j','k','l'},{'m','n','o'},{'p','q','r','s'},{'t','u','v'},{'w','x','y','z'}};
    public List<String> letterCombinations(String digits) {
        List<String> ans = new ArrayList<>();
        if (digits == null || digits.length() == 0) {
            return ans;
        }
        helper(digits, 0, new StringBuilder(), ans);
        return ans;
    }
    private void helper(String s, int index, StringBuilder path, List<String> ans) {
        if (index == s.length()) {
            ans.add(path.toString());
            return;
        }
        char c = s.charAt(index);
        if (c == '1' || c == '0') {
            return;
        }
        int end = 2;
        if (c == '9' || c == '7') {
            end = 3;
        }
        for (int i = 0; i <= end; i++) {
            path.append(chars[c - '0' - 2][i]);
            helper(s, index + 1, path, ans);
            path.deleteCharAt(path.length() - 1);
        }
    }

results matching ""

    No results matching ""