LeetCode 367 Valid Perfect Square

LeetCode 367 Valid Perfect Square Problem

Download Code
class Solution(object):
    # def isPerfectSquare(self, num):
    #     """
    #     :type num: int
    #     :rtype: bool
    #     """
    #     i = 1
    #     while num > 0:
    #         num -= i
    #         i += 2
    #     return num == 0

    def isPerfectSquare(self, num):
        low, high = 1, num
        while low <= high:
            mid = (low + high) / 2
            mid_square = mid * mid
            if mid_square == num:
                return True
            elif mid_square < num:
                low = mid + 1
            else:
                high = mid - 1
        return False

    # def isPerfectSquare(self, num):
    #     x = num
    #     while x * x > num:
    #         x = (x + num / x) / 2
    #     return x * x == num
Download Valid Perfect Square.py

List of all Valid Perfect Square problems

Leetcode 367 Valid Perfect Square problem solution in python3 with explanation. This is the best place to expand your knowledge and get prepared for your next interview.

Feedback is the most important part of any website.

If you have any query, suggestion or feedback, Please feel free to contact us.