367.valid-perfect-square


描述

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.

测试用例

Input: num = 16
Output: true

题解

暴力循环。

var isPerfectSquare = function(num) {
    if(num === 1) return true;
    let n = Math.floor(num / 2);
    for(let i = 1; i <= n; i++) {
        if(num === i * i) {
            return true;
        }
    }
    return false;
};

结果

Accepted

70/70 cases passed (1640 ms)

Your runtime beats 19.81 % of javascript submissions

Your memory usage beats 11.62 % of javascript submissions (39.1 MB)


文章作者: 阿汪同学
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 阿汪同学 !
评论
  目录