-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode 69 - sqrt(x).py
37 lines (25 loc) · 925 Bytes
/
leetcode 69 - sqrt(x).py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
"""
Given a non-negative integer x, compute and return the square root of x.
Since the return type is an integer, the decimal digits are truncated, and only the integer part of the result is returned.
Note: You are not allowed to use any built-in exponent function or operator, such as pow(x, 0.5) or x ** 0.5.
Question : https://leetcode.com/problems/sqrtx/
"""
# import numpy as np
# class Solution:
# def mySqrt(self, x: int) -> int:
# return int(np.sqrt(x))
class Solution:
def mySqrt(self, x: int) -> int:
start = 0
end = x
while start + 1 < end:
mid = start + (end - start) // 2
if mid * mid == x:
return mid
elif mid * mid < x:
start = mid
else:
end = mid
if end * end == x:
return end
return start