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.

 

Example 1:

Input: num = 16
Output: true

Example 2:

Input: num = 14
Output: false

 

Constraints:

  • 1 <= num <= 2^31 - 1




 class Solution:
    def isPerfectSquare(self, num: int) -> bool:

        if num < 2:
            return num

        left = 0
        right = num//2

        while left <=right:
            mid = left + (right-left)//2
            x =mid*mid

            if  x == num:
                return True
            elif x > num:
                right = mid -1
            else:
                left = mid + 1

        return False

Random Note


  1. Effectively Using Django REST Framework Serializers
  2. How To Use DRF Serializers Effectively in Django
  3. My personal django rest framework serializer notes
  4. How to use DRF serializers effectively during write operations