【Leetcode】Climbing Stairs
2014-03-19 22:24
316 查看
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 有两种途径:从 n - 1 走一步 or 从 n - 2 走两步.
所以 f(n) = f(n - 1) + f (n - 2), 即斐波那契数列。可以迭代求解或者直接用通项公式。
View Code
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
到达 n 有两种途径:从 n - 1 走一步 or 从 n - 2 走两步.
所以 f(n) = f(n - 1) + f (n - 2), 即斐波那契数列。可以迭代求解或者直接用通项公式。
class Solution { public: int climbStairs(int n) { const double s = sqrt(5); return floor((pow((1+s)/2, n+1) + pow((1-s)/2, n+1))/s + 0.5); } };
View Code
相关文章推荐
- hdu 4300 Clairewd’s message(KMP)
- swap size failed
- HDU 1021Fibonacci Again
- URAL 1017. Staircases(递推)
- Non-PIE Binary in Adobe AIR
- 跨进程访问(AIDL服务)—Service(三)
- hotmail邮箱pop3服务器设置方法
- pair用法
- grails&groovy的IllegalArgument异常
- 【Leetcode】Container With Most Water
- SERC 2013 A: Beautiful Mountains
- 终于明白int main() 里面的argc,argv 参数了 (转自百科)
- 解决方法之ora-28056: Writing audit records to Windows Event Log failed
- raid超级块组织,外部元数据与内部元数据
- Thread_wait、notify、notifyAll的使用方法
- 一个异常:ConstraintViolationException: validation failed for classes [] during persist time for groups
- OCM_Session1_9_Database Backup and Availability
- TIME_WAIT过多的处理方法
- AI相关领域期刊会议列表
- http://blog.csdn.net/jrq/article/details/4211075(URL参数中文乱码)