Skip to content

Commit 2eec026

Browse files
authored
Merge pull request neetcode-gh#1233 from UdayGarg/367-Valid-Perfect-Square
Create: 367-Valid-Perfect-Square.py
2 parents 305edac + 540898b commit 2eec026

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

python/367-Valid-Perfect-Square.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def isPerfectSquare(self, num: int) -> bool:
3+
for i in range(1, num+1):
4+
if i * i == num:
5+
return True
6+
if i* i > num:
7+
return False
8+
9+
10+
11+
def isPerfectSquare_2(self, num: int) -> bool:
12+
l ,r = 1, num
13+
while l <= r:
14+
mid = (l +r) // 2
15+
if mid * mid > num:
16+
r = mid - 1
17+
elif mid * mid < num:
18+
l = mid + 1
19+
else:
20+
return True
21+
return False

0 commit comments

Comments
 (0)