-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisPerfectSquare.py
More file actions
37 lines (29 loc) · 965 Bytes
/
isPerfectSquare.py
File metadata and controls
37 lines (29 loc) · 965 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
"""
Given a positive integer num, write a function which returns True if num is a perfect square else False.
Follow up: Do not use any built-in library function such as sqrt.
Took a very random kind of route... where I found the square root and converted the value to an int then squared it
again.
Found a solution that's probably better using binary search.
Use that one lol.
class Solution:
def isPerfectSquare(self, num: int) -> bool:
if num==1:
return True
high = num
low = 1
while low <= high:
mid = (high + low) // 2
sq = mid * mid
if sq == num:
return True
elif sq > num:
high = mid - 1
elif sq < num:
low = mid + 1
return False
"""
class Solution:
def isPerfectSquare(self, num: int) -> bool:
if num == 1:
return True
return int(num ** 0.5) ** 2 == num