iOS无重复字符的最长子串

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ago_lei/article/details/85233137

- (void)viewDidLoad {
    [super viewDidLoad];

    NSString *str = @"gasdkgkasgjdnafgdgda";
    
    NSMutableSet*set = [NSMutableSet new];
    
    long n = str.length;
    
    int ans = 0, i = 0, j = 0 ,index = 0;
    
    while (i<n && j<n) {


        if (![set containsObject:[str substringWithRange:NSMakeRange(j,1)]]) {
            
            [set addObject:[str substringWithRange:NSMakeRange(j++, 1)]];
            

            if (j - i > ans) {
                
                ans = j - i;
                
                index = i;
                
            }
            
        }else{
            
            [set removeObject:[str substringWithRange:NSMakeRange(i++, 1)]];
        }
     
}
    NSLog(@"%@",[str substringWithRange:NSMakeRange(index, ans)]);

}

猜你喜欢

转载自blog.csdn.net/ago_lei/article/details/85233137
今日推荐