数学推导出来,就可以保证时间空间都是 O(1) 了

0,1,2,我们直接返回number即可,其他的数字返回2的(number-1)次方。

python实现

class Solution:
    def jumpFloorII(self , number: int) -> int:
        # write code here
        if number <= 2:
            return number
        return 2**(number-1)

c++实现

class Solution {
public:
    int jumpFloorII(int number) {
        if(number <= 2) return number;
        return 1<<(number-1);
    }
};