我的将军生涯 吴妈令不小心摔了一下,最后面一个数字只可以看到一半,我该怎么办?有地方可有修复吗?

数组中有一个数字出现的次数超过了数组长度的一半,找出这个数
这个算法的时间复杂度是O(n),另外用了两个辅助变量。
k用于临时存储数组中的数据,j用于存储某个数出现的次数。
开始时k存储数组中的第一个数,j为0,如果数组出现的数于k相等,则j加1,否则就减1,如果j为0,就把当前数组中的数赋给k
因为指定的数出现的次数大于数组长度的一半,所有j++与j--相抵消之后,最后j的值是大于等于1的,k中存的那个数就是出现最多的那个数。
下面这个算法只适合数组中数组中某个数的出现次数超过数组长度一半的数组,符合题意。
1 #include&stdio.h&
2 #include&stdlib.h&
3 int Search(int A[],int len)
if(NULL==A || len&=0)
return -1;
int k, j=0;
for(int i=0;i&++i)
if(k==A[i])
28 void main(){
int len=10;
int a[10]={4,5,5,2,3,5,2,5,5,5};
int result=Search(a,len);
printf("%d\n",result);
public class Search {
public static void main(String[] args) {
//System.out.println("Hello World!");
Integer []a={4,5,5,2,3,5,2,5,5,5};
Integer len= a.
Integer result = search(a,len);
System.out.println(result);
public static Integer search(Integer A[],Integer len){
if(A==null || len&=0)
return -1;
Integer k=null, j=0;
for(Integer i=0;i&++i)
if(k==A[i])
参考自:http://blog.csdn.net/iefswang/article/details/7581613
感谢原作者
阅读(...) 评论()

我要回帖

更多关于 我的将军生涯 吴妈 的文章

 

随机推荐