【LeetCode】070. Climbing Stairs
2024-08-23 13:00:16
题目:
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?
Note: Given n will be a positive integer.
题解:
爬到第n层的方法要么是从第n-1层一步上来的,要不就是从n-2层2步上来的
Solution 1 ()
class Solution {
public:
int climbStairs(int n) {
vector<int> dp(n,);
if(n<) return n;
dp[] = ;
dp[] = ;
for(int i=; i<n; i++) {
dp[i] = dp[i-] + dp[i-];
}
return dp[n-];
}
};
Solution 2 ()
class Solution {
public:
int climbStairs(int n) {
if(n<) return n;
int a = ;
int b = ;
for(int i=; i<n; i++) {
b += a;
a = b - a;
}
return b;
}
};
Solution 2
class Solution {
public:
int climbStairs(int n) {
const double t = sqrt();
return floor((pow(( + t) / , n + ) + pow(( - t) / , n + )) / t + 0.5);
}
};
斐波那契数列通项公式
最新文章
- Ubuntu安装Mysqlcluster集群
- Tempter of the Bone---hdu1010(dfs+剪枝)
- 【原/转】【boost】智能指针使用规则以及介绍
- redis AND memcache
- Android精品课程—PullToRefresh 下拉刷新
- OBIEE 11g:Error:nQSError 36010 Server version 318 cannot read the newer version of the repository
- 李洪强iOS开发本人集成环信的经验总结_07_监听好友请求
- C#中DataGridView控件使用大全
- hdu 4741 Save Labman No.004(2013杭州网络赛)
- hdu 2859 (二维dp)
- android:layout_weight属性详解
- Tomcat如何检测内存泄漏
- vue中keep-alive使用时,注意要点
- 使用Visual Studio 2017 C++17模块(module)特性
- Python:Day43 抽屉
- Django(十二)—关于查询知识点总结
- hdu-2865-polya+dp+矩阵+euler函数
- 计算机网络 之 TCP和UDP的端口号解析
- vue预渲染实践总结
- Java SE 基础知识(String,Array)