【剑指offer】面试题39 数组中出现次数超过一半的数字

来源:互联网 发布:李剑模糊阅读知乎 编辑:程序博客网 时间:2024/06/18 16:26

解法一:基于Partition函数的时间复杂度为O(n)
    算法思想:如果将数组排序,排好序的之后位于中间的数字一定就是出现次数超过一半的的数字。也就是说,这个数字是统计学上的中位数
    利用快速排序的思想实现此类算法。代码如下:
   

bool g_bInputInvalid = false;bool CheckInvalidArray(int* number, int length){    g_bInputInvalid = false;    if (number == NULL || length <= 0)        g_bInputInvalid = true;    return g_bInputInvalid;}bool CheckMoreThanHalf(int* numbers, int length, int number){    int times = 0;    for (int i = 0; i < length; i++)    {        if (numbers[i] == number)            times++;    }    bool isMoreInvalid = true;    if (times * 2 <= length)    {        g_bInputInvalid = true;        isMoreInvalid = false;    }    return isMoreInvalid;}int Partition(int array[], int left, int right){    int key = right;    int begin = left;    int end = key - 1;    while (begin < end)    {        //从左往右找比它大的        while (begin < end && array[begin] <= array[key])            begin++;        //从右往左找比它小的        while (begin < end && array[end] >= array[key])            end--;        if (array[begin] > array[end])        {            swap(array[begin], array[end]);            begin++;            //end--;        }    }    if (begin != key&&array[begin] > array[key])    {        swap(array[begin], array[key]);        return begin;    }    return right;}int MoreThanHalfNum(int* number, int length){    if (CheckInvalidArray(number, length))  //检测数组是否合法        return 0;    int middle = length >> 1;    int start = 0;    int end = length - 1;    int index = Partition(number, start, end);//实现快速排序的基础    while (index!=middle)    {        if (index < middle)        {            start = index + 1;            index = Partition(number, start, end);        }        else        {            end = index - 1;            index = Partition(number, start, end);        }    }    int result = number[index];    if (!CheckMoreThanHalf(number, length, result)) //判断该数字出现的次数是否超过数组的一半        return 0;    return result;}

解法二:
    算法思想:我们可以考虑再遍历数组的时候保留两个值:一个是数组中的数字,另一个次数。当我们遍历到下一个数字的时候,如果下一个数字和我们保存的数字相等,则此时加1;如果下一个数字和我们之前保存的数字不同则减一;如果次数为零我们需要保存下一个数字,并设置成1;由于我们要找的数字出现次数比其他所有数字出现的次数之和还要多,那么要找的数字肯定是最后一次把次数设为1时对应的数字

bool g_bInputInvalid = false;bool CheckInvalidArray(int* number, int length){    g_bInputInvalid = false;    if (number == NULL || length <= 0)        g_bInputInvalid = true;    return g_bInputInvalid;}bool CheckMoreThanHalf(int* numbers, int length, int number){    int times = 0;    for (int i = 0; i < length; i++)    {        if (numbers[i] == number)            times++;    }    bool isMoreInvalid = true;    if (times * 2 <= length)    {        g_bInputInvalid = true;        isMoreInvalid = false;    }    return isMoreInvalid;}int MoreThanHalfNum(int* number, int length){    if (CheckInvalidArray(number, length))        return 0;    int result = number[0];    int times = 1;    for (int i = 0; i < length; i++)    {        if (times == 0)        {            result = number[1];            times = 1;        }        else if (number[i] == result)        {            times++;        }        else            times--;    }    if (!CheckMoreThanHalf(number, length, result))        return 0;    return result;}
阅读全文
0 0