剑指8:跳台阶

题目描述

一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果)。
class Solution {
public:
    int jumpFloor(int number) {
        if (number<=0){
            return 0;
        }
        if (number==1){
            return 1;
        }
        if (number==2){
            return 2;
        }
        int first=1,second=2,third=0;
        for (int i=3;i<=number;i++){
            third=first+second;
            first=second;
            second=third;
        }
        return third;
    }
};
# -*- coding:utf-8 -*-
class Solution:
    def jumpFloor(self, number):
        # write code here
        a,b=0,1
        n=0
        while n<=number:
            n+=1
            a,b=b,a+b
            
        return a
            
           
原文地址:https://www.cnblogs.com/hrnn/p/13415938.html