面试题29:数组中出现次数超过一半的数字
来源:互联网 发布:长沙源码信息 编辑:程序博客网 时间:2024/04/29 13:22
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;elsecout << "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;elsecout << "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++;elsetimes--;}if (!CheckMoreThanHalf(numbers, length, result))result = 0;return result;}
结果:
0 0
- 面试题:数组中出现次数超过一半的数字
- 面试题:数组中出现次数超过一半的数字
- 面试题29:数组中出现次数超过一半的数字
- [剑指offer][面试题29]数组中出现次数超过一半的数字
- 数组中出现次数超过一半的数字(面试题 29)
- 剑指Offer:面试题29 数组中出现次数超过一半的数字
- 面试题29 数组中出现次数超过一半的数字
- 剑指offer 面试题29—数组中出现次数超过一半的数字
- 《剑指Offer》学习笔记--面试题29:数组中出现次数超过一半的数字
- 面试题29:数组中出现次数超过一半的数字
- 面试题29:数组中出现次数超过一半的数字
- 【剑指Offer学习】【面试题29 :数组中出现次数超过一半的数字】
- 面试题29_数组中出现次数超过一半的数字
- 剑指Offer面试题29(java版):数组中出现次数超过一半的数字
- 面试题29数组中出现次数超过一半的数字
- 剑指offer面试题29-数组中出现次数超过一半的数字
- 面试题29:数组中出现次数超过一半的数字
- 面试题29:数组中出现次数超过一半的数字
- iis 访问需要密码解决
- 通道类型
- ButterKnife的常用方法
- 玩转Easy UI框架的artDialog(三)
- kernel 3.10内核源码分析--hung task机制
- 面试题29:数组中出现次数超过一半的数字
- Java 打印n行杨辉三角数据
- 【VS开发】win7下让程序默认以管理员身份运行
- 用Jplayer做的一个带动画的播放器
- 最小树形图
- 小希的礼物
- Objective c中的锁 NSLock,NSConditionLock,NSRecursiveLock
- 整数在内存中的表达
- 复杂度nlog(n)之归并排序