Skip to content

Commit d72d19a

Browse files
authored
Merge pull request #1220 from ivan1016017/march25
adding sqrtx
2 parents cbec9c4 + b8fc441 commit d72d19a

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def mySqrt(self, x: int) -> int:
6+
7+
left, right = 0, x
8+
9+
while left <= right:
10+
11+
mid = (left + right)//2
12+
13+
if mid ** 2 > x:
14+
right = mid - 1
15+
elif mid ** 2 < x:
16+
left = mid + 1
17+
else:
18+
return mid
19+
20+
return min(left, right)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_14\
3+
.sqrtx import Solution
4+
5+
class Solution:
6+
def mySqrt(self, x: int) -> int:
7+
8+
left, right = 0, x
9+
10+
while left <= right:
11+
12+
mid = (left + right)//2
13+
14+
if mid ** 2 > x:
15+
right = mid - 1
16+
elif mid ** 2 < x:
17+
left = mid + 1
18+
else:
19+
return mid
20+
21+
return min(left, right)

0 commit comments

Comments
 (0)