题解 | #跳台阶#
跳台阶
http://www.nowcoder.com/practice/8c82a5b80378478f9484d87d1c5f12a4
动态规划
class Solution { public: int jumpFloor(int number) { vector<int> F(number, 0); F[0] = 1; F[1] = 2; for(int i=2; i<F.size(); ++i) F[i] = F[i-1]+F[i-2]; return F[F.size()-1]; } };
动态规划+迭代
class Solution { public: int jumpFloor(int number) { if(number==1 || number==2) return number; int pre=1, cur=2, pro; for(int i=2; i<number; ++i){ pro = pre+cur; pre = cur; cur = pro; } return pro; } };