LeetCode 17. 电话号码的字母组合

17. 电话号码的字母组合

给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。
在这里插入图片描述

关键字:所有组合
模式识别:搜索算法

解题思路:
自顶向下的递归实现深度搜索
定义子问题
在当前递归层结合子问题解决原问题

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

//深度优先遍历
- (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)];
            }
        }
    }
}
  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jarlen John

谢谢你给我一杯咖啡的温暖

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值