题解 | #斐波那契数列#

斐波那契数列

https://www.nowcoder.com/practice/c6c7742f5ba7442aada113136ddea0c3

class Solution:
    def Fibonacci(self , n: int) -> int:
        tmp1 = 1
        tmp2 = 1

        for i in range(2, n):
            tmp = tmp1 + tmp2
            tmp1 = tmp2
            tmp2 = tmp
        
        return tmp2

时间复杂度为O(N),空间复杂度为O(1)。

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务