跳转至

367.valid-perfect-square

Statement

Metadata

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

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

 

示例 1:

输入:num = 16
输出:true

示例 2:

输入:num = 14
输出:false

 

提示:

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

Metadata

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

Solution

class Solution:
    def isPerfectSquare(self, num: int) -> bool:
        return int(num ** 0.5) ** 2 == num

最后更新: October 11, 2023
回到页面顶部