*的字母组合

给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。

给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。

 

示例:

输入:"23"
输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number

class Solution {
        public List<String> letterCombinations(String digits) {
            List<String> lo;
            List<String> resl = new ArrayList<String>();
            if (digits.length() == 1) {
                return d2l(digits);
            } else {
                char cur = digits.charAt(digits.length() - 1);
                lo = letterCombinations(digits.substring(0, digits.length() - 1));
                List<String> curl = d2l(cur + "");
                for (int i = 0; i < lo.size(); i++) {
                    for (int j = 0; j < curl.size(); j++) {
                        resl.add(lo.get(i) + curl.get(j));
                    }
                }
            }
            return resl;
        }

        public List<String> d2l(String digits) {
            List<String> l = new ArrayList<String>();
            int n = Integer.parseInt(digits);
            l.add((char) (97 + (n - 2) * 3) + "");
            l.add((char) (97 + (n - 2) * 3 + 1) + "");
            l.add((char) (97 + (n - 2) * 3 + 2) + "");
            if (n == 9) {
                l.add((char) (97 + (n - 2) * 3 + 3) + "");
            }
            return l;
        }
    }
 
原文地址:https://www.cnblogs.com/cyy12/p/11804999.html