【Leetcode】Climbing Stairs
2016-06-04 20:15
375 查看
题目链接:https://leetcode.com/problems/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?
思路:
上一步怎么走的跟下一步都的没关系。用data数组表示到每一步为止,有多少种方法。
算法:
[java] view
plain copy
public int climbStairs(int n) {
int[] data = new int[n + 3];
data[1] = 1;
data[2] = 2;
for (int i = 3; i <= n; i++) {
data[i] = data[i - 2] + data[i - 1];
}
return data
;
}
题目:
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?
思路:
上一步怎么走的跟下一步都的没关系。用data数组表示到每一步为止,有多少种方法。
算法:
[java] view
plain copy
public int climbStairs(int n) {
int[] data = new int[n + 3];
data[1] = 1;
data[2] = 2;
for (int i = 3; i <= n; i++) {
data[i] = data[i - 2] + data[i - 1];
}
return data
;
}
相关文章推荐
- AIDL简单实现(2)
- AIDL简单实现(1)
- kafka集群启动遇到LeaderNotAvailableException错误
- UVA253——骰子涂色(Cube painting)
- POJ1804 Brainman (逆序对)
- 使用行为树(Behavior Tree)实现游戏AI
- Failed to lookup view... in views directory...
- Communications link failure
- git 首次使用以及error: failed to push some refs to ****问题的解决
- 内置的材质包含文件 .cginc
- UVA 10780 Again Prime? No Time(质因数分解)
- 体育竞技游戏的团队AI
- 在windows下安装scala出现错误:找不到或无法加载主类 scala.tools.nsc.MainGenericRunner
- 来自知乎的pthread_cond_wait为什么总是带着mutex
- 梯田AGAIN
- [Codeforces667A]Pouring Rain(数学,几何)
- 【STL源码剖析】__type_traits技法
- MongoDB:[HostnameCanonicalizationWorker] Failed to obtain address information for hostname
- AirlineTicket
- Codeforces AIM Tech Round (Div. 2) A. Save Luke