一、题目

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

二、解答思路

      如果第一步选择竖方向填充,则剩下的填充规模缩小为n-1;

    剑指offer十之矩形覆盖

     如果第一步选择横方向填充,则剩下的填充规模缩小为n-2,因为第一排确定后,第二排也就确定了。

    剑指offer十之矩形覆盖

    因此,递归式为:

    tectCover(n)= tectCover(n-1)+ tectCover(n-2);

    边界条件为:

    当n=0时, 总共有0种方法;

    当n=1时, 总共有1种方法;

    当n=2时, 总共有2种方法;

3、代码

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 {
            return RectCover(target-2)+ RectCover(target-1); //递归调用
        }
    }
}
View Code

相关文章:

  • 2021-06-20
  • 2022-12-23
  • 2022-12-23
  • 2021-10-27
  • 2021-07-29
  • 2022-02-15
  • 2022-01-24
  • 2022-12-23
猜你喜欢
  • 2021-12-04
  • 2022-01-03
  • 2021-04-04
  • 2021-07-05
  • 2021-06-08
  • 2021-07-17
相关资源
相似解决方案