题目描述
一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
思路:f(n) = 1 + f(1) + f(2) + ...+ f(n-1) ,n>2.
f(1) = 1,f(2)=2
public class Solution {
public int JumpFloorII(int target) {
if(target<3) return target;
int sum = 1 + 2;//用来记录前n-1项的和,f(1)+f(2)
int index = 2;
int temp = 0;
while(index!=target){
temp = sum + 1;//f(n)
sum += temp;
index++;
}
return temp;
}
}