当前位置:网站首页>剑指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;
}
};
边栏推荐
- AI zhetianchuan DL regression and classification
- Deep learning experiment: softmax realizes handwritten digit recognition
- [training day3] delete
- [unity3d] rocker
- [training Day1] spy dispatch
- 相对路径与绝对路径
- ssm练习第四天_获取用户名_用户退出_用户crud_密码加密_角色_权限
- Spark data format unsafe row
- LeetCode 0139. 单词拆分
- 2、 Topic communication principle, code implementation
猜你喜欢
随机推荐
drools-基础语法
Overview of the agenda of the keynote speech of apachecon Asia, an international celebrity vs a local open source star
Click hijacking attack
Leetcode 50 day question brushing plan (day 5 - longest palindrome substring 10.50-13:00)
web项目文件简单上传和下载
.net CLR GC dynamic loading transient heap threshold calculation and threshold excess calculation
Hosts this file has been set to read-only solution
SQL判断某列中是否包含中文字符、英文字符、纯数字,数据截取
Quartz trigger rule
VIM多行操作
深度学习实验:Softmax实现手写数字识别
LeetCode 0137. 只出现一次的数字 II
Become a test / development programmer, Xiao Zhang: reality is coming
AI遮天传 ML-无监督学习
Detailed explanation of openwrt's feeds.conf.default
【集训Day2】cinema ticket
Relative path and absolute path
十年架构五年生活-06 离职的冲动
Deep learning experiment: softmax realizes handwritten digit recognition
AI zhetianchuan DL regression and classification









