Please enable Javascript to view the contents

剑指office(十)矩形覆盖

 ·  ☕ 1 分钟  ·  🎅 YSL

题目描述

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

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

img

示例1

输入

4

返回值

5
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class Solution {
public:
    int rectCover(int number) {
        if(number<=2)
            return number;
        int first = 1;
        int second = 2;
        int result = 0;
        for(int i = 3;i<=number;i++)
        {
            result = first + second;
            first = second;
            second = result;
        }
        return result;
    }
};