小编Mah*_*ddy的帖子

查找数组在小于O(n ^ 2)内重复的次数

我写的示例代码.但这是n ^ 2

int a[]={1,4,1,5,2,2,4,3,4,1};
int b[][]=new int[5][2];
int i,j,k=0,count=1;
boolean temp=false;
for(i=0;i<a.length;i++)
{
    for(j=0;j<5;j++)
    {
        if(a[i]==b[j][0])
        {   temp=true;
            b[j][1]++;
            break;
        }
    }

    if(temp==false)
    {
        b[k][0]=a[i];
        b[k][1]=1;
        k++;    
    }
    temp=false;
}
for(i=0;i<5;i++)
{
    for(j=0;j<1;j++)
    {
    System.out.println(b[i][j]+" is repeated "+b[i][j+1]+" times");
    }
}
Run Code Online (Sandbox Code Playgroud)

java algorithm complexity-theory

3
推荐指数
2
解决办法
9432
查看次数

标签 统计

algorithm ×1

complexity-theory ×1

java ×1