面试题29:数组中出现次数超过一半的数字

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;
}

结果:


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值