n个数字相加的时间复杂度是多少 [英] What is the time complexity of adding n numbers

查看:100
本文介绍了n个数字相加的时间复杂度是多少的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我必须添加任意数字,例如数字1,12,14,71,83,21 ...那么此操作的时间复杂度是多少?

If I have to add arbitrary numbers like the numbers 1,12,14,71,83,21... then what would be the time complexity of this operation?

我知道两个数字相加为O(1),但是n个数字的列表呢.假设我正在使用最佳的数据结构来存储它们,以免对过程产生任何影响!

I know that adding two numbers is O(1), but how about a list of n numbers. Assuming I'm using the best possible data structure to store them for this purpose, if at all that can have any impact on the process!

提前谢谢!

推荐答案

加2是 O(1),因为操作本身是固定时间,输入是固定的.不管输入什么,该操作将始终节省时间.

Adding 2 numbers is O(1) since the operation itself is constant time and the input is fixed. Regardless of the input, the operation will always take the save time.

移动到项目的集合中,该操作仍然是固定时间,但是现在已执行了多次.输入大小(N)越大,花费的时间就越长,并且增长率将是线性的-对于输入中的每个额外项,该操作将花费1个周期以上.

Moving to a collection of items, the operation is still constant time, but now it's being done multiple times. The larger the input size (N), the longer it will take, and the growth rate will be linear - for each extra item in the input, the operation will take 1 more cycle.

因此,时间复杂度为 O(N).

这篇关于n个数字相加的时间复杂度是多少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆