iOS算法小记

  • 说明

    1. 这是一些iOS面试的小小算法题,在此做个笔记,以供参考和以后的翻阅。
    2. 用C语言实现字符串的翻转计算一个View上的UIImageView的个数,这两个算法题目,只给出代码,工程文件未做上传,请谅解。
    3. Demo是一个macOS的命令行工程,为了避免运行模拟器的麻烦。
    4. 某些代码,并未按照《禅与Objective-C编程艺术》中写的那样规范,请轻喷。
    5. Demo地址

  • 用C语言写一个函数,实现翻转字符串,并考虑时间复杂度、空间复杂度,尽可能最小

    /*实现字符串翻转*/
    char *reverse_str(char *str) {
        if(NULL == str) { //字符串为空直接返回
            return str;
        }
        char *begin;
        char *end;
        begin = end = str;
    
        while(*end != '\0') { //end指向字符串的末尾
            end++;
        }
        --end;
    
        char temp;
        while(begin < end) { //取地址,交换两个字符
            temp = *begin;
            *begin = *end;
            *end = temp;
            begin++;
            end--;
        }
    
        return str; //返回结果
    }
    
    void main() {
        char str[] = "kengdiedemianshiti";
        printf(reverse_str(str));
    }
    
    
    

  • 用Objective-C语言 实现冒泡排序

    //Objective-C 冒泡排序
    + (void)inputNeedArray:(NSMutableArray *)array
    {
        //依次拿相邻的两个元素做比较
        for (int i = 0; i < array.count; ++i) {
            for (int j = 0; j < array.count - 1; ++j) {
                if (array[j] < array[j + 1]) {
                    [array exchangeObjectAtIndex:j withObjectAtIndex:j + 1];
                }
                NSString * str = @"";
                for (NSNumber * value in array) {
                    str = [str stringByAppendingString:[NSString stringWithFormat:@"%zd ",[value integerValue]]];
                }
                NSLog(@"%@",str);
            }
            NSLog(@"------");
        }
    }
    

  • 用Objective-C语言 实现选择排序

    //Objective-C 选择排序
    + (void)inputNeedArray:(NSMutableArray *)array
    {
        //拿第一个跟后面的挨个比较
        for (int i = 0; i < array.count; i++) {
            for (int j = i + 1; j < array.count; j++) {
                if (array[i] < array[j]) {
                    [array exchangeObjectAtIndex:i withObjectAtIndex:j];
                }
                
                NSString * str = @"";
                for (NSNumber * value in array) {
                    str = [str stringByAppendingString:[NSString stringWithFormat:@"%zd ",[value integerValue]]];
                }
                NSLog(@"%@",str);
            }
            NSLog(@"------");
        }
    }
    

  • 写一个函数,给它传一个数组,去除数组中包含特定字符的元素,并返回一个新的数组

    //输入一个数组,返回去除包含"Foo"字符串的元素的一个数组
    //[@"A", @"FooB", @"C", @"FooH", @"FooI"
    + (void)returnNeedArray:(NSMutableArray *)array
      {
        NSMutableArray *muArr = [NSMutableArray arrayWithCapacity:1];
    
        for (int i = 0; i < array.count; i++) {
            if (![array[i] hasPrefix:@"Foo"]) {
                [muArr addObject:array[i]];
            }
        }
    
        NSLog(@"%@", muArr);
      }
    

  • 写一个函数,将两个有序数组合并后,得到一个新的有序数组,并且无重复元素

    + (NSMutableArray *)combineArray:(NSArray *)arrayNumberOne otherArray:(NSArray *)arrayNumberTwo
    {
        //    NSArray *array1 = @[@1, @3, @5, @6,];
        //    NSArray *array2 = @[@2, @3, @4, @5, @7];
        NSMutableArray *resultArray = [NSMutableArray arrayWithCapacity:1];
        int i = 0;
        int j = 0;
        while (i < arrayNumberOne.count && j < arrayNumberTwo.count) {
            if (arrayNumberOne[i] < arrayNumberTwo[j]) {
                resultArray[i + j] = arrayNumberOne[i];
                i++;
            }
            else {
                resultArray[i + j] = arrayNumberTwo[j];
                j++;
            }
        }
        while (i < arrayNumberOne.count) {
            resultArray[i + j] = arrayNumberOne[i];
            i++;
        }
        while (j < arrayNumberTwo.count) {
            resultArray[i + j] = arrayNumberTwo[j];
            j++;
        }
        //去重 有序
        NSOrderedSet *set = [NSMutableOrderedSet orderedSetWithArray:resultArray];
        NSLog(@"%@", set.array);
        return resultArray;
    }
    

  • 关于GCD的打印问题

    + (void)prinfLogForGCD
    {
        dispatch_queue_t queue = dispatch_queue_create("myQueue", DISPATCH_QUEUE_SERIAL);
        NSLog(@"1 --- %@", [NSThread currentThread]);
        dispatch_async(queue, ^{
            NSLog(@"2 --- %@", [NSThread currentThread]);
            dispatch_sync(queue, ^{
                NSLog(@"3 --- %@", [NSThread currentThread]);
            });
            NSLog(@"4 --- %@", [NSThread currentThread]);
        });
        NSLog(@"5 --- %@", [NSThread currentThread]);
    }
    

  • 关于object_getClass 和 class的问题

    #import 
    
    + (void)printLogForObjcgetClassAndClass
    {
        XXPrintObjectClass *obj = [XXPrintObjectClass new];
        
        NSLog(@"instance         :%p", obj);
        NSLog(@"class            :%p", object_getClass(obj));
        NSLog(@"meta class       :%p", object_getClass(object_getClass(obj)));
        NSLog(@"root meta        :%p", object_getClass(object_getClass(object_getClass(obj))));
        NSLog(@"root meta's meta :%p", object_getClass(object_getClass(object_getClass(object_getClass(obj)))));
        
        NSLog(@"---------------------------------------------");
        
        NSLog(@"class            :%p", [obj class]);
        NSLog(@"meta class       :%p", [[obj class] class]);
        NSLog(@"root meta        :%p", [[[obj class] class] class]);
        NSLog(@"root meta's meta :%p", [[[[obj class] class] class] class]);
    }
    

  • 写一个函数,要求能计算出这个View上的子视图中UIImageView的个数

    - (NSInteger)returnUIImageViewNumber:(UIView *)view
    {
        static NSInteger a = 0;
        for (int i = 0; i < view.subviews.count; i++) {
            if ([[NSString stringWithFormat:@"%@", [view.subviews[i] class]] isEqualToString:@"UIImageView"]) {
                a++;
            }
            else if (view.subviews[i].subviews.count != 0) {
                [self returnUIImageViewNumber:view.subviews[i]];
            }
        }
    //    NSLog(@"%d", a);
        return a;
    }
    

  • 写一个函数,传给它一个二维矩阵和一个数值,来判断是否存在这样的元素在矩阵中

    + (void)checkElementFromArray:(NSArray *)array findNumber:(int)number
    {
        BOOL isOrNot = NO;
        //判断二维数组是否为空
        if (array.count != 0) {
            //总行数
            int rows = (int)array.count;
            NSArray *arr = array[0];
            if (arr.count != 0) {
                //总列数
                int columns = (int)arr.count;
                //行
                int row = 0;
                //列
                int column = columns - 1; //最后一个元素
                while (row < rows && column >= 0) {
                    NSString *arrStr = [NSString stringWithFormat:@"%@", array[row][column]];
                    NSString *numStr = [NSString stringWithFormat:@"%d", number];
                    int arrNumber = [arrStr intValue];
                    int targetNumber = [numStr intValue];
                    if (arrNumber == targetNumber) {
                        isOrNot = YES;
                        break;
                    }
                    else if (arrNumber > targetNumber) {
                        --column;
                    }
                    else {
                        ++row;
                    }
                }
            }
        }
        NSLog(@"%hhd", isOrNot);
    }
    

你可能感兴趣的:(iOS算法小记)