[LeetCode] 70. Climbing Stairs(斐波那契数列)

【思路】
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.可以发现最终得出的是一个斐波那契数列。
由于直接用递归会超时,于是用数组来存储每一个位置的走法数目。代码如下:
class Solution {
public:
int climbStairs(int n) {
if(n == || n == || n == )
return n;
int *res = new int[n + ];
res[] = ; //有1级台阶时
res[] = ; //有2级台阶时
for(int i = ;i <= n;i ++)
{
res[i] = res[i - ] + res[i - ];
}
return res[n];
}
};
[LeetCode] 70. Climbing Stairs(斐波那契数列)的更多相关文章
- [LeetCode] Climbing Stairs 斐波那契数列
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- [每日一题2020.06.14]leetcode #70 爬楼梯 斐波那契数列 记忆化搜索 递推通项公式
题目链接 题意 : 求斐波那契数列第n项 很简单一道题, 写它是因为想水一篇博客 勾起了我的回忆 首先, 求斐波那契数列, 一定 不 要 用 递归 ! 依稀记得当年校赛, 我在第一题交了20发超时, ...
- [LeetCode] 70. Climbing Stairs 爬楼梯问题
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- 42. leetcode 70. Climbing Stairs
70. Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time y ...
- LN : leetcode 70 Climbing Stairs
lc 70 Climbing Stairs 70 Climbing Stairs You are climbing a stair case. It takes n steps to reach to ...
- leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法
Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you ...
- Leetcode 70 Climbing Stairs 递推
其实就是斐波那契数列 参考dp[n] = dp[n-1] +dp[n-2]; class Solution { public: int climbStairs(int n) { ; ; ; ; i & ...
- C#版 - 剑指offer 面试题9:斐波那契数列及其变形(跳台阶、矩形覆盖) 题解
面试题9:斐波那契数列及其变形(跳台阶.矩形覆盖) 提交网址: http://www.nowcoder.com/practice/c6c7742f5ba7442aada113136ddea0c3?tp ...
- [Amazon] Program for Fibonacci numbers 斐波那契数列
The Fibonacci numbers are the numbers in the following integer sequence. 0, 1, 1, 2, 3, 5, 8, 13, 21 ...
随机推荐
- javascript--setTimeout定时器
setTimeout() 可以理解为 定时炸弹 ---------------->隔一段事件执行,并且只会执行一次 函数语法: setTimeout(参数1,参数2) 参数1:待执行 ...
- Hello,移动WEB—px,dp,dpr像素基础
问题点1:iphone5分辨率:640 * 1136 dp,为什么chrome浏览器F12中显示的320 *568?? iPhone5 分辨率640 * 1136指的是物理像素,而实际 ...
- 初识python 函数(定义,传参,返回值)
python基础(二): 菜鸟教程基础知识讲解的非常全面,内容选择我认为的重点输出一遍 函数: 定义一个函数: 你可以定义一个由自己想要功能的函数,以下是简单的规则: def fun(arg): pa ...
- Leecode刷题之旅-C语言/python-136只出现一次的数字
/* * @lc app=leetcode.cn id=136 lang=c * * [136] 只出现一次的数字 * * https://leetcode-cn.com/problems/singl ...
- Angularjs 跨域post数据到springmvc
先贴网上己有解决方案链接: http://www.tuicool.com/articles/umymmqY (讲的是springmvc怎么做才可以跨域) http://my.oschina.net/ ...
- Android Studio modify language level to Java 8
If you need use lambda, should modify language level File -> Project Structure -> app -> Pr ...
- Delphi中Templates代码模板添加注意事项
今天用Delphi中的代码模板添加一段代码,结果就是有问题,多次测试后,发现是编码需要注意. <?xml version="1.0" encoding="GB231 ...
- ORB-SLAM (四)tracking跟踪解析
初始化完成后,对于相机获取当前图像mCurrentFrame,通过跟踪匹配上一帧mLastFrame特征点的方式,可以获取一个相机位姿的初始值:为了兼顾计算量和跟踪鲁棒性,处理了三种模型: 1. Tr ...
- php杂记——1(基础知识与文件读写)
1.变量前面需要加美元符号"$",常量则不需要: define('PRICE',100); echo PRICE; 2.用一个变量的值作为另一个变量的名称可以得到类似C中的指针变量 ...
- java线程池技术
1.线程池的实现原理?简介: 多线程技术主要解决处理器单元内多个线程执行的问题,它可以显著减少处理器单元的闲置时间,增加处理器单元的吞吐能力.假设一个服务器完成一项任务所需时间为:T1 创建线程时间, ...