zoukankan      html  css  js  c++  java
  • 剑指offer

    题目描述

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

    code:

    分析:该题为一个斐波那契数列,与 https:////www.cnblogs.com/s-star/p/12512501.html 同理

    递归:
    public class Solution {
        public int Fibonacci(int n) {
            if (n <= 1) {
                return n;
            }
            return Fibonacci(n - 1) + Fibonacci(n - 2);
        }
    }

    数组存储:

    public class Solution {
        public int Fibonacci(int n) {
            if (n <= 1) {
                return n;
            }
            int arr[] = new int[n + 1];
            arr[0] = 0;
            arr[1] = 1;
            for (int i = 2; i <= n; i++) {
                arr[i] = arr[i - 1] + arr[i - 2];
            }
            return arr[n];
        }
    }

    动态规划:

    public class Solution {
        public int Fibonacci(int n) {
            if (n <= 1) {
                return n;
            }
            //存储n位置值的变量
            int x = 1;
            //存储n-1位置的变量
            int y = 0;
            for (int i = 2; i <= n; i++) {
                //i位置的值
                x = x + y;
                //i-1位置的值
                y = x - y;
            }
            //返回n位置值
            return x;
        }
    }
  • 相关阅读:
    os和sys模块
    time模块
    collections模块
    re模块
    Python初识一
    Python闭包函数
    压栈
    isinstance()和issubclass()
    匿名函数--lambda函数
    机器学习入门文章
  • 原文地址:https://www.cnblogs.com/s-star/p/12513409.html
Copyright © 2011-2022 走看看