当前位置:网站首页>剑指offer 青蛙跳楼梯
剑指offer 青蛙跳楼梯
2022-07-23 05:45:00 【wzf6667】
题目描述
一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。(变态跳楼梯)
解题思路
1.数学归纳
因为n级台阶,第一步有n种跳法:跳1级、跳2级、到跳n级
跳1级,剩下n-1级,则剩下跳法是f(n-1)
跳2级,剩下n-2级,则剩下跳法是f(n-2)
所以f(n)=f(n-1)+f(n-2)+…+f(1)
因为f(n-1)=f(n-2)+f(n-3)+…+f(1)
所以f(n)=2*f(n-1)
直接根据公式计算,写出递归
2.暴力破解
跳10个楼梯,可以分为跳1个,跳2个一直到跳10个,所以循环。
如果跳了一个,剩下就还有9个,递归运行,九个又可以分成跳1个到跳九个,直到如果最后只剩下一个楼梯了,就返回1。其实这两种思路本质上是一样的,只是上一种在计算从1跳到n的加和中使用了公式而暴力破解是用循环,比如我跳10个,就会计算从f(1)加到f(9)的,但是公式直接就算出来f(10)=2*f(10-1)
public class Solution {
public int JumpFloorII(int target) {
if(target==1){
return 1;
}
int temp=0;
for(int i =1;i<target;i++){
temp=temp+JumpFloorII(i);
}
return temp+1;
}
}
边栏推荐
- 桌面远程协议-编解码
- Dicom开源工具库
- Jetson TX1安装 Pytorch
- [distinguish the meaning and usage of constant pointer and pointer constants const int * and int * const]
- Interpretation of the paper: "deep-4mcw2v: sequence based predictor for identifying N4 methylcytosine (4mc) sites in E. coli"
- Interpretation of the paper: develop and verify the deep learning system to classify the etiology of macular hole and predict the anatomical results
- 5.4 Pyinstaller库安装与使用
- 使用InfluxDB数据库的疑惑
- Embedded from entry to mastery (buried) - sharing of ultra detailed knowledge points 1
- Baidu Shen Shuo: focus on the scene, deeply cultivate the industry, and bring practical results to enterprise Digitalization
猜你喜欢
随机推荐
冒泡排序,快速排序
LVS负载均衡调度原理及配置方法
Axure实现增删改查
Baidu Shen Shuo: focus on the scene, deeply cultivate the industry, and bring practical results to enterprise Digitalization
Prometheus
鋼結構基本原理複習
Vs attribute configuration related knowledge
3.2daydayup举一反三:三天打鱼两天晒网式学习
[introduction to AUTOSAR com 4.com service layer module]
【AUTOSAR CanDrive 1.学习CanDrive的功能和结构】
【AUTOSAR DCM 1.模块简介(DSL,DSD,DSP)】
Simply realize the function of stack
【AUTOSAR COM 1.通信协议栈介绍】
Problems encountered in configuring the historical version of detectron
Find the saddle point of the matrix and its corresponding subscript.
Talent column | can't use Apache dolphin scheduler? The most complete introductory tutorial written by the boss in a month
Question bank of basic principles of steel structure
C语言数据库:基于tcp多进程的在线词典,有详细的步骤已经图解,欢迎大家来观看
合成中文识别数据集的相关repo
K-nucleotide frequencies (KNF) or k-mer frequencies








