[LeetCode]: 70: Climbing Stairs
2015-10-08 09:49
330 查看
题目:
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-1梯,再上1梯" 和"先上n-2梯,再上2梯"这两个过程。
代码:
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-1梯,再上1梯" 和"先上n-2梯,再上2梯"这两个过程。
代码:
public static int climbStairs(int n) { switch(n){ case 1: return 1; case 2: return 2; default: int[] arrResult = new int[n+1]; arrResult[0] = 1; arrResult[1] = 1; arrResult[2] = 2; for(int i = 3;i <= n;i++){ arrResult[i]=arrResult[i-1]/*最后剩一个楼梯*/ + arrResult[i-2] /*最后剩两个楼梯*/; } return arrResult ; } }
相关文章推荐
- vs中 main和_tmain的区别
- Duplicate Emails
- 【交换安全】DAI - Dynamic ARP Inspection 详解
- 【初识——最大流】 hdu 1532 Drainage Ditches(最大流) USACO 93
- 弱校联萌十一大决战之背水一战C. Counting Pair
- Mysql检查表进行repair、optimize
- sencha touch 在新版谷歌浏览器中painted事件无法触发解决方案以及carousel 控件、togglefield控件、滚动条失效
- Locality-constrained Linear Coding for Image Classification(阅读)
- hdu4750Count The Pairs 并查集
- poj 1681 Painter's Problem (高斯消元)
- Codeforces #323 div2. D Once Again... dp LIS
- 人工智能与智能硬件
- hibernate中使用schemaExport生成数据表报错解决方法
- UVA_10976: Fractions Again?!
- 进程、僵尸进程、孤儿进程、进程组、前台进程组、后台进程组、孤儿进程组、会话、控制终端http://blog.csdn.net/hmsiwtv/article/details/7901711
- QPainter对象的一点说明
- AIDL 编译报couldn't find import for class原因探究
- BNUOJ 24251 Counting Pair 配对计数 【数论】
- UVA_442: Matrix Chain Multiplication
- exec函数族 http://blog.csdn.net/yuwenliang/article/details/5220650