力扣题目学习笔记(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)];
            }
        }
    }
}
相关推荐
北上ing4 小时前
算法练习:19.JZ29 顺时针打印矩阵
算法·leetcode·矩阵
小Tomkk4 小时前
2025年PMP 学习十五 第10章 项目资源管理
学习·pmp·项目pmp
oceanweave4 小时前
【K8S学习之生命周期钩子】详细了解 postStart 和 preStop 生命周期钩子
学习·kubernetes
XiaoyaoCarter5 小时前
每日一道leetcode
c++·算法·leetcode·职场和发展·二分查找·深度优先·前缀树
愚戏师5 小时前
Linux复习笔记(六)shell编程
linux·笔记·shell
.(ᗜ ˰ ᗜ) .6 小时前
机器学习笔记2
笔记
小葡萄20256 小时前
黑马程序员c++2024版笔记 第一章 变量和基本类型
笔记·c++20
顾子茵7 小时前
计算机图形学基础--Games101笔记(一)数学基础与光栅化
笔记·图形渲染
黄暄7 小时前
初识计算机网络。计算机网络基本概念,分类,性能指标
笔记·学习·计算机网络·考研
WarPigs7 小时前
Unity光照笔记
笔记·unity·游戏引擎