Climbing Stairs 解答
2015-09-19 23:46
381 查看
Question
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?
Solution
Classic DP problem. When we climb n steps, we have two choices for last step, either one step or two steps. To save space, we can further improve the solution using O(1) space.public class Solution { public int climbStairs(int n) { if (n < 1) return 0; int[] dp = new int[3]; dp[0] = 1; dp[1] = 1; for (int i = 2; i <= n; i++) { dp[2] = dp[0] + dp[1]; dp[0] = dp[1]; dp[1] = dp[2]; } return dp[1]; } }
相关文章推荐
- CLOSE_WAIT的产生以及影响和解决方案
- YoMail,Gmail死忠粉的福音——直接收发Gmail邮件
- YoMail,Gmail死忠粉的福音——直接收发Gmail邮件
- dojo EnhancedGrid的两种实现方式对比,转载自http://blog.csdn.net/earthhour/article/details/17203515
- AIDL--------应用之间的通信接口
- Codeforces Round #320 (Div. 2) 579A Raising Bacteria(脑洞)
- Paint
- Trailing Zeroes (III)(lightoj 二分好题)
- relocation R_ARM_THM_MOVW_ABS_NC against `a local symbol' can not be used when making a shared objec
- 增益 Gain 分贝 dB
- Light oj 1138 - Trailing Zeroes (III)
- mexTrainDL - SPAms
- “warning C4251::CStringT<BaseType,StringTraits>需要有 dll 接口”解决办法
- Git Step By Step - Step 1: Start Over Again
- AI-路径导航(最短路径算法 and A算法)
- xcode7报错:does not contain bitcode
- nginx: [emerg] getpwnam(“www”) failed
- poj3250 Bad Hair Day
- 关于在虚拟设备上运行项目的时候遇到INSTALL_FAILED_NO_MATCHING_ABIS,提示卸载以前安装的版本
- Silverlight 2.5D RPG游戏技巧与特效处理:(十一)AI系统