LeetCode_Climbing Stairs
ou are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb or steps. In how many distinct ways can you climb to the top?
分析:类似斐波那契序列,使用动态规划的思想。定义f(n)为台阶数为n时青蛙跳到台阶顶部的方法数。那么当n>2 时f(n) = f(n-1) + f(n-2) f(1) = 1; f(2) = 2;
class Solution {
public:
int climbStairs(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(n <= ) return n;
int f1 = , f2 = , res = ;
for(int i = ; i <= n; ++i){
res = f1 + f2;
f1 = f2;
f2 = res;
}
return res;
}
};
LeetCode_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 ...
- [LintCode] Climbing Stairs 爬梯子问题
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- Leetcode: climbing stairs
July 28, 2015 Problem statement: You are climbing a stair case. It takes n steps to reach to the top ...
- LintCode Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- 54. Search a 2D Matrix && Climbing Stairs (Easy)
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...
- Climbing Stairs
Climbing Stairs https://leetcode.com/problems/climbing-stairs/ You are climbing a stair case. It tak ...
- 3月3日(6) Climbing Stairs
原题 Climbing Stairs 求斐波那契数列的第N项,开始想用通项公式求解,其实一个O(n)就搞定了. class Solution { public: int climbStairs(int ...
- Cllimbing Stairs [LeetCode 70]
1- 问题描述 You are climbing a stair case. It takes n steps to reach to the top. Each time you can eithe ...
- leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法
Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you ...
随机推荐
- jsonp实现跨域资源访问
平时项目中处理ajax跨域资源请求时,例如www.example2.com上的某个页面要请求www.example1.com的数据,我们使用得较多的是jsonp方式.jsonp通过JavaScript ...
- 警惕P2B模式
大家都知道P2P是什么,估计也有很多人了解P2B的意思,这里也不多做解释,但是为什么要警惕P2B,这里我要做详细说明,希望能给大家一个参考. 首先我们要把P2B分成两种,一种是针对大型企业, ...
- android 再按一次退出程序(实现代码)
效果如图: 实现代码: private long exitTime = 0; /** * 捕捉返回事件按钮 * * 因为此 Activity 继承 TabActivity 用 onKeyDown 无响 ...
- ASP.NET MVC 3 Razor Nested foreach with if statements
You need to write code this way. @Html.Raw("<tr>") Copy the below code and paste it ...
- Java中的自动装箱与拆箱
自动装箱和拆箱从Java 1.5开始引入,目的是将原始类型值转自动地转换成对应的对象.自动装箱与拆箱的机制可以让我们在Java的变量赋值或者是方法调用等情况下使用原始类型或者对象类型更加简单直接. 如 ...
- Day02_VI基本操作及C基础
2013年09月30日 星期一 09时37分03秒 回顾: 1. linux系统的知识背景 2. vi的使用 在正常模式下使用nyy可以把光标所在行开始的连续n行拷贝到剪贴板上去 在正 ...
- Shell中变量的使用
1.变量的声明 name="blacksonny" 注意://变量定义时不加$,变量与等号之间不能有空格 变量命名规则: 首个字符必须为字母(a-z,A-Z). 中间不能有空格,可 ...
- 模型-视图-控制器 (MVC)
在MVC中 ,模型代表数据和业务规则, 视图包含了用户界面元素,例如文本,表单等 控制器则管理模型和视图中的通信
- [原创作品]Javascript内存管理机制
如果你也喜欢分享,欢迎加入我们:QQ group:164858883 内存策略:堆内存和栈内存栈内存:在函数中定义的一些基本类型的变量和对象的引用变量都是在函数的栈内存中分配.当在一段代码块中定义一个 ...
- 共享IP云主机(VPS)玩转wdcp
目前国内有不少性能还不错的共享IP VPS,但因为没有独立IP,所以环境配置起来会比较麻烦. 因为本人自己现在用的就是共享IP的vps,所以把一些配置方法分享一下,供大家参考. 首先是系统的选择,根据 ...