Created
May 10, 2020 08:08
-
-
Save vedantb/223267865e6fe4b26b22ed16e4bb7ebf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {number} num | |
* @return {boolean} | |
*/ | |
var isPerfectSquare = function(num) { | |
if (num < 2) return true; | |
let left = 2; | |
let right = Math.floor(num / 2); | |
while (left <= right) { | |
let mid = left + Math.floor((right - left) / 2); | |
let guess = mid * mid; | |
if (guess === num) return true; | |
if (guess > num) { | |
right = mid - 1; | |
} else { | |
left = mid + 1; | |
} | |
} | |
return false; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment