70. Climbing Stairs
2016-04-14 13:45
387 查看
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
Hide Tags
Dynamic Programming
Solution 1:
Solution 2:
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
Hide Tags
Dynamic Programming
Solution 1:
public class Solution { public int climbStairs(int n) { if(n <=2 ) return n; int[] count = new int ; count[0] = 1; count[1] = 2; return getCount(count, n-1); } private int getCount(int[] count, int index) { if(count[index] != 0) return count[index]; int steps = getCount(count, index -1) + getCount(count, index -2); count[index] = steps; return steps; } }
Solution 2:
public class Solution { public int climbStairs(int n) { if(n <=2 ) return n; int[] count = new int ; count[0] = 1; count[1] = 2; for(int i =2; i< n; ++i) { count[i] = count[i-1]+count[i-2]; } return count[n -1]; } }
相关文章推荐
- Aizu 0005 GCD and LCM【辗转相除法】
- 70. Climbing Stairs
- Aizu 2224 Save your cats【最大生成树】
- MDK 5.10 -- Reading one or more Pack descriptions failed
- VS2008或以上版本的ActiveX控件的测试工具(ActiveX Control Test Container)编译记录
- CDISC SDTM SV domain 学习笔记
- SSH出现Host key verification failed
- 了解点OpenAI及深度学习研究前沿
- 【RAID】基本结构
- sleep()、wait()、yield()、join()方法 解析
- 数据仓库专题(23):总线矩阵的另类应用-Drill Down into a More Detailed Bus Matrix
- 从人工智能的“冬天“走出来
- [Consul]Error starting agent: Failed to get advertise address: Multiple private IPs found.
- baidu实习日志(入职篇)
- 理解TimesTen错误日志信息"waiting for latch"
- marathon参考(6): 约束Constraints
- WINAPI WinMain
- Failing Oozie Launcher, Main class [org.apache.oozie.action.hadoop.ShellMain], exit code [1]
- ICPCCamp2016day 1 - All Pair Shortest Path【bitset优化】
- http://blog.csdn.net/szwangdf/article/details/23432783