iOS 判断数组中数字是否连续

此时此刻没有想法,顺手写的,可能有很多bug,欢迎指正。。。

-(BOOL)suibian:(NSArray  *)array{
    
    NSMutableArray *tempArr = [NSMutableArray array];
    NSMutableArray *tempArr2 = [NSMutableArray array];

    for (int i = 1; i < array.count; i ++) {
        if ([array[i] integerValue] > [array[i - 1] integerValue] ) {
            
            NSInteger max = [array[i] integerValue];
            NSInteger min = [array[i - 1] integerValue];
            if (max - min == 1) {
                [tempArr addObject:@"yes"];
            }
            
        }
    }
    for (int i = 1; i < array.count; i ++) {
        if ([array[i] integerValue] < [array[i - 1] integerValue]  ) {
            
            NSInteger max = [array[i - 1] integerValue];
            NSInteger min = [array[i] integerValue];
            if (max - min == 1) {
                [tempArr2 addObject:@"yes"];
            }
            
        }
    }
    if (tempArr.count == 5 || tempArr2.count == 5) {
        return YES;
    }else{
        
        return NO;
    }
    
}
判断数组中,字母是否连续
FOUNDATION_EXPORT BOOL hasSerialSubstrWithString(NSString * string);

+(BOOL)hasSerialSubstrWithString(NSString *string) {
    NSUInteger markLength = 3;
    if(markLength > string.length){
        return  NO;
    }
    
    NSUInteger length = string.length;
    NSUInteger substrLength = 0;
    NSUInteger index = 0;
    BOOL isDesc = 1;
    // 65-90 A-Z  97-122 a-z
    while (index < length) {
        int asciiCode = [string characterAtIndex:index];
        NSLog(@"%d,%lu",asciiCode,substrLength);
        if(!(asciiCode >= 65 && asciiCode <= 90) && !(asciiCode >= 97 && asciiCode <= 122) ){
            if(index >= length - 1){
                return  NO;
            }else {
                index++;
                substrLength = 0;
                isDesc = NO;
                NSLog(@"d1");
                continue;
            }
            
        }
        if(index + 1 >= length){
            return NO;
        }
        int nextAscii = [string characterAtIndex:index + 1];
        if(!(nextAscii >= 65 && nextAscii <= 90)  && !(nextAscii >= 97 && nextAscii <= 122) ){
            substrLength = 0;
            index += 2;
            continue;
        }
        
        if(nextAscii - asciiCode == 1 ){
            if(substrLength == 0){
                substrLength++;
                index++;
                isDesc = NO;
                continue;
            }else{
                if(isDesc){
                    
                    substrLength = 0;
                    index += 1;
                    continue;
                }else{
                    substrLength++;
                    index++;
                    if(substrLength >= markLength){
                        return YES;
                    }
                    continue;
                }
            }
            
        }else if(nextAscii -asciiCode == -1){
            
            if(substrLength == 0){
                substrLength++;
                index++;
                isDesc = YES;
                continue;
            }else{
                if(isDesc){
                    substrLength++;
                    index++;
                    if(substrLength >= markLength){
                        return YES;
                    }
                    continue;
                    
                }else{
                    substrLength = 0;
                    index += 1;
                    continue;
                }
            }
            
        }else {
            substrLength = 0;
            index ++;
            continue;
        }
        
        
        
    }
    return NO;
}
               

你可能感兴趣的:(iOS 判断数组中数字是否连续)