【LeetCode】70 - Climbing Stairs
2015-07-26 22:26
465 查看
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?
Hide Tags: Dynamic Programming
Solution:一个台阶的方法次数为1次,两个台阶的方法次数为2个。n个台阶的方法可以理解成上n-2个台阶,然后2步直接上最后一步;或者上n-1个台阶,再单独上一步
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Hide Tags: Dynamic Programming
Solution:一个台阶的方法次数为1次,两个台阶的方法次数为2个。n个台阶的方法可以理解成上n-2个台阶,然后2步直接上最后一步;或者上n-1个台阶,再单独上一步
int climbStairs(int n) { //Runtime:0ms int a=1,b=2; if(n==1)return a; if(n==2)return b; int c=0; for(int i=3;i<=n;i++){ c=a+b; a=b; b=c; } return c; } int climbStairs2(int n){ ////Runtime:0ms if(n <= 2)return n; int* step = new int ; step[0] = 1; step[1] = 2; for(int i = 2; i < n; i++)step[i] = step[i-1] + step[i-2]; return step[n-1]; }
相关文章推荐
- RAII手法封装互斥锁
- assign、copy、retain、weak、strong的区别与联系
- DataInputStream和DataOutputStream操作基本数据类型的流
- Fine-Grained Histopathological Image Analysis via Robust Segmentation and Large-Scale Retrieval文章总结
- Contains Duplicate
- hdu 5289 - Assignment(2015 Multi-University Training Contest 1 )单调队列+RMQ+树状数组
- Could not find a storyboard named 'MainStoryboard'
- 如何让gmail不过滤垃圾邮件
- 5.4 QEMU block & AIO
- hdu2464 A Pair of Graphs 线段树
- scikit-learn:4.7. Pairwise metrics, Affinities and Kernels
- ssh中“Host key verification failed.“的解决方案
- 14级第一次测试 E - The Snail
- HDU-2675 Equation Again
- 解决 httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0 错误
- 传苹果今年推iPad mini4 相当于微缩版Air2
- Project Euler:Problem 92 Square digit chains
- Airbnb Interview - Nested Integer List Parser
- ORA-12571: TNS:packet writer failure
- leetcode:Factorial Trailing Zeroes