当前位置:网站首页>剑指offer 跳台阶扩展问题
剑指offer 跳台阶扩展问题
2022-07-26 17:31:00 【早田凛凛子】
跳台阶扩展问题
题目描述:
一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶(n为正整数)总共有多少种跳法?
解题:
f[0]=f[1]=1
f[n]=f[n-1]+f[n-2]+…+f[0]
因为 f[n-1]=f[n-2]+…+f[0]
所以 f[n]=2 * f[n-1]
同理 f[n-1]=2 * f[n-2]
最终得到 f[n]=2 * 2 * 2 * … * f[0]
代码:
class Solution {
public:
int jumpFloorII(int number) {
if(number==1 || number==0){
return 1;
}
int sum=1;
//因为0和1都是1所以从哪里开始都无所谓
for(int i=1;i<number;i++){
//左移乘2,右移除以2
sum=sum<<1;
}
return sum;
}
};
边栏推荐
- Spark unified memory partition
- Cross site scripting attack (XSS)
- Common APIs
- PMP Exam details, what changes have been made to the new exam outline?
- 8.2 一些代数知识(群、循环群和子群)
- 相对路径与绝对路径
- 长征证券开户安全吗?
- 10、 Implementation of parameter modification of parameter server
- “蔚来杯“2022牛客暑期多校训练营3记录
- AI zhetianchuan ml integrated learning
猜你喜欢

Cross Site Request Forgery (CSRF)

It is said that the salary of Alibaba P7 is really fragrant

PS_1_认识主界面_新建文档(分辨率)_打开保存(序列动画)

8.2 一些代数知识(群、循环群和子群)

Become a test / development programmer, Xiao Zhang: reality is coming

How to assemble a registry?

CentOS installs docker and MySQL and redis environments

LeetCode50天刷题计划(Day 1—— 两数相加 11.00-12.30)

ICML 2022(第四篇)|| 图分层对齐图核实现图匹配

4、 Service communication principle, code implementation
随机推荐
【静态代码质量分析工具】上海道宁为您带来SonarSource/SonarQube下载、试用、教程
OpenWrt之feeds.conf.default详解
[Day2] cinema ticket
【集训Day1】 Dwarves line up
Apartment rental system based on JSP
【集训Day3】section
LeetCode50天刷题计划(Day 1—— 两数相加 11.00-12.30)
Linux Installation mysql8.0.29 detailed tutorial
【Unity3D】摇杆
Week 17 free intrusion pointer exercise - output maximum
Drools basic grammar
PS_1_认识主界面_新建文档(分辨率)_打开保存(序列动画)
Vector CANape - How to Send Receive CAN Message in CANape
《敏捷宣言》四大价值观,十二大原则
College personnel management system based on jsp+servlet
Vector CANoe Menu Plugin拓展入门
百度飞桨EasyDL X 韦士肯:看轴承质检如何装上“AI之眼”
Spark unified memory partition
常用api
2022河南萌新联赛第(三)场:河南大学