[LeetCode]#69. Sqrt(x)

Input: 4
Output: 2
Input: 8
Output: 2
Explanation: The square root of 8 is 2.82842..., and since
the decimal part is truncated, 2 is returned.
class Solution(object):
def mySqrt(self, num):

left = 0
right = num

while(left <= right):
mid = (left + right)//2

if(mid**2 < num):
left = mid + 1

elif(mid**2 > num):
right = mid -1

else:
return mid
if num==1:
return 1
else:
return left -1

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store