Last active
May 9, 2020 18:13
-
-
Save IngeFrodo/346301cc0784d29aa3507b3175a07c2a 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
class ValidPerfectSquare { | |
public boolean isPerfectSquare(int num) { | |
if (num < 2) { | |
return true; | |
} | |
long left = 1l; | |
long right = num; | |
long mid, product; | |
while (left <= right) { | |
mid = left + (right - left) / 2; | |
product = mid * mid; | |
if (product == num) { | |
return true; | |
} else if (product < num) { | |
left = mid + 1; | |
} else { | |
right = mid - 1; | |
} | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment