剑指offer29--数组中出现超过一半的数字

来源:互联网 发布:mac解压zip文件 编辑:程序博客网 时间:2024/04/29 22:11
public class MoreThanHalfNum {
/*
* 数组中有一个数字出现的次数超过数组长度的一般,求这个数字
*/
public Integer moreThanHalfNum(int[] array)
{
if(array==null)
return null;
Integer number=null;
int count=0;
Integer resultInteger=null;
for(int i=0;i<array.length;i++)
{
if(number==null)
{
number=array[i];
count++;
}
else
{
if(array[i]!=number)
if(count==0)
{
number=array[i];
count=1;
}
else 
count--;
else
count++;
}
if(count==1)
resultInteger=number;
}
if(checkMoreThanHalf(array, resultInteger))
return resultInteger;
else
return null;
}
private boolean checkMoreThanHalf(int[] array,int number)
{
int times=0;
for(int i=0;i<array.length;i++)
{
if(array[i]==number)
times++;
}
if(times*2<=array.length)
return false;
else 
return true;

}
}
0 0
原创粉丝点击