面试题:矩形覆盖

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

思路:用数学归纳法 可知 是Fibonacci数列(使用递归或者循环)

代码:循环

public class Solution {
    public int RectCover(int target) {
        if(target<=0){
            return 0;
        }else if(target==1){
            return 1;
        }else if(target==2){
            return 2;
        }else{
            int a=1;
            int b=2;
            int f=0;
            for(int i=3;i<=target;i++){
                f=a+b;
                a=b;
                b=f;
            }
            return f;
        }
    }
}
原文地址:https://www.cnblogs.com/Aaron12/p/9524728.html