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;
}
               
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,742评论 25 709
  • 今天我又来了! 今天几乎准时下班了。对了顺便记录下昨天9:30-18:30连续开会9小时的奇迹。今天的状态比...
    janice2017阅读 1,693评论 0 0
  • “讨厌所有喜欢主动张罗局面、积极热情地照顾别人、穿衣打扮花枝招展、语音声调高人四度的人。” 面对这样类型的人,我会...
    Miss伊柚阅读 4,001评论 3 4
  • 在灯红酒绿繁世中,我努力地寻求在那街口望着我的你,我渴望目光交汇的那一秒,那注目;那深邃水灵的泪珠看到你的期...
    鬼手白头人阅读 3,229评论 0 0
  • 你叫有备而来的路人甲我叫有备而来的打酱油
    1860fb3b42da阅读 1,445评论 0 2

友情链接更多精彩内容