zoukankan      html  css  js  c++  java
  • 矩形覆盖

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

    思路:

     
     
                   
                   

    观察上面的矩形,如果使用2x1的矩形来覆盖2x8的矩形的话,设有f(n)中覆盖放法。首先,第一个2x1的矩形有两种放法,第一种,竖着放,剩下的部分有f(7)种,第二种横着放,剩下的部分有f(6)种。则一共有f(8) = f(7) + f(6)。很明显这又是著名的Fibonacci数列。

    实现代码:

    public class Solution {
        public int RectCover(int target) {
            if(target == 0)
                return 1; //2*0的矩形  后台数据竟然是1
            if(target == 1)
                return 1;
            int[] ret = new int[target+1];
            ret[0] = 1;
            ret[1] = 1;
            ret[2] = 2;
            for(int i=3; i<=target; i++) {
                ret[i] = ret[i-1] + ret[i-2];
            }
            return ret[target];
        }
    }
  • 相关阅读:
    BeautifulSoup
    requests
    安装xpath helper
    取消搜狗输入法的快捷键
    numpy初识 old
    Jupyter Notebook 快捷键
    安装numpy、matplotlib
    JavaScript 继承 -JavaScript高级程序设计
    mac /windows
    unicode 地址
  • 原文地址:https://www.cnblogs.com/wxisme/p/5236365.html
Copyright © 2011-2022 走看看