0069.cpp (374B)
1 class Solution { 2 public: 3 int mySqrt(int x) { 4 int low = 1, high = x; 5 while (low <= high) { 6 int mid = low + (high - low) / 2; 7 if (mid == x / mid) 8 return mid; 9 else if (mid > x / mid) 10 high = mid - 1; 11 else 12 low = mid + 1; 13 } 14 return high; 15 } 16 };