-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path367.py
29 lines (27 loc) · 975 Bytes
/
367.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
# 367. Valid Perfect Square
# Given a positive integer num, return true if num is a perfect square or false otherwise.
# A perfect square is an integer that is the square of an integer.
# In other words, it is the product of some integer with itself.
# You must not use any built-in library function, such as sqrt.
# Example 1:
# Input: num = 16, Output: true
# Explanation: We return true because 4 * 4 = 16 and 4 is an integer.
# Example 2:
# Input: num = 14, Output: false
# Explanation: We return false because 3.742 * 3.742 = 14 and 3.742 is not an integer.
class Solution:
def isPerfectSquare(self, num):
low = 1
high = num
while low <= high:
mid = (low + high) // 2
if mid*mid == num:
return True
elif mid*mid < high:
low = mid + 1
else:
high = mid - 1
return False
solution = Solution()
num = 16
print(solution.isPerfectSquare(num))