相关疑难解决方法(0)

为什么这个算法的Big-O N ^ 2*log N.

将数组a从[0]填充到[n-1]:生成随机数,直到得到一个尚未包含在先前索引中的数字.

这是我的实施:

public static int[] first(int n) {
    int[] a = new int[n];
    int count = 0;

    while (count != n) {
        boolean isSame = false;
        int rand = r.nextInt(n) + 1;

        for (int i = 0; i < n; i++) {
            if(a[i] == rand) isSame = true;
        }

        if (isSame == false){
            a[count] = rand;
            count++;
        }
    }

    return a;
}
Run Code Online (Sandbox Code Playgroud)

我以为它是N ^ 2,但它显然是N ^ 2logN,我不确定何时考虑日志功能.

java algorithm big-o permutation

15
推荐指数
2
解决办法
4064
查看次数

标签 统计

algorithm ×1

big-o ×1

java ×1

permutation ×1