10剑指OFFER之变态跳台阶+矩形覆盖

注意

不要数错了

自己的解法:(变态跳台阶)
class Solution {
public:
    int jumpFloorII(int number) {
        
        if(number<=2)
            return number;
        int s=2;
        for(int i=2;i
自己的解法:(矩形覆盖)
class Solution {
public:
    int rectCover(int number) {
        
        if(number<=2)
            return number;
        return FibonacciCore(number);
        
    }
    
    int FibonacciCore(int number)
    {
        int a[256]={0};
        a[2] =2;
        a[3] =3;
        for(int i=4;i<=number;i++)
        {
            a[i]=a[i-2]+a[i-1];
        }
        return a[number];
    }
};
//2X1的矩形:1
//2X2的矩形:2
//2X3的矩形:3
//2X4的矩形:先竖着:3 ,横着:2
//2X5的矩形:先竖着:5 ,竖着:3
//斐波那契数列啊

标准答案:(变态跳台阶)
class Solution {
public:
    int jumpFloorII(int number) {
        
        if(number ==0)
            return 1;
        //数学归纳法得出来的2^(n-1)
        return pow(2,number-1);
    }
};
标准答案:(矩形覆盖)
class Solution {
public:
    int rectCover(int number) {
        
        int Result[3] = {0,1,2};
        if(number<=2)
            return Result[number];
        int FibOne =1;
        int FibTwo =2;
        int FibResult = 0;
        for(int i = 3;i<=number;i++)
        {
            FibResult = FibOne+FibTwo;
            FibOne = FibTwo;  
            FibTwo = FibResult;
        }
        return FibResult;
    }
};

你可能感兴趣的:(10剑指OFFER之变态跳台阶+矩形覆盖)