力扣题目学习笔记(OC + Swift)17. 电话号码的字母组合

17. 电话号码的字母组合

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

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

关键字:所有组合

模式识别:搜索算法

解题思路:

自顶向下的递归实现深度搜索

定义子问题

在当前递归层结合子问题解决原问题

Swift

swift 复制代码
func letterCombinations(_ digits: String) -> [String] {
        guard !digits.isEmpty else { return [] }
        
        let phoneMap:[Character : String] = [
            "2": "abc", "3": "def", "4": "ghi", "5": "jkl",
            "6": "mno", "7": "pqrs", "8": "tuv", "9": "wxyz"
        ]
        
        var combinations:[String] = [String]();
        var combination:String = ""
        
        //回朔法,
        func backTrack(_ index:Int) {
            if index == digits.count {
                combinations.append(combination)
            }else {
                let digit = digits[digits.index(digits.startIndex, offsetBy: index)]
                if let letters = phoneMap[digit] {
                    for letter in letters {
                        combination.append(letter)
                        backTrack(index+1)
                        combination.removeLast()
                    }
                }
            }
        }
        
        backTrack(0)
        
        return combinations
    }

OC

c 复制代码
//深度优先遍历
- (NSArray *)letterCombinations:(NSString *)digits {
    if (digits.length == 0) {
        return @[];
    }
    
    NSDictionary *phoneMap = @{
        @"2": @"abc", @"3": @"def", @"4": @"ghi", @"5": @"jkl",
        @"6": @"mno", @"7": @"pqrs", @"8": @"tuv", @"9": @"wxyz"
    };
    
    NSMutableArray *combinations = [NSMutableArray array];
    NSMutableString *combination = @"".mutableCopy;
    
    //回溯法
    [self backTrackWithCombinations:combinations
                combination:combination
                phoneMap:phoneMap
                             digits:digits
                              index:0];
    
    return combinations.copy;
}

- (void)backTrackWithCombinations:(NSMutableArray *)combinations
                      combination:(NSMutableString *)combination
                         phoneMap:(NSDictionary *)phoneMap
                           digits:(NSString *)digits
                            index:(NSInteger)index {
    if (index == digits.length) {
        [combinations addObject:combination.copy];
    }else {
        NSString *digit = [digits substringWithRange:NSMakeRange(index, 1)];
        NSString *letters = phoneMap[digit];
        if (letters.length > 0) {
            for (NSInteger i=0; i<letters.length; i++) {
                unichar charac = [letters characterAtIndex:i];
                NSString *str = [NSString stringWithCharacters:&charac length:1];
                
                [combination appendString:str];
                [self backTrackWithCombinations:combinations
                            combination:combination
                            phoneMap:phoneMap
                                         digits:digits
                                          index:index+1];
                [combination deleteCharactersInRange:NSMakeRange((combination).length-1, 1)];
            }
        }
    }
}
相关推荐
Drawing stars1 小时前
JAVA后端 前端 大模型应用 学习路线
java·前端·学习
崇山峻岭之间1 小时前
Matlab学习记录33
开发语言·学习·matlab
玄〤2 小时前
黑马点评中 VoucherOrderServiceImpl 实现类中的一人一单实现解析(单机部署)
java·数据库·redis·笔记·后端·mybatis·springboot
科技林总2 小时前
【系统分析师】3.5 多处理机系统
学习
老鼠只爱大米2 小时前
LeetCode算法题详解 239:滑动窗口最大值
算法·leetcode·双端队列·滑动窗口·滑动窗口最大值·单调队列
芯思路3 小时前
STM32开发学习笔记之三【按键】
笔记·stm32·学习
Lips6113 小时前
2026.1.11力扣刷题笔记
笔记·算法·leetcode
charlie1145141914 小时前
从 0 开始的机器学习——NumPy 线性代数部分
开发语言·人工智能·学习·线性代数·算法·机器学习·numpy
咚咚王者4 小时前
人工智能之核心基础 机器学习 第十二章 半监督学习
人工智能·学习·机器学习
袁气满满~_~4 小时前
Python数据分析学习
开发语言·笔记·python·学习