1 Star 0 Fork 3.1K

沐瑶 / LearningNotes

forked from 陌溪 / LearningNotes 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README.md 892 Bytes
一键复制 编辑 原始数据 按行查看 历史
陌溪 提交于 2020-06-25 14:37 . add blog

矩形覆盖

来源

https://www.nowcoder.com/practice/72a5a919508a4251859fb2cfb987a0e6

描述

我们可以用2 X 1 的小矩形横着或者竖着去覆盖更大的矩形。请问用n个21的小矩形无重叠地覆盖一个2n的大矩形,总共有多少种方法?

比如n=3时,2*3的矩形块有3种覆盖方法:

image-20200625090357495

思考

相当于转变成了斐波那契数列,得到通式

f(n) = f(n-1) + f(n-2)

n = 1: 1
n = 2: 3

代码

class Solution:
    def rectCover(self, number):
        # write code here
        if number == 0:
            return 0
        if number == 1:
            return 1
        if number == 2:
            return 2
        
        a = 1
        b = 2
        for i in range(3, number + 1):
            b = a + b
            a = b - a
        return b
1
https://gitee.com/muyao_vip/LearningNotes.git
git@gitee.com:muyao_vip/LearningNotes.git
muyao_vip
LearningNotes
LearningNotes
master

搜索帮助