独学ヒトリPosts
LeetCode 69. Sqrt(x)
2022.07.18

問題

https://leetcode.com/problems/sqrtx/https://leetcode.com/problems/sqrtx/

typescript


function mySqrt(x: number): number {
  let l = 1
  let r = x
  let m
  while (l < r) {
    m = Math.floor((l + r) / 2)
    if (m ** 2 === x) {
      return m
    } else if ( m ** 2 < x) {
      l = m + 1
    } else {
      r = m
    }
  }
  if (r ** 2 > m) return r - 1
  return r
};

Profile Icon
独学ヒトリ
Tech is Simple !
複雑化しすぎたテクノロジーについて、原点に立ち戻り、絶対に理解します。
YouTube
Twitter
  • Duolingo English Test
  • テクノロジー解説
  • SQL
  • アルゴリズム
  • LeetCode
  • atcoder
    • ★2
    • ★3
    • ★4
    • ★5
    • ★6
    • ★7
    • bit全探索
    • 包除処理
    • 二分探索
    • 累積和
  • Python
    • Pandas
  • CSS
  • ガジェット
  • 雑記
  • 2020
    • 03 (2)
  • 2021
    • 08 (4)
    • 09 (4)
    • 10 (9)
    • 12 (6)
  • 2022
    • 03 (3)
    • 05 (1)
    • 06 (3)
    • 07 (24)
    • 08 (3)
    • 09 (1)
    • 10 (5)
    • 11 (4)
    • 12 (7)
TOP投稿記事プライバシーポリシー
Copyright© 独学ヒトリ , 2022 All Rights Reserved.