-
Notifications
You must be signed in to change notification settings - Fork 2
/
69.py
73 lines (58 loc) · 1.92 KB
/
69.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# [ LeetCode ] 69. Sqrt(x)
def solution(x: int) -> int:
if x <= 1:
return x
else:
answer, start, end = 0, 1, x // 2
while start <= end:
middle: int = (start + end) // 2
if middle * middle > x:
end = middle = 1
else:
answer = middle
start = middle + 1
return answer
def another_solution(x: int) -> int:
if x < 2:
return x
else:
start, end = 2, x // 2
while start <= end:
middle: int = start + ((end - start) // 2)
target: int = middle * middle
if target > x:
end = middle - 1
elif target < x:
start = middle + 1
else:
return middle
return end
def bit_manipulation(x: int) -> int:
if x < 2:
return x
else:
left: int = bit_manipulation(x = x >> 2) << 1
right: int = left + 1
return left if right * right > x else right
def newton_method(x: int) -> int:
if x <= 1:
return x
else:
previous_x: int = x
next_x: int = (previous_x + (x / previous_x)) / 2
while previous_x - next_x >= 1:
previous_x = next_x
next_x = (previous_x + (x / previous_x)) / 2
return int(next_x)
if __name__ == "__main__":
cases: list[dict[str, dict[str, int] | int]] = [
{ "input": { "x": 0 }, "output": 0 },
{ "input": { "x": 1 }, "output": 1 },
{ "input": { "x": 4 }, "output": 2 },
{ "input": { "x": 8 }, "output": 2 },
]
for case in cases:
assert case["output"] == solution(**case["input"])
assert case["output"] == another_solution(**case["input"])
assert case["output"] == bit_manipulation(**case["input"])
assert case["output"] == newton_method(**case["input"])