【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-十大经典排序算法
随机推荐
- spring-web中的StringHttpMessageConverter简介
spring的http请求内容转换,类似netty的handler转换.本文旨在通过分析StringHttpMessageConverter 来初步认识消息转换器HttpMessageConverte ...
- cocos2d-x-3.1 国际化strings.xml解决乱码问题 (coco2d-x 学习笔记四)
今天写程序的时候发现输出文字乱码,尽管在实际开发中把字符串写在代码里是不好的做法.可是有时候也是为了方便,遇到此问题第一时间在脑子里面联想到android下的strings.xml来做国际化.本文就仅 ...
- PHP实现上次登录功能
通过一个sql语句把上次的登录时间给本次登录时间,再把当前时间记录下来 update userinfo set lasttime=userinfo.logintime,logintime= CURR ...
- checkStyle使用具体解释
简单介绍 checkStyle是一款代码格式检查工具.它依据设置好的编码规则来自己主动检查代码.比方命名规范,文件长度.代码行长度等等.代码检查工具是保证项目代码质量.统一编码风格的一种重要途径.本篇 ...
- Socket的UDP协议在erlang中的实现
现在我们看看UDP协议(User Datagram Protocol,用户数据报协议).使用UDP,互联网上的机器之间可以互相发送小段的数据,叫做数据报.UDP数据报是不可靠的,这意味着如果客户端发送 ...
- python 基础 9.0 安装MySQL-python-1.2.5客户端
一. 安装客户端 python 标准数据库接口为Python DB-API,Python DB-API 为开发人员提供了数据应用编程接口.参考地址:https://wiki.python.or ...
- Mybatis之入门Helloworld程序
本篇我们来实现一个Mybatis的Helloworld级别的一个示例程序. 一.搭建基本环境 1.基本开发环境搭建,这里选择: eclipse j2ee 版本,mysql 5.1 ,jdk 1.8,m ...
- Hibernate连接池设置
在公司第一次做项目放到服务器上测试,发现每隔一段时间不用数据库就连接不上了(以前学过连接池,很久没用就忘了),在myeclipse上的时候没发现,网上搜索才发现是hibernate连接池配置问题. 1 ...
- Intellij Idea生成JavaDoc
JavaDoc是一种将注释生成HTML文档的技术,生成的HTML文档类似于Java的API,易读且清晰明了.在简略介绍JavaDoc写法之后,再看一下在Intellij Idea 中如何将代码中的注释 ...
- ABAP 设置单元格颜色
http://blog.163.com/ronanchen@126/blog/static/172254750201161811040488/ http://blog.csdn.net/lhx20/a ...