一、题目
数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1, 2, 3, 2, 2, 2, 5, 4, 2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。
二、关键
三、解释
1.解法一、时间复杂度为O(n)的算法得到数组中任意第k大的数字的算法?先在数组中随机选择一个数字,然后调整数组中数字的顺序,使得比选中的数字小的数字都在它的左边,比选中的数字大的数字都在它的右边。如果这个选中的数字的下标大于n/2,那么中位数应该位于它的左边,那么我们应该继续在它的左边部分的数组中查找;如果它的下标小于n/2,那么中位数应该位于它的右边,我们可以接着在它的右边部分的数组中查找。这是一个递归的过程。
2.解法二:数组中有一个数字出现的次数超过了数组长度的一半,也就是说它出现的次数比其他所有数字出现的次数的和还要多。因此,我们考虑在遍历数组的时候保存两个值:一个是数组中的一个数字;另一个是次数。当我们遍历到下一个数字的时候,如果下一个数字和我们之前保存的数字相同,则次数加1;如果下一个数字和我们之前保存的数字不同,则次数减1。如果次数为零,那么我们需要保存下一个数字,并把次数设为1.由于我们要找的数字出现的次数比其他所有数字出现的次数之和还要多,那么要找的数字肯定是最后一次把次数设为1时对应的数字。
3.如何利用一个数字出现的次数超过了数组长度的一半?如果把这个数组排序,那么排序之后位于数组中间的数字一定就是那个出现次数超过数组长度一半的数字。即,要找的数字是数组的中位数。即长度为n的数组中第n/2大的数字。
四、代码
#include <cstdio>
#include "..\Utilities\Array.h"
bool g_bInputInvalid = false;
bool CheckInvalidArray(int* numbers, int length) //检测输入的数组是否有效
{
g_bInputInvalid = false;
if(numbers == nullptr && 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 isMoreThanHalf = true;
if(times * 2 <= length)
{
g_bInputInvalid = true;
isMoreThanHalf = false;
}
return isMoreThanHalf;
}
// ====================方法1====================
int MoreThanHalfNum_Solution1(int* numbers, int length)
{
if(CheckInvalidArray(numbers, length))
return 0;
int middle = length >> 1;
int start = 0;
int end = length - 1;
int index = Partition(numbers, length, start, end);
while(index != middle)
{
if(index > middle)
{
end = index - 1;
index = Partition(numbers, length, start, end);
}
else
{
start = index + 1;
index = Partition(numbers, length, start, end);
}
}
int result = numbers[middle];
if(!CheckMoreThanHalf(numbers, length, result))
result = 0;
return result;
}
// ====================方法2====================
int MoreThanHalfNum_Solution2(int* numbers, int length)
{
if(CheckInvalidArray(numbers, length))
return 0;
int result = numbers[0];
int times = 1;
for(int i = 1; i < length; ++i)
{
if(times == 0)
{
result = numbers[i];
times = 1;
}
else if(numbers[i] == result)
times++;
else
times--;
}
if(!CheckMoreThanHalf(numbers, length, result))
result = 0;
return result;
}
// ====================测试代码====================
void Test(char* testName, int* numbers, int length, int expectedValue, bool expectedFlag)
{
if(testName != nullptr)
printf("%s begins: \n", testName);
int* copy = new int[length];
for(int i = 0; i < length; ++i)
copy[i] = numbers[i];
printf("Test for solution1: ");
int result = MoreThanHalfNum_Solution1(numbers, length);
if(result == expectedValue && g_bInputInvalid == expectedFlag)
printf("Passed.\n");
else
printf("Failed.\n");
printf("Test for solution2: ");
result = MoreThanHalfNum_Solution2(copy, length);
if(result == expectedValue && g_bInputInvalid == expectedFlag)
printf("Passed.\n");
else
printf("Failed.\n");
delete[] copy;
}
// 存在出现次数超过数组长度一半的数字
void Test1()
{
int numbers[] = {1, 2, 3, 2, 2, 2, 5, 4, 2};
Test("Test1", numbers, sizeof(numbers) / sizeof(int), 2, false);
}
// 不存在出现次数超过数组长度一半的数字
void Test2()
{
int numbers[] = {1, 2, 3, 2, 4, 2, 5, 2, 3};
Test("Test2", numbers, sizeof(numbers) / sizeof(int), 0, true);
}
// 出现次数超过数组长度一半的数字都出现在数组的前半部分
void Test3()
{
int numbers[] = {2, 2, 2, 2, 2, 1, 3, 4, 5};
Test("Test3", numbers, sizeof(numbers) / sizeof(int), 2, false);
}
// 出现次数超过数组长度一半的数字都出现在数组的后半部分
void Test4()
{
int numbers[] = {1, 3, 4, 5, 2, 2, 2, 2, 2};
Test("Test4", numbers, sizeof(numbers) / sizeof(int), 2, false);
}
// 输入空指针
void Test5()
{
int numbers[] = {1};
Test("Test5", numbers, 1, 1, false);
}
// 输入空指针
void Test6()
{
Test("Test6", nullptr, 0, 0, true);
}
int main(int argc, char* argv[])
{
Test1();
Test2();
Test3();
Test4();
Test5();
Test6();
return 0;
}
Partition函数(在给定的数组中随机的选择一个数字并将该数字放到该在的位置),以及该函数在快排中的应用。(没看懂partition函数)
(1)RandomInRange用来生成一个在start和end之间的随机数。
(2)函数Swap的作用是交换两个数字。
//Partition
int Partition(int data[],int length,int start,int end)
{
if(data==nullptr||lenght<=0||start<0||end>=length)
throw new std::exception("Invalid Parameters");
int index = RandomInRange(start,end);
Swap(&data[index],&data[end]);
int samll = start-1;
for(index=start;index<end;index++)
{
if(data[index]<data[end])
{
small++;
if(small!=index)
Swap(&data[index],&data[small]);
}
}
samll++;
Swap(&data[small],&data[end]);
return small;
}
void QuickSort(int data[],int length,int start,int end)
{
if(start==end)
return;
int index=Partition(data,length,start,end);
if(index>start)
QuickSort(data,length,,start,index-1);
if(index<end)
QuickSort(data,length,index+1,end);
}
partition函数的第二种方法
int Partition(int A[],int left,int right)
{
int temp = A[left];
while(left<right)
{
while(left<right&&A[right]>temp) //反复左移right
right--;
A[left]=A[right];
while(left<right&&A[left]<=temp) //反复右移left
left++;
A[right] = A[left];
}
A[left] = temp;
return left; //返回相遇的下标
}
void QuickSort(int A[],int left,int right)
{
if(A==null)
return ;
if(left<right)
{
int pos=Partition(A,left,right);
QuickSort(A,left,pos-1);
QuickSort(A,pos+1,right);
}
}