您的位置:首页 > Web前端

剑指offer 跳台阶

2016-04-14 12:18 288 查看
题目描述

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

/**
* 第n阶,有两种跳法,从n-1跳上来,或者从n-2跳上来
* f(n)=f(n-1)+f(n-2)
* f(1) = 1;
* f(2) = 2;
*
* @param target
* @return
*/
public int JumpFloor(int target) {
if (target < 1) {
return 0;
}
if (target == 1) {
return 1;
}
if (target == 2) {
return 2;
}
int fx = 1;
int fy = 2;
int fn = 0;
for (int i = 3; i <= target; ++i) {
fn = fy + fx;
fx = fy;
fy = fn;
}
return fn;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息