gpt4 book ai didi

c# - 与时间复杂度有关的疑问!

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:33:03 24 4
gpt4 key购买 nike

考虑以下片段:

for(i = n-1; i >= 0; i--)
{
if(str[i] == ' ')
{
i += 2; // I am just incrementing it by 2, so that i can retrieve i+1
continue;
}
// rest of the code with many similar increments of i
}

假设循环永远不会变成无穷大,并且如果我遍历具有许多这样的递增和递减的循环,我确信复杂度不会是 N 阶或 N 阶。但是这种解决方案有任何普遍的复杂性吗?

P.S:我知道这是最糟糕的代码,但仍想尝试一下 :-)

最佳答案

如果字符串中有空格,这是一个无限循环(无限复杂)。由于您正在使用 continue,因此它会返回到 for 并从 i+2 开始。

关于c# - 与时间复杂度有关的疑问!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5233125/

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