标签:
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?
Tags:Dynamic Programming
简单分析之:
class Solution { public: //递归方法:在LeetCode测试,发现超时 int climbStairs_recur(int n) { if (1 == n) { return 1; } else if (2 == n) { return 2; } else { return climbStairs_recur(n-2) + climbStairs_recur(n-1); } } //循环方法:利用了上一次的计算结果,速度快 int climbStairs_loop(int n) { if (1 == n) { return 1; } else if (2 == n) { return 2; } int cur = 2; int last = 1; for (int i=3; i<=n; i++) { int t = last + cur; last = cur; cur = t; } return cur; } };
标签:
原文地址:http://www.cnblogs.com/whl2012/p/5751269.html