Climbing Stairs
/* 斐波那契数列的简单应用,用矩阵乘法可以做到log(n)的时间复杂度 但这里返回的类型是int所以用普通的递推就行了F(n) = F(n-1)+F(n-2),F(1) = F(2) = 1; */ class Solution { public: int climbStairs(int n) { if(n<1) return 0; int a = 1,b = 1,c = 1; for(int i = 2 ; i <= n ; i++){ c = a+b; a = b; b = c; } return c; } };