一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个 n 级的台阶总共有多少种跳法(先后次序不同算不同的结果)。
数据范围:
要求:时间复杂度: ,空间复杂度:
class Solution { public: int jumpFloor(int number) { if (number <= 0) { return 0; } if (number == 1) { return 1; } if (number == 2) { return 2; } int first = 1, second = 2, third = 0; for (int i = 3; i <= number; i++) { third = first + second; first = second; second = third; } return third; } };
public class Solution { public int jumpFloor(int target) { if(target < 0) { throw new IllegalStateException(); } int toFloorOneMethods = 1; int toFloorTwoMethods = 2; int result = 0; if(target == 1) { return toFloorOneMethods; } if(target == 2) { return toFloorTwoMethods; } for (int i = 3; i <= target; i++) { result = toFloorOneMethods + toFloorTwoMethods; toFloorOneMethods = toFloorTwoMethods; toFloorTwoMethods = result; } return result; } }
class Solution: dp = [1 for i in range(50)] def jumpFloor(self , number: int) -> int: for i in range(2, number+1): self.dp[i] = self.dp[i-1] + self.dp[i-2] return self.dp[number]
class Solution: def jumpFloor(self , number: int) -> int: a, b, c = 1, 1, 1 for i in range(2, number + 1): a = b b = c c = a + b return c
export function jumpFloor(number: number): number { // write code here if(number <= 0){ return 0; } if(number === 1){ return 1; } if(number === 2){ return 2; } let first:number = 1,second:number = 2 ,third:number = 0; for(let i = 3; i <= number;i++){ third = first + second; first = second; second = third; } return third }
int[] arr = new int[]{0,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368,75025,121393,196418,317811,514229,832040,1346269,2178309,3524578,5702887,9227465,14930352,24157817,39088169,63245986,102334155,165580141}; return arr[target];
public class Solution { // f(n) = f(n-1) + f(n-2), 代表最后一次跳一步和最后一次跳两步的跳法之和 // f(1) = 1, f(2) = 2, 注意不要从f(0)开始, f(0)没有意义 public int jumpFloor(int target) { if (target <= 2) { return target; } int pre1 = 2, pre2 = 1, ans = 0; for (int i = 3; i <= target; i++) { ans = pre1 + pre2; pre2 = pre1; pre1 = ans; } return ans; } }
import java.math.BigDecimal; public class Solution { //第一次的解法:暴力破解、去重(如果不用BigDecimal,数字会存不下),效率一般,超高耗内存 public int jumpFloor(int target) { BigDecimal ans = BigDecimal.ZERO; for(int i=0; i<=target; i++){ for(int j=0; j<=target/2+1; j++){ if(i+2*j==target){ BigDecimal n = dfsOrder(BigDecimal.valueOf((long)(i+j)), BigDecimal.valueOf(2), BigDecimal.valueOf(1)); if(i>1){ n = n.divide(dfsRepeat(BigDecimal.valueOf(i),BigDecimal.ONE,BigDecimal.ONE), 0, BigDecimal.ROUND_DOWN); } if(j>1){ n = n.divide(dfsRepeat(BigDecimal.valueOf(j),BigDecimal.ONE,BigDecimal.ONE), 0, BigDecimal.ROUND_DOWN); } ans = ans.add(n); } } } return ans.intValue(); } /** * 数字的不同的排列结果 * @param n * @return */ public BigDecimal dfsOrder(BigDecimal n, BigDecimal start, BigDecimal ans){ if(start.compareTo(n) > 0){ return ans; } ans = start.multiply(ans); return dfsOrder(n, start.add(BigDecimal.valueOf(1)), ans); } //去除重复计算的方式 public BigDecimal dfsRepeat(BigDecimal num, BigDecimal start, BigDecimal ans){ if(start.compareTo(num) > 0){ return ans; } ans = start.multiply(ans); return dfsRepeat(num, start.add(BigDecimal.valueOf(1)), ans); } }
//第二种解法 斐波那契数列 public int jumpFloor(int target) { return fibonacci(target, 1, 1); // return fibonacci(target); } // //普通递归,效率差,耗内存 // public int fibonacci(int n){ // if(n<=1){ // return 1; // } // return fibonacci(n-1)+fibonacci(n-2); // } //尾递归,效率高,耗内存 public int fibonacci(int n, int pre, int curr){ if(n <= 1){ return curr; } return fibonacci(n-1, curr, pre + curr); }
//轮询大法 public int jumpFloor(int target) { if(target <= 3){ return target; } int pre = 1; int curr = 1; while(target >= 2){ int temp = curr; curr = pre + curr; pre = temp; target--; } return curr; }
# -*- coding:utf-8 -*- class Solution: def jumpFloor(self, number): # write code here # 迭代写法 if number <= 1: return number pre_n = 0 n = 1 for _ in range(number): total = pre_n + n pre_n = n n = total return total # 递归写法 内存溢出 # if number <= 2: # return number # return self.jumpFloor(number-1)+self.jumpFloor(number-2)
function jumpFloor(number) { // 定义一个数组,用空间换时间 var tempArr = [,1,2] // 使用递归,第n个台阶有两种方式,跳一个从n-1跳上来,跳两个从n-2跳上来 function tempFun (number) { if (tempArr[number]) { return tempArr[number] } else { tempArr[number] = tempFun(number - 1) + tempFun(number - 2) return tempArr[number] } } return tempFun (number) } module.exports = { jumpFloor : jumpFloor };
public class Solution { int jumpFloor(int number) { if (number <= 1) return 1; if (number == 2) return 2; return jumpFloor(number - 1) + jumpFloor(number - 2); } }Java 2,非递归:
class Solution { public int jumpFloor(int n) { if (n <= 0) return 0; if (n == 1) return 1; if (n == 2) return 2; int a = 1, b = 2, sum = a + b; for (int i = 3; i < n; i++) { a = b; b = sum; sum = a + b; } return sum; } }
class Solution { public: int nums; void dec(int x){ if(x-1==0){ nums++; return; }else if(x-2==0){ nums+=2; return; }else{ dec(x-1); dec(x-2); } } int jumpFloor(int number) { nums=0; dec(number); return nums; } };
对于本题,前提只有 一次 1阶或者2阶的跳法。
a.如果两种跳法,1阶或者2阶,那么假定第一次跳的是一阶,那么剩下的是n-1个台阶,跳法是f(n-1);
b.假定第一次跳的是2阶,那么剩下的是n-2个台阶,跳法是f(n-2)
c.由a\b假设可以得出总跳法为: f(n) = f(n-1) + f(n-2)
d.然后通过实际的情况可以得出:只有一阶的时候 f(1) = 1 ,只有两阶的时候可以有 f(2) = 2
e.可以发现最终得出的是一个斐波那契数列:
| 1, (n=1)
f(n) = | 2, (n=2)