
题目描述
一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法
思路解析
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
public class { public int jumpFloor(int target){ if(target == 1) return 1; if(target == 2) return 2; int firstStep = 1; int secondStep = 2; int result = 0; for(int i = 3; i <= target; i++){ result = firstStep + secondStep; firstStep = secondStep; secondStep = result; } return result; } }
|
近期评论