力扣70. 爬楼梯
记录一下自己当时的思路,矩阵快速幂暂时没有尝试
题目:【https://leetcode.cn/problems/climbing-stairs/?envType=study-plan-v2&envId=top-interview-150】
初版思路
1 static int cache[45]; 2 3 int body(int n) 4 { 5 if (0 == n) return 0; 6 else if (1 == n) return 1; 7 else if (2 == n) return 2; 8 9 if (-1 != cache[n - 1]) 10 return cache[n - 1]; 11 12 cache[n - 1] = body(n - 1) + body(n - 2); 13 return cache[n - 1]; 14 } 15 16 int climbStairs(int n) { 17 memset(cache, -1, sizeof(cache)); 18 return body(n); 19 }
稍微进阶一点
1 int climbStairs(int n) { 2 int a, b, c; 3 a = 0; 4 b = 1; 5 c = 0; 6 7 for (int i = 0; i < n; ++i) { 8 c = a + b; 9 a = b; 10 b = c; 11 } 12 return c; 13 }
浙公网安备 33010602011771号