【LeetCode】070. 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?
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);
}
};
斐波那契数列通项公式
【LeetCode】070. Climbing Stairs的更多相关文章
- 【LeetCode】70. Climbing Stairs 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目大意 题目大意 解题方法 递归 记忆化搜索 动态规划 空间压缩DP 日期 [L ...
- 【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 ...
- 【一天一道LeetCode】#70. Climbing Stairs
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 You are ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
随机推荐
- Java 嵌套类和内部类演示样例<二>
嵌套类(nested class)是一个在还有一个类或接口内部声明的类. 嵌套类分为两种:静态内部类(static inner class)和非静态嵌套类(non-static nested clas ...
- Android 自己定义ImageView实现圆角/圆形 附加OnTouchListener具体凝视以及Button圆角
转载请注明出处:王亟亟的大牛之路 平时要用一些非方方正正的button之类的小伙伴们是怎样实现的?RadioButton? ImageButton? 还是其它? 今天亟亟上的是ImageView来实现 ...
- Chrome + Python 抓取动态网页内容
用Python实现常规的静态网页抓取时,往往是用urllib2来获取整个HTML页面,然后从HTML文件中逐字查找对应的关键字.如下所示: import urllib2 url="http: ...
- hdu 1068 Girls and Boys 二分图的最大匹配
题目链接:pid=1068">http://acm.hdu.edu.cn/showproblem.php? pid=1068 #include <iostream> #in ...
- wpf自定义colorpicker
自定义colorpicker <ObjectDataProvider MethodName="GetType" ObjectType="{x:Type system ...
- Python 之 安装模块的多种方法
1.自己写的模块,能够直接加入到路径下. 这样就能够直接调用. import sys sys.path.append("/home/username/") 2.单文件模块 直接把文 ...
- 字符数组和strcpy
已知strcpy函数的原型是char *strcpy(char *strDest, const char *strSrc);,其中strDest是目的字符串,strSrc是源字符串. (1)Write ...
- 前后端分离之fiddler前端开发代理 autoresponder 正则表达式 regex:(?insx) 修正符详解
regex:(?isx)^http://127.0.0.1:3000(/dlscene)?/order/(\w*) http://127.0.0.1:8080/dlscene/order/$2 上面这 ...
- Software-defined networking
Software-defined networking administrators to programmatically initialize, control, change, and mana ...
- Java for LeetCode 099 Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...