JavaScript Solution O(logn)


  • 0
    /**
     * @param {number} x
     * @return {number}
     */
    var mySqrt = function(x) {
      if (x < 1) return 0;
      let high = x;
      let low = 1;
      let mid = 0;
      while(low + 1 < high) {
        mid = Math.floor((high + low)/2);
        if (mid * mid > x) {
          high = mid;
        } else if (mid * mid < x) {
          low = mid;
        } else {
          return mid;
        }
      }
      return low;
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.