Please enable Javascript to view the contents

剑指office(八)跳台阶

 ·  ☕ 1 分钟  ·  🎅 YSL

题目描述

一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果)。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class Solution {
public:
    int jumpFloor(int number) {
        if(number<=2)
            return number;
        int first = 1;
        int second = 2;
        int result = 0;
        for(int i = 2;i<number;i++)
        {
            result = first + second;
            first = second;
            second = result;
        }
        return result;
    }
};