-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqrt.py
More file actions
40 lines (36 loc) · 874 Bytes
/
sqrt.py
File metadata and controls
40 lines (36 loc) · 874 Bytes
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
38
39
40
# Implement int sqrt(int x).
#
# Compute and return the square root of x, where x is guaranteed to be a non-negative integer.
#
# Since the return type is an integer, the decimal digits are truncated and only the integer part of # the result is returned.
#
# Example 1:
#
# Input: 4
# Output: 2
class Solution(object):
def mySqrt(self, x):
"""
:type x: int
:rtype: int
"""
if x == 0 or x == 1:
return x
s = 0
while s * s <= x:
s = s + 1
if s * s > x:
s = s - 1
return s
if __name__ == "__main__":
s = Solution()
print(s.mySqrt(125))
print(s.mySqrt(225))
print(s.mySqrt(25))
print(s.mySqrt(2))
print(s.mySqrt(1))
print(s.mySqrt(0))
print(s.mySqrt(144))
print(s.mySqrt(190))
print(s.mySqrt(399))
print(s.mySqrt(900))