java - 寻找 BigO - 一个 while 循环嵌套两个 for 循环

标签 java arrays algorithm big-o

谁能告诉我以下的 BigO:

public void doFoo(int n) {
    int pass = 1;
    while (pass <= n) {
        for (int index = 0; index < n; index++) {
            for (int count = 1; count < 10; count++) {
                if (arr1[pass] == arr2[index]) {
                    arr1[pass]++;
                }
            }
        }
        pass = pass + 1;
    }
}

我得出了 O(n2) 的结论,但我想澄清一下它是否正确。感谢帮助。

最佳答案

答案是0(n^2) ..逻辑如下:

 while (pass <= n) {                     // executes n times
        for (int index = 0; index < n; index++) {      // executes n times
            for (int count = 1; count < 10; count++) { // always executes 9 times.. irrespective of "n". So. it doesn't matter.
                if (arr1[pass] == arr2[index]) {
                    arr1[pass]++;
                }
            }
        }

关于java - 寻找 BigO - 一个 while 循环嵌套两个 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28760831/

相关文章:

java - 为什么有必要明确地说一个 lambda 是 Consumer 来使用 andThen 方法?

java - 检查变量是否包含数组

php - 仅从另一个数组的值创建关联数组 - PHP

python - 我如何获得由 Pandas 数据框中的值表示的两个连接代码的聚合百分比

java - Eclipse 插件内的图像路径

java - 使用最新的 Jackson Parser 库在 Android 设备上解析 JSON

java - 由于 "Negative seek offset",Cargo Maven 插件无法下载和解压 Jetty zip

python - 如何将 numpy 数组列表拆分为 n 个子列表

algorithm - 来自 3D 点集合的边界框

Java : Taking 2 array find if the element that exists in both array return true if exist else false