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);
        }
    }
}
复制代码

转载于:https://juejin.im/post/5cf5e8db51882578b902475b

猜你喜欢

转载自blog.csdn.net/weixin_34050519/article/details/91457430
今日推荐