java数据结构和算法(09)矩形覆盖

  • 咱们能够用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?完成以下代码:
public class Solution {
    public int RectCover(int target) {

    }
}
复制代码
  • 思路:数学概括法
    • n=1时,1种
    • n=2时,2种
    • n=3时,3种
    • n=4时,5种
  • 详细分析看下图

  • 代码
public class Solution {
    public int RectCover(int target) {
         if(target<1){
            return 0;
        }else if(target==1){
            return 1;
        }else if(target==2){
            return 2;
        }else {
            return RectCover(target-1)+RectCover(target-2);
        }
    }
}
复制代码
相关文章
相关标签/搜索