剑指offer---10---动态规划:矩形覆盖

 
题意
我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?
 
分析
动态规划
和前面几道题都是一样的
00000
00000
设想这么一个矩形,n=3的情况等于:
1.n=2加上一列
2.n=1加上两行
 
 
代码
public class Solution {
    public int RectCover(int target) {
        if(target<=0)return 0;
        int pre=1;
        int last=1;
        int temp=last;
        int index=2;
        while(index<=target){
            temp=last;
            last = pre+last;
            pre = temp;
            index++;
        }
        return last;
    }
}

猜你喜欢

转载自www.cnblogs.com/buptyuhanwen/p/9376962.html