c++ binary search (3 ms)


  • 0
    class Solution {
    public:
      int mySqrt(int x) {
        if (x == 1) return x;
        int l = 0, r = min((x >> 1) + 1, 46341);
        while (r - l > 1) {
          int mid = (l + r) >> 1;
          if (mid * mid > x) r = mid;
          else l = mid;
        }
        return l;
      }
    };
    

Log in to reply
 

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