Leetcode 練習:2021-12-17

December 17, 2021

Sqrt(x)

題號:69

var mySqrt = function (x) {
  if (x <= 1) return x
  let min = 1
  let max = x
  while (min <= max) {
    const current = Math.floor((max + min)/2)
    if (current**2 === x) return current
    if (current**2 > x) {
      max = current - 1
    } else {
      min = current + 1
    }
  }
  return min - 1
}

Climbing Stairs

題號:70

var climbStairs = function (n) {
  const map = new Map([
    [1, 1],
    [2, 2],
    [3, 3],
  ])

  const step = (k) => {
    if (!map.has(k)) map.set(k, step(k - 1) + step(k - 2))
    return map.get(k)
  }
  return step(n)
}

Profile picture

Wei Hung 的筆記 / 部落格。