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 1 or 2 steps. In how many distinct ways can you climb to the top?
Subscribe to see which companies asked this question
简单的dp问题,代码如下:
class Solution {
public:
int climbStairs(int n) {
if(n <= )
return n;
dp.resize(n + );
dp[] = ;
dp[] = ;
for(int i = ; i < n + ; ++i){
dp[i] += dp[i - ];
dp[i] += dp[i - ];
}
return dp[n];
}
private:
vector<int> dp;
};
java版本代码如下所示:
public class Solution {
public int climbStairs(int n) {
if(n < ) return n;
int dp [] = new int [n+];
dp[] = ;
dp[] = ;
for(int i = ; i <= n; ++i){
dp[i] = dp[i-] + dp[i-];
}
return dp[n];
}
}
LeetCode OJ:Climbing Stairs(攀爬台阶)的更多相关文章
- Leetcode#70. Climbing Stairs(爬楼梯)
题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解 ...
- leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法
Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you ...
- [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 ...
- LeetCode 70 Climbing Stairs(爬楼梯)(动态规划)(*)
翻译 你正在爬一个楼梯. 它须要n步才干究竟顶部. 每次你能够爬1步或者2两步. 那么你有多少种不同的方法爬到顶部呢? 原文 You are climbing a stair case. It tak ...
- LeetCode OJ——Climbing Stairs
http://oj.leetcode.com/problems/climbing-stairs/ 走台阶的题目,转换出数学模型之后,就是Fibonacci数列.后一个数等于前两个数的和. 递归版本超时 ...
- [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 爬楼梯
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- 42. leetcode 70. Climbing Stairs
70. Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time y ...
- LeetCode746 Min Cost Climbing Stairs(爬上楼梯的最小损失)
题目 On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you p ...
- LN : leetcode 70 Climbing Stairs
lc 70 Climbing Stairs 70 Climbing Stairs You are climbing a stair case. It takes n steps to reach to ...
随机推荐
- Core ML 入门
1.下载Xcode 9 2.下载模型,https://developer.apple.com/machine-learning/ 3.开动.. 4.待续 模拟器66的
- Oracle中用sql语句取随机数和整数
--- 应用round(5.678,3)保留小数 应用floor(5.678)保留整数 应用dbms_random.value(30,50)取得随机数 --- dbms_random包 获得随机小数S ...
- C++ 第三十三天
Ⅰ.类成员函数的隐式参数 T *const this . 就是说对于某个类的成员函数 returnType function() 的真实面目其实是这样的 returnType function(T * ...
- nginx简介及优点总结
简介:nginx是web服务器,由C语言开发,基于事件驱动能处理百万级别的tcp连接,高度模块化的设计和自由的许可证使得扩展其功能的模块层出不穷, 跨平台,可使用当前操作系统特有的一些高效API来提高 ...
- linux 第三周
linux内核目录结构 arch目录包括了所有和体系结构相关的核心代码.它下面的每一个子目录都代表一种Linux支持的体系结构,例如i386就是Intel CPU及与之相兼容体系结构的子目录.PC机一 ...
- slf4j和log4j、logback
现在主流java项目一般使用slf4j+log4j的日志方案,最近抽点时间扫了一下slf4j.log4j.logback的官方文档,做个笔记.这篇比较不打算描述具体的配置方法,因为官方文档已经讲得很清 ...
- spi nor flash使用汇总
Overview SPI flash, 分为spi flash, DUAL spi flash, QUAD spi flash, 3-wire spi, 4-wire spi, 6-wire spi. ...
- Putting Apache Kafka To Use: A Practical Guide to Building a Stream Data Platform-part 2
转自: http://confluent.io/blog/stream-data-platform-2 http://www.infoq.com/cn/news/2015/03/ap ...
- Grunt Part 1
Grunt Part 1 Objectives and Outcomes In this exercise, you will learn to use Grunt, the task runner. ...
- Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 离散化+逆元
E. Mike and Geometry Problem time limit per test 3 seconds memory limit per test 256 megabytes input ...