跳台阶

 

## 方法一: 递归

class Solution:
    def jumpFloor(self, number):
        # write code here
        if number == 2 or number == 1:
            return number

        return self.jumpFloor(number-1) + self.jumpFloor(number-2)

 

 

 ## 方法二:循环实现,避免阶数过大造成时间超限

# -*- coding:utf-8 -*-
class Solution:
    def jumpFloor(self, number):
        # write code here
        if number == 2 or number == 1:
            return number
        n, m = 1, 2
        for i in range(number-2):
            result = m + n
            n, m = m, result
        return result

 

posted @ 2020-01-05 20:35  公众号海哥python  阅读(165)  评论(0编辑  收藏  举报