LeetCode(70)题解: climbing-stairs
https://leetcode.com/problems/climbing-stairs/
题目:
You are climbing a stair case. It takes n steps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
1)递归: 超时
class Solution {
public:
int climbStairs(int n) {
if (n==)
return ;
if(n==)
return ;
return climbStairs(n-)+climbStairs(n-);
}
};
2)非递归, 避免嵌套调用
递推公式
a_n=a_{n-1}+a_{n-2}.
a_1=1, a_2=2.
问题:给定n求a_n。其实就是斐波那契数列。
AC代码:
class Solution {
public:
int climbStairs(int n) {
if (n==)
return ;
if (n==)
return ;
int t=,x=,y=,z;
while(t!=n){
t++;
z=x+y;
x=y;
y=z;
}
return z;
}
};
LeetCode(70)题解: climbing-stairs的更多相关文章
- [LeetCode] Min Cost Climbing Stairs 爬楼梯的最小损失
On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...
- 【LeetCode练习题】Climbing Stairs
Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you c ...
- LeetCode(70) Climbing Stairs
题目 You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cli ...
- LeetCode算法题-Climbing Stairs(Java实现)
这是悦乐书的第159次更新,第161篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第18题(顺位题号是70).你正在爬楼梯,它需要n步才能达到顶峰.每次你可以爬1或2步, ...
- 【LeetCode】070. Climbing Stairs
题目: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cl ...
- Leetcode 题目整理 climbing stairs
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- LeetCode OJ:Climbing Stairs(攀爬台阶)
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- Python3解leetcode Min Cost Climbing Stairs
问题描述: On a staircase, the i-th step has some non-negative cost cost[i]assigned (0 indexed). Once you ...
- [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 ...
随机推荐
- bzoj 1137 [POI2009]Wsp 岛屿
题目大意 Byteotia岛屿是一个凸多边形.城市全都在海岸上.按顺时针编号1到n.任意两个城市之间都有一条笔直的道路相连.道路相交处可以自由穿行.有一些道路被游击队控制了,不能走,但是可以经过这条道 ...
- POJ3625 Building Roads
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10803 Accepted: 3062 Description Fa ...
- 如果dom节点是动态添加进页面的,在页面节点绑定事件如何解决的问题。
如果dom节点是动态添加进页面,想在节点绑定事件,传统的做法就是遍历节点,但会出现问题,也肯能有其他的办法,突然想到 可以依据事件冒泡,这样就不惧页面后添加节点而不响应事件的问题.比较结实.示例代码如 ...
- AForge.NET 设置摄像头分辨率
AForge.NET 老版本在预览摄像头时可通过设置DesiredFrameSize 属性,设置摄像头支持的分辨率,新版本提示已过期: 解决办法: 获取VideoCapabilities属性集合,选中 ...
- delphi 四舍五入Round函数【百帖整理】
在最近版本的Delphi Pascal 编译器中,Round 函数是以 CPU 的 FPU (浮点部件) 处理器为基础的.这种处理器采用了所谓的 "银行家舍入法",即对中间值 (如 ...
- 本地hosts文件
(1)什么是Hosts文件? Hosts是一个没有扩展名的系统文件,可以用记事本等工具打开,其作用就是将一些常用的网址域名与其对应的IP地址建立一个关联“数据库”,当用户在浏览器中输入一个需要登录的网 ...
- 洛谷—— P1577 切绳子
https://www.luogu.org/problemnew/show/P1577 题目描述 有N条绳子,它们的长度分别为Li.如果从它们中切割出K条长度相同的 绳子,这K条绳子每条最长能有多长? ...
- 洛谷——P1078 文化之旅
P1078 文化之旅 题目描述 有一位使者要游历各国,他每到一个国家,都能学到一种文化,但他不愿意学习任何一种文化超过一次(即如果他学习了某种文化,则他就不能到达其他有这种文化的国家).不同的国家可能 ...
- Skiing(最短路)
poj——3037 Skiing Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4921 Accepted: 1315 ...
- 【jar】JDK将单个的java文件打包为jar包,并引用到项目中使用【MD5加密】
==================================================================================================== ...