《剑指offer》系列 跳台阶(Java)

本文详细解析了跳台阶问题,包括牛客网的跳台阶题目和LeetCode上的剑指Offer10-II.青蛙跳台阶问题。提供了清晰的思路解析和代码实现,对比了牛客和LeetCode在输入范围和是否取模的差异,最后列举了相似题目以加深理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链接

牛客:跳台阶

LeetCode:剑指 Offer 10- II. 青蛙跳台阶问题

思路

和斐波那契那题类似。

代码

牛客:

public class Solution {
    public int JumpFloor(int num) {
        if (num == 0 || num == 1 || num == 2)
            return num;
        int first = 1;
        int second = 2;
        int res = 0;
        for (int i = 3; i <= num; i++) {
            res = first + second;
            first = second;
            second = res;
        }
        return res;
    }
}

LeetCode:

class Solution {
    public int numWays(int n) {
        if (n == 0 || n == 1) {
            return 1;
        }
        int first = 1, second = 1;
        int result = 0;
        for (int i = 2; i <= n; i++) {
            result = (first + second) % 1000000007;
            first = second;
            second = result;
        }
        return result;
    }
}

牛客和 LeetCode 的区别在于 n 的取值范围还有是否取模

相似题

70. 爬楼梯

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值