的时间复杂度嵌套的IF语句
问题描述:
如果我们有“m
”外if-else语句和每个外if-else语句包含“n
” if-else语句,那么这将是代码的时间复杂度?的时间复杂度嵌套的IF语句
例如:
if(Condition 1){
if(Condition 2){
//Do something
}
.
. //'n' inner IF-ELSE statements
.
else{
//Do something else
}
}
.
. //'m' outer IF-ELSE Statements
.
else{
//Do something else
}
答
时间复杂度(最坏情况)将是O(M + N)。它将检查外部其他条件,如果发现真实条件,将检查内部其他条件的n条件。
什么在每个“做某事”或“做别的事情”块中执行?这会影响代码的时间复杂度。 – templatetypedef