一道簡(jiǎn)單的算法題

我們每周有技術(shù)分享會(huì),今天有道算法跟大家分享下。

題目

我說(shuō)說(shuō)我知道的三種思路

  • 先剔除N,再找出最大值
- (void)justTest{
    NSMutableArray *testArr = [NSMutableArray arrayWithArray:@[@[@3,@2,@1,@0],@[@23,@4,@4,@4,@4,@23]]];
    for (int i = 0; i < testArr.count; i++) {
        [self getMax:testArr[i]];
    }
}

- (void)getMax:(NSArray*)arr{
    NSMutableArray *temp = [NSMutableArray arrayWithArray:arr];
    for (int i = 0; i < arr.count; i ++) {
        NSNumber *num = arr[i];
        if (num.integerValue == arr.count - 1) {
            [temp removeObjectAtIndex:i];
            break;
        }
    }
    NSInteger max = 0;
    for (int i = 0; i < temp.count; i ++) {
        NSNumber *num = temp[i];
        if (max < num.integerValue) {
            max = num.integerValue;
        }
    }
    NSLog(@"最大數(shù)%ld",max);
}

這種方式要for循環(huán)兩次。

  • 只找出N,再找出最大值
- (void)justTest{
    NSMutableArray *testArr = [NSMutableArray arrayWithArray:@[@[@3,@2,@1,@0],@[@23,@4,@4,@4,@4,@23]]];
    for (int i = 0; i < testArr.count; i++) {
        [self getMax:testArr[i]];
    }
}

- (void)getMax:(NSArray*)arr{
    NSInteger N = 0;
    NSInteger max = 0;
    for (int i = 0; i < arr.count; i ++) {
        NSNumber *num = arr[i];
        if (num.integerValue == arr.count - 1 && N == 0) {
            N = num.integerValue;
        }else{
            if (num.integerValue > max) {
                max = num.integerValue;
            }
        }
    }
    NSLog(@"最大數(shù)%ld",max);
}

這種方式只要for循環(huán)一次性能更優(yōu)。

  • 通過冒泡排序方式
- (void)justTest{
    NSMutableArray *testArr = [NSMutableArray arrayWithArray:@[@[@3,@2,@1,@0],@[@23,@4,@9,@4,@4,@23]]];
    for (int i = 0; i < testArr.count; i++) {
        [self getMax:testArr[i]];
    }
}

- (void)getMax:(NSArray*)arr{
    NSMutableArray *tempArr = [NSMutableArray arrayWithArray:arr];
    for (int i = 0; i < tempArr.count - 1; i ++) {
        for (int j = 0; j < tempArr.count - 1 - i; j++) {
            if ([tempArr[j] integerValue] > [tempArr[j+1] integerValue]) {
                NSNumber *temp = tempArr[j];
                tempArr[j] = tempArr[j + 1];
                tempArr[j + 1] = temp;
            }
        }
    }
    NSInteger max = [tempArr.lastObject integerValue];
    NSInteger temp;
    if (max != tempArr.count) {
        temp = max;
    }else{
        temp = [tempArr[tempArr.count - 2] integerValue];
    }
    NSLog(@"最大數(shù)%ld",temp);
}

這種方式最耗時(shí),遍歷次數(shù)最多。

知識(shí)回顧

  • 二分查找(遞歸和非遞歸)
//遞歸查找
int binarySearch2(int a[], int value, int low, int high)
{
    int mid = (low + high)/2;
    if (a[mid] == value) {
        return mid;
    }else if (a[mid] > value){
        return binarySearch2(a, value, low, mid - 1);
    }else{
        return binarySearch2(a, value, mid + 1, high);
    }
}

// 非遞歸查找
int binarySearch(int a[], int value, int n)
{
    int low, high, mid;
    low = 0;
    high = n - 1;
    mid = (low + high)/2;
    while (low < high) {
        if (a[mid] == value) {
            return mid;
        }else if (a[mid] > value){
            high = mid - 1;
        }else{
            low = mid + 1;
        }
    }
    return -1;
}

  • 快速查找
void quicksort(int array[], int maxlen, int begin, int end)
{
    if (begin + 2 > end) {
        return;
    }
    int i = begin + 1;
    int j = end;
    if(begin < end)
    {
        while(i < j)
        {
            if(array[i] > array[begin]){
                int temp = array[i];
                array[i]= array[j];
                array[j]= temp;
                j--;
            }else{
                i++;
            }
        }
        
        if(array[i] >= array[begin]) {
            i--;
        }
        
        int temp = array[begin];
        array[begin] = array[i];
        array[i]= temp;
        
        quicksort(array, maxlen, begin, i);
        quicksort(array, maxlen, j, end);
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 題目:統(tǒng)計(jì)給定數(shù)字中,值為1的二進(jìn)制位的數(shù)量。如果是數(shù)組呢? 解法1:遍歷算法 第一種想法比較簡(jiǎn)單,從最后一位開始...
    Torival閱讀 1,537評(píng)論 0 9
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語(yǔ)法,類相關(guān)的語(yǔ)法,內(nèi)部類的語(yǔ)法,繼承相關(guān)的語(yǔ)法,異常的語(yǔ)法,線程的語(yǔ)...
    子非魚_t_閱讀 34,644評(píng)論 18 399
  • 記錄項(xiàng)目中關(guān)于ztree插件的使用。實(shí)現(xiàn)的功能有:排序、遷移、搜索、刪除、右鍵菜單、重命名、新增。 html代碼 ...
    maxwellyue閱讀 4,401評(píng)論 0 11
  • 本人近來(lái)在一位老先生的指導(dǎo)下學(xué)了學(xué)古文觀止,小成。想把心中想法進(jìn)行分享。今天先來(lái)一篇《滕王閣序》。 首先要說(shuō)這...
    933c8828262f閱讀 400評(píng)論 1 1

友情鏈接更多精彩內(nèi)容