Sunny-117 / js-challenges

✨✨✨ Challenge your JavaScript programming limits step by step

Home Page:https://juejin.cn/column/7244788137410560055

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

x 的平方根

lzxjack opened this issue · comments

x 的平方根
const mySqrt = x => {
  let [left, right] = [0, x];

  while (left <= right) {
    const mid = (left + right) >> 1;

    if (mid ** 2 > x) {
      right = mid - 1;
    } else if (mid ** 2 < x) {
      left = mid + 1;
    } else {
      return mid;
    }
  }

  return right;
};