标签:style blog class code c java
#include<iostream> using namespace std; bool solver(const int a[],const int n, int & num) { if(NULL == a || 0>= n) return false; ////注意,是小写~ int count = 0; int com = a[0]; for(int i = 1;i<n;i++) { if(0 == count) { com = a[i]; count++; } else{ if(a[i] == com) count++; else count--; } } num = com; return true; } int main() { int a[] = {1,2,3,3,3,3,3,3,4}; int temp; solver(a,9,temp); cout<<temp; }
在O(n)时间复杂度内找到出现超过一半的数,布布扣,bubuko.com
标签:style blog class code c java
原文地址:http://www.cnblogs.com/berkeleysong/p/3735545.html