大O表示法用于两个非嵌套循环

编程入门 行业动态 更新时间:2024-10-23 02:05:10
本文介绍了大O表示法用于两个非嵌套循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

对于两个未嵌套的for循环,Big O表示法是什么?

示例:

for(int i=0; i<n; i++){ System.out.println(i); } for(int j=0; j<n; j++){ System.out.println(j); }

解决方案

线性

O(n) + O(n) = 2*O(n) = O(n)

您有多少个非嵌套循环(如果该数目是一个常数,并且不依赖于n)并不重要,复杂度将是线性的,并且将等于循环中的最大迭代次数. /p>

What would the Big O notation be for two for loops that aren't nested?

Example:

for(int i=0; i<n; i++){ System.out.println(i); } for(int j=0; j<n; j++){ System.out.println(j); }

解决方案

Linear

O(n) + O(n) = 2*O(n) = O(n)

It does not matter how many non nested loops do you have (if this number is a constant and does not depends on n) the complexity would be linear and would equal to the maximum number of iterations in the loop.

更多推荐

大O表示法用于两个非嵌套循环

本文发布于:2023-11-30 02:47:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648462.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   两个

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!