367. Valid Perfect Square

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):
        """
        :type num: int
        :rtype: bool
        """
        n = 1
        while n*n<=num:
            if n*n == num:
                return True
            n += 1
        return False
posted @ 2018-10-04 11:29  bernieloveslife  阅读(134)  评论(0编辑  收藏  举报