1.数组中有一个数字的出现的次数超过数组长度的一半,找出这个数字,例如长度为9的数组{1,2,3,2,2,2,5,4,2},其中2出现了5次,超过了数组长度的一半,因此输出为2.
结果:
分析:有两种思路,
一种思路是:如果数组中有一个数字出现的次数超过了一半的话,那么将这个数组排序之后,中间位置的数就是我们要找的数,这样一来我们可以借鉴快速排序的方法来找出这个数字。
另一种思路就是:重头到尾遍历一遍数组,并设置一个计数,如果下一个数字和当前额数字相同,则计数加1,如果不同则减1,为0的时候,我们保存下一个数字。如果存在这种出现次数超过数组长度一半的数字的话,那么要找的数字肯定是最后一个把数字设置成1的数字。
源码:
/**
* 功能说明:Description
* 作者:K0713
* 日期:2016-7-21
**/
#include<iostream>
using namespace std;
bool g_bInputInvalid = false;
//产生随机数
int RandomInRange(int min, int max);
//交换两个数值
void Swap(int* num1, int* num2);
//基于快速排序的划分
int Partition(int data[], int length, int start, int end);
//验证输入数据的合法性
bool CheckInvalidArray(int* numbers, int length);
//检查结果出现的次数是否正确
bool CheckMoreThanHalf(int* numbers, int length, int number);
//基于快排的思想
int MoreThanHalfNum_Solution1(int* numbers, int length);
//基于计数的思想
int MoreThanHalfNum_Solution2(int* numbers, int length);
int main()
{
int numbers[] = { 1, 2, 3, 2, 2, 2, 5, 4,2};
cout << "solution2: " << endl;
int result = MoreThanHalfNum_Solution2(numbers, sizeof(numbers) / sizeof(int));
if (result == 0)
cout << "can not find the number that satisfies the requirement" << endl;
else
cout << "the result is :" << result << endl;
cout << "solution1: " << endl;
result = MoreThanHalfNum_Solution1(numbers, sizeof(numbers) / sizeof(int));
if (result == 0)
cout << "can not find the number that satisfies the requirement" << endl;
else
cout << "the result is :" << result << endl;
system("PAUSE");
return 0;
}
int Partition(int data[], int length, int start, int end)
{
if (data == NULL || length <= 0 || start < 0 || end >= length)
throw new std::exception("Invalid Parameters");
int index = RandomInRange(start, end);
Swap(&data[index], &data[end]);
int small = start - 1;
for (index = start; index < end; ++index)
{
if (data[index] < data[end])
{
++small;
if (small != index)
Swap(&data[index], &data[small]);
}
}
++small;
Swap(&data[small], &data[end]);
return small;
}
int RandomInRange(int min, int max)
{
int random = rand() % (max - min + 1) + min;
return random;
}
void Swap(int* num1, int* num2)
{
int temp = *num1;
*num1 = *num2;
*num2 = temp;
}
bool CheckInvalidArray(int* numbers, int length)
{
g_bInputInvalid = false;
if (numbers == 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 isMoreThanHalf = true;
if (times * 2 <= length)
{
g_bInputInvalid = true;
isMoreThanHalf = false;
}
return isMoreThanHalf;
}
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;
}
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;
}
结果: