AFFILIATE LINKS
Great resource I use to learn algorithms.
40% off Tech Interview Pro: http://techinterviewpro.com/terriblewhiteboard
20% off CoderPro: http://coderpro.com/terriblewhiteboard
Here is the full implementation.
/**
* @param {number} num
* @return {boolean}
*/
let isPerfectSquare = function(num) {
if (num < 1) {
return false;
}
if (num === 1) {
return true;
}
let left = 1;
let right = num;
while (left <= right) {
let mid = left + Math.floor((right - left) / 2);
if (mid * mid === num) {
return true;
} else if (mid * mid < num) {
left = mid + 1;
} else if (mid * mid > num) {
right = mid - 1;
}
}
return false;
};