Leetcode题解之 —— 爬楼梯

思路


动态规划

  • 最后一阶 = 倒数第一 + 1 || 倒数第二 + 2

题解


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* @param {number} n
* @return {number}
*/
var climbStairs = function(n) {
const cache = [0, 1, 2];
let count = 3;

while (count <= n) {
cache[count] = cache[count - 1] + cache[count - 2];
count++;
}

return cache[n];
};