青蛙跳-算法

本文介绍了两种青蛙跳台阶的问题及解决方案。第一种情况下,青蛙每次可以跳1级或2级台阶,求达到n级台阶的所有可能跳法。第二种情况允许青蛙跳任意级台阶,同样求所有可能的跳法。文章提供了完整的Java代码实现。

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

/**
*一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
*/
public
class Solution { public int JumpFloor(int target) { if(target<=2){ return target; } int start=1; int end =2; end=this.sum(start,end,3,target); return end; }
/**
*分析出数据: 1 2 3 5 8
*start  前一个台阶跳法

*end   当前台阶跳法
*total 总台阶
*/
public int sum(int start,int end ,int i ,int total){ if(i<=total){ int tmp=start; start=end; end+=tmp; end=this.sum(start,end,i+1,total); } return end; } }

/**
*一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法
*/

/**
*数据分析1 2 4
*/
public class Solution {
    public int JumpFloorII(int target) {
        return 1<<(target-1);
    }
}

 

转载于:https://www.cnblogs.com/excite/p/4589312.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值