Algorithm complexity: if/else under for loop

Ama*_*eur 3 complexity-theory for-loop if-statement

I am wondering if in a situation like the following (an if/else statement under a for loop) the complexity would be O(n) or O(n^2):

for character in string:
    if character==something:
        do something
    else:
        do something else.
Run Code Online (Sandbox Code Playgroud)

Thank you!

小智 5

这将是

如果“做某事”和“做某事”为O(1),则为O(n)

如果“做某事”和“做某事”为O(n),则为O(n ^ 2)

基本上,for循环的复杂度取决于it组件和no的复杂度。的循环。

  • 天哪-请有人编辑这个...为什么这是标记的答案? (6认同)
  • 好吧,这个答案令人困惑。第一行和第二行都有相同的“if do.. and something else..” (3认同)