题目:

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);
}
};

斐波那契数列通项公式

最新文章

  1. Ubuntu安装Mysqlcluster集群
  2. Tempter of the Bone---hdu1010(dfs+剪枝)
  3. 【原/转】【boost】智能指针使用规则以及介绍
  4. redis AND memcache
  5. Android精品课程—PullToRefresh 下拉刷新
  6. OBIEE 11g:Error:nQSError 36010 Server version 318 cannot read the newer version of the repository
  7. 李洪强iOS开发本人集成环信的经验总结_07_监听好友请求
  8. C#中DataGridView控件使用大全
  9. hdu 4741 Save Labman No.004(2013杭州网络赛)
  10. hdu 2859 (二维dp)
  11. android:layout_weight属性详解
  12. Tomcat如何检测内存泄漏
  13. vue中keep-alive使用时,注意要点
  14. 使用Visual Studio 2017 C++17模块(module)特性
  15. Python:Day43 抽屉
  16. Django(十二)—关于查询知识点总结
  17. hdu-2865-polya+dp+矩阵+euler函数
  18. 计算机网络 之 TCP和UDP的端口号解析
  19. vue预渲染实践总结
  20. Java SE 基础知识(String,Array)

热门文章

  1. wpf SplitButton
  2. HDU 1874 畅通project续 最短路径入门(dijkstra)
  3. GitHub使用问题(遇到一个记一个)
  4. SpringBoot启动流程分析(二):SpringApplication的run方法
  5. ubuntu下matlab的无界面启动---命令行操作
  6. 解决mysql数据库乱码问题
  7. python升级或者其他原因把yum搞坏了
  8. python学习(三)数字类型示例
  9. 允许局域网内其他主机访问本地MySql数据库
  10. C#高级编程 第十五章 反射