leetcode刷题python之爬楼梯

mac2024-04-01  29

这个题符合斐波那契数列 使用了斐波那契公式,直接求更快更节省空间 思路来源:https://leetcode-cn.com/problems/climbing-stairs/solution/pa-lou-ti-by-leetcode/

class Solution: def climbStairs(self, n: int) -> int: sqrt5 = 5 ** 0.5 return int((1 / sqrt5) * ( ((1 + sqrt5) / 2) ** (n + 1) - ((1 - sqrt5) / 2) ** (n + 1) ))
最新回复(0)