1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
var isPerfectSquare = function (num) { if (num === 1) { return true; }
let [start, end] = [0, num - 1];
while (start <= end) { const middle = ~~((start + end) / 2); const pow = middle ** 2;
if (pow > num) { end = middle - 1; } else if (pow < num) { start = middle + 1; } else { return true; } }
return false; };
|