力扣题目学习笔记(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)];
            }
        }
    }
}
相关推荐
使一颗心免于哀伤3 小时前
《设计模式之禅》笔记摘录 - 21.状态模式
笔记·设计模式
Fanxt_Ja2 天前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
_落纸2 天前
三大基础无源电子元件——电阻(R)、电感(L)、电容(C)
笔记
Alice-YUE2 天前
【CSS学习笔记3】css特性
前端·css·笔记·html
2303_Alpha2 天前
SpringBoot
笔记·学习
萘柰奈2 天前
Unity学习----【进阶】TextMeshPro学习(三)--进阶知识点(TMP基础设置,材质球相关,两个辅助工具类)
学习·unity
沐矢羽2 天前
Tomcat PUT方法任意写文件漏洞学习
学习·tomcat
好奇龙猫2 天前
日语学习-日语知识点小记-进阶-JLPT-N1阶段蓝宝书,共120语法(10):91-100语法+考え方13
学习
向阳花开_miemie2 天前
Android音频学习(十八)——混音流程
学习·音视频
元亓亓亓2 天前
LeetCode热题100--105. 从前序与中序遍历序列构造二叉树--中等
算法·leetcode·职场和发展