众数问题
来源:互联网 发布:知乎 江歌 贫困生 编辑:程序博客网 时间:2024/04/23 19:31
How to find if a number is present >= (n / 2) times in an array of size
n?
n?
关于这个题目,有没有时间复杂度是O(n),空间复杂度是O(1)的解?
solution by swanswan @ MitBBS
int number=a[0];int count=1;for (int i=1; i<n; i++) {if (a[i]!=number) { count--; if (count==0) { number = a[i]; count = 1; }} else count++;}return number;
Simple modification is to add another loop to check if there's a number
which has appeared [n/2] times.
【 在 iverson1407 (iverson1407) 的大作中提到: 】
: 这个只对绝对众数有效。也就是大于n/2的时候,但是等于n/2的时候,是无效的。
- 众数问题
- 众数问题
- 众数问题
- 众数问题
- 众数问题
- 众数问题
- 众数问题
- 众数问题
- 众数问题
- 众数问题
- 众数问题
- 众数问题
- 众数问题
- 众数问题
- 众数问题
- 众数问题
- 众数问题
- 众数问题
- 信号丢失问题
- POJ-1977 很好的矩阵乘法题..
- C/C++中指针和引用之相关问题研究
- CSDN 论坛无名游戏 C++版
- 21种工作流模式
- 众数问题
- GS语言更新到V3.0
- The way to Install qemu and kvm (qemu-kvm) in suse 11
- SQL SERVER 2012 DQS练习-数据净化
- 如何退出Android应用程序
- SQL SERVER 2012 DQS练习-数据匹配
- Android学习实现画廊效果
- xcode4.2中objective的说明,有空的时候看看
- Android 4.0 x86安装教程 附带联网参数详细设置