题解 | #跳台阶扩展问题#
跳台阶扩展问题
http://www.nowcoder.com/practice/22243d016f6b47f2a6928b4313c85387
跳至n阶
f1=1;
f2=2;
fn=f1+f2+....f(n-1)+1;//1代表直接跳n步
fn=Sum(0,n-1)+1;
Sum(0,n-1)在遍历的时候用一个变量保存,再+1即为所求
public class Solution {
public int jumpFloorII(int target) {
int t0=1,t1=2,sum=0,sum1=0;
if(target==1||target==2){
return target;
}else{
for(int i=3;i<=target;i++){
sum1=t0+t1;
sum=sum1+1;
t0=sum1;
t1=sum;
}
}
return sum;
}
}