Skip to content

Commit

Permalink
Valid Perfect Square
Browse files Browse the repository at this point in the history
  • Loading branch information
nirmalnishant645 committed May 8, 2020
1 parent f63f9ea commit 766cfcd
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions 0367-Valid-Perfect-Square.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'''
Given a positive integer num, write a function which returns True if num is a perfect square else False.
Note: Do not use any built-in library function such as sqrt.
Example 1:
Input: 16
Output: true
Example 2:
Input: 14
Output: false
'''
class Solution:
def isPerfectSquare(self, num: int) -> bool:
start, end = 0, num
while start <= end:
mid = (start+end)//2
square = mid*mid
if square == num:
return True
elif square > num:
end = mid - 1
else:
start = mid + 1
return False

0 comments on commit 766cfcd

Please sign in to comment.