17 电话号码的字母组合 9 / hot100

本文介绍了一个编程问题,如何使用递归方法生成一个给定仅包含2-9数字的字符串的所有可能字母组合。Solution类中的backtrack函数实现了回溯算法,时间复杂度为O(m^n),m为每个数字对应字母组合的平均长度,n为输入字符串digits的长度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。

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

回溯

时间复杂度:O(m^n),m 是每个数字对应的字母组合的平均长度,n是digits长度。

这个递归写的很牛逼,很值得学习 。

class Solution {
    public List<String> letterCombinations(String digits) {
        List<String> combinations = new ArrayList<String>();
        //创建字符串列表
        if (digits.length() == 0) {
            return combinations;
        }//输入字符串为空,直接返回空列表
        Map<Character, String> phoneMap = new HashMap<Character, String>() {{
            put('2', "abc");
            put('3', "def");
            put('4', "ghi");
            put('5', "jkl");
            put('6', "mno");
            put('7', "pqrs");
            put('8', "tuv");
            put('9', "wxyz");
        }};
        //创建数字到字符串的映射关系
        backtrack(combinations, phoneMap, digits, 0, new StringBuffer());
        //回溯
        return combinations;
    }

    public void backtrack(List<String> combinations, Map<Character, String> phoneMap, 
String digits, int index, StringBuffer combination) {
        if (index == digits.length()) {
        //满足条件,递归返回
        //第一次调用不会进入if语句中
            combinations.add(combination.toString());
        } else {
            char digit = digits.charAt(index);
            //获得第index+1个字符
            String letters = phoneMap.get(digit);
            //获得第index+1个字符对应的字符串
            int lettersCount = letters.length();
            //获得第index+1个字符对应的字符串的长度
            for (int i = 0; i < lettersCount; i++) {
                combination.append(letters.charAt(i));
                //将第i+1个字符添加到combination中
                backtrack(combinations, phoneMap, digits, index + 1, combination);
                //递归进去,直到满足if条件,获得一个完整的combination加入到combinations中
                combination.deleteCharAt(index);
                //一个一个递归返回,删除combination的每个字符,以便进行下一次for循环
            }
        }
    }
}

补:

组合for循环中递归

List<String> res = new ArrayList<>();

StringBuffer temp = new StringBuffer();

public List<String> letterCombinations(String digits){
    if(digits.length() == 0) return res;
    // 将digits中的数字映射为字母组
    String[] numString = {"", "", "abc", "def","ghi", "gkl", "mno", "pqrs", "tuv", "wxyz"};
    backtrace(digits, numString, 0);
    return res;
}


public void backtrace(String digits, String[] numString, int num){
    // num记录temp当前长度
    // num记录递归到digits的哪一位
    if(num == digits.length()) {
        res.add(temp.toString());
        return;
    }
    // 获取str映射的字母组
    String str = numString[digits.charAt(num) - '0'];
    // 遍历str,每次遍历使用不同的str
    // 因为要获取abc、def两组字母的所有组合,所以要遍历abc(def)。把i<length设置成字幕组的长度
    for(int i = 0; i < str.length(); i++){
        temp.append(str.charAt(i));
        backtrace(digits, numString, i+1);
        temp.deleteCharAt(temp.length() - 1);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值