1 Star 0 Fork 1.5K

沐瑶 / LearningNotes

forked from 陌溪 / LearningNotes 
Create your Gitee Account
Explore and code with more than 6 million developers,Free private repositories !:)
Sign up
Clone or download
README.md 892 Bytes
Copy Edit Web IDE Raw Blame History
陌溪 authored 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

Comment ( 0 )

Sign in for post a comment

1
https://gitee.com/muyao_vip/LearningNotes.git
git@gitee.com:muyao_vip/LearningNotes.git
muyao_vip
LearningNotes
LearningNotes
master

Search

102255 3a0e046c 1850385 102255 7aaa926c 1850385