gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:23:54 25 4
gpt4 key购买 nike

谁能告诉我以下的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com