Climbing Stairs
2015-07-25 22:17
746 查看
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?
题目意思 : n步到达,每次可以走一步或两步 , 有多少种走法!
题目思路 : 当你最后一步走 一步的时候,对应着的 == W(n - 1) , 当你走两步 , == W(n - 2);
W(n) = W(n - 1) + W(n - 2)
子问题就是 当n 分别等于 1 , 2 , 3, 4 , ...... n - 1时对应的方法数。
public class Solution { public int climbStairs(int n) { int[] ways = new int ; if (n <= 0) return 0; ways[0] = 1; if (n >= 2) ways[1] = 2; else return ways[0]; for (int i = 2; i < n ; i++) { ways[i] = ways[i - 1] + ways[i - 2]; } return ways[n - 1]; } }
相关文章推荐
- strcpy,strncpy http://blog.csdn.net/shutear/article/details/8256096
- int main(int argc, char*argv[])
- 2015 Multi-University Training Contest 2 hdu 5308 I Wanna Become A 24-Point Master
- ECHOSRV.C中的main()设立一个 I/O completion port
- 【bzoj4152】 AMPPZ2014The Captain 最短路
- 一刀斩 :“SVN” failed to start 服务启动失败
- UVA - 10976 Fractions Again?!
- centos中w使用smbclient连接window出现:session setup failed: NT_STATUS_LOGON_FAILURE
- 二分搜索 HDOJ 2675 Equation Again
- 模拟器报Installation error: INSTALL_FAILED_CONTAINER_ERROR解决方法
- hdoj 2647 N!Again
- 人工智能的策略,如果国家优先发展”梦想成真”?
- Linux中的文件描述符与打开文件之间的关系 :http://blog.csdn.net/cywosp/article/details/38965239
- 关于Flash中的黑白棋AI编写心得
- Snail—OC学习之类别Category
- [leetcode ]220.Contains Duplicate III
- Snail—OC学习之空变量的表示
- Snail—OC学习之NSNumber
- Snail—OC学习之日期NSDate
- UVa 11853 - Paintball(DFS)