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 ...
随机推荐
- hdu 4602 递推关系矩阵快速幂模
Partition Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- HDU 4722:Good Numbers(数位DP)
类型:数位DP 题意:定义一个Good Number 为 一个数所有位数相加的和%10==0.问[A,B]之间有多少Good Number. 方法: 正常“暴力”的定义状态:(i,d,相关量) 定义d ...
- 用Linux完成Oracle自动物理备份
https://blog.csdn.net/24024846/article/details/115292
- 模块化开发(seajs)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- django中表变更后migrate无效的问题
问题描述: 已有的model,修改之后,想重新建模,于是将migrations文件夹中除__init__.py之外其他文件都删掉,再次执行以下步骤python manage.py makemigrat ...
- js-压缩混淆工具 uglifyjs
单个打包文件npm install uglify-js -g 使用uglifyjs压缩js uglifyjs 原始js文件 -m -c -o 压缩后js文件 uglifyjs 原始js文件 -b -c ...
- 四个很好的开源app项目
Open Source and the iOS App Store Today, we are open-sourcing 4 iOS apps: ThatInbox, an email client ...
- xamarin.android pullToRefresharp.Android下拉刷新样式、侧滑删除功能
如果你正则使用xamarin.From开发项目,那么listview一定是你比不可少的控件.但是由于xamarin的listview在安卓上的功能有限,所以经常需要使用Renderers来改写平台实现 ...
- Ubuntu -- 下如何查看CPU信息, 包括位数和多核信息
from: http://hi.baidu.com/sdusoul/blog/item/76f349508f74fb6e843524eb.html 查看当前操作系统内核信息# uname -a Lin ...
- tensorflow global_variables_initializer()
老版本为 init = tf.initialize_all_variables() 新版本为 init = tf.global_variables_initializer()