题目描述
一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
分类:数组
解法1:本质也是斐波那契数列
- package test;
-
- public class Test3 {
-
- public int JumpFloor(int target) {
- if(target==1){
- return 1;
- }
- int arr[] = new int[2];
- arr[0] = 1;
- arr[1] = 2;
- int temp = 0;
- for(int i=3;i<=target;i++){
- temp = arr[1];
- arr[1] = arr[0] + arr[1];
- arr[0] = temp;
- }
- return arr[1];
- }
-
- public static void main(String[] args) {
- Test3 t = new Test3();
- System.out.println(t.JumpFloor(30));
- }
- }
也可以使用递归:
- public class Solution {
- public int JumpFloor(int target) {
- if(target==1) return 1;
- if(target==2) return 2;
- return JumpFloor(target-1)+JumpFloor(target-2);
- }
- }
原文链接 http://blog.csdn.net/crazy__chen/article/details/44985023