0

#yyds干货盘点# 解决剑指offer:跳台阶

 1 year ago
source link: https://blog.51cto.com/u_15488507/5366518
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

#yyds干货盘点# 解决剑指offer:跳台阶

原创

97的风 2022-06-08 10:54:16 博主文章分类:面试题 ©著作权

文章标签 数据 时间复杂度 递归 文章分类 Java 编程语言 阅读数178

1.简述:

一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个 n 级的台阶总共有多少种跳法(先后次序不同算不同的结果)。

数据范围:要求:时间复杂度: ,空间复杂度: 

青蛙要跳上两级台阶有两种跳法,分别是:先跳一级,再跳一级或者直接跳两级。因此答案为2

2.代码实现:

public class Solution {
public int jumpFloor(int target) {
//这里第0项为1,第1项为1
if(target <= 1)
return 1;
else
//递归子问题相加
return jumpFloor(target - 1) + jumpFloor(target - 2);
}
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK