Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我的算法的时间复杂度是多少?_Python_Python 3.x_Time Complexity - Fatal编程技术网

Python 我的算法的时间复杂度是多少?

Python 我的算法的时间复杂度是多少?,python,python-3.x,time-complexity,Python,Python 3.x,Time Complexity,该算法是为LeetCode的爬楼梯问题编写的,运行速度为20ms。我相信时间复杂度是O(n),因为这个链接说O(n)是当运行时间随着输入的大小变得更大而线性增加时,我认为这就是这里发生的事情 我想知道是否有人能向我解释一下这里的时间复杂性,以及为什么会这样。如果我不在线性时间的话,任何关于如何让它进入线性时间的额外信息也会有所帮助 这是我的密码: class Solution: def climbStairs(self, n: int) -> int: if n =

该算法是为LeetCode的爬楼梯问题编写的,运行速度为20ms。我相信时间复杂度是O(n),因为这个链接说O(n)是当运行时间随着输入的大小变得更大而线性增加时,我认为这就是这里发生的事情

我想知道是否有人能向我解释一下这里的时间复杂性,以及为什么会这样。如果我不在线性时间的话,任何关于如何让它进入线性时间的额外信息也会有所帮助

这是我的密码:

class Solution:
    def climbStairs(self, n: int) -> int:
        if n == 0: return 1

        else:
            temp = []

            for i in range(0, n + 1):
                temp.append(i)
                if temp[i] == 0:
                    temp[i] = 1
                elif temp[i] > 1:
                    temp[i] = temp[i-1] + temp[i-2]

            return temp[-1]

每次迭代都要做固定的工作量(假设
append
需要固定的时间);有N次迭代,因此是O(N)次。

为什么要使用
1*单元格
?我现在明白了。这是毫无意义的。算法是什么?它解决了Leet代码上的爬楼梯问题。以下是问题的链接:@AMC