Skip to content

Latest commit

 

History

History
22 lines (20 loc) · 452 Bytes

爬楼梯.md

File metadata and controls

22 lines (20 loc) · 452 Bytes
// 经典动态规划题目  f(x)=f(x−1)+f(x−2)
function climbStairs(n) {
  if (n === 1) {
    return 1;
  }

  let first = 1,
    second = 2;
  // 滚动数组思想
  for (let i = 3; i <= n; i++) {
    // 第三个数字为第一个与第二个之和
    let third = first + second;
    first = second;
    second = third;
  }
  // 返回第二个
  return second;
}