(简单)367有效的完全平方数

题目

给定一个 正整数 num ,编写一个函数,如果 num 是一个完全平方数,则返回 true ,否则返回 false 。

进阶:不要 使用任何内置的库函数,如  sqrt 。

来源:力扣(LeetCode)
链接:题目描述与示例icon-default.png?t=LA23https://leetcode-cn.com/problems/valid-perfect-square

代码

 

class Solution:
    def isPerfectSquare(self, num: int) -> bool:
        ans = 1
        while ans**2 <= num:
            if ans**2 == num:
                return True
            ans += 1
        return False

你可能感兴趣的:(leetcode,算法,python)