Quick search solution with go


  • 0
    K
    func mySqrt(x int) int {
      if x == 0 {
        return 0
      }
      if x == 1 {
        return 1
      }
      i, j := 1, 2
      for {
        if i * i <= x && (i + 1) * (i + 1) > x {
            break
        }
        if i * i < x && (i * 2) * (i * 2) > x {
          j = i / 2
          for (i + j) * (i + j) > x {
            j /= 2
          }
          i += j
        } else if (i * 2) * (i * 2) <= x {
          i *= 2
        }
      }
      return i
    }
    

Log in to reply
 

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