The Weekend Blog

I hope it will continue to be updated

算法和数据结构

1.找到字符串中最长无重复子串

  • 例如aabbc,最长无重复子串abc,长度3;abcd,长度4;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 思路:
// 1.先申请一个字典,key是每一个字符,value是字符上一次出现的位置
// 2.pre:该字符上次出现的位置
// 3.cur:当前位置和上次出现位置的距离
// 4.len:最长距离
- (void)maxUniqu:(NSString *)string{
    NSMutableDictionary *nums = [NSMutableDictionary new];
    NSArray  *array = [string componentsSeparatedByString:@","];
    [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        nums[obj] = @(-1);
    }];
    
   __block int pre = -1;
   __block int len = 0;
   __block int cur = 0;
    [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        pre = MAX(pre, [nums[obj] intValue]);
        cur = idx - pre;
        len = MAX(len, cur);
        nums[obj] = @(idx);
        NSLog(@"\nobj:%@\npre:%d\ncur:%d\nlen:%d\nnums:%@",obj,pre,cur,len,nums);
    }];
}