Skip to content

Instantly share code, notes, and snippets.

@IngeFrodo
Last active May 9, 2020 18:13
Show Gist options
  • Save IngeFrodo/346301cc0784d29aa3507b3175a07c2a to your computer and use it in GitHub Desktop.
Save IngeFrodo/346301cc0784d29aa3507b3175a07c2a to your computer and use it in GitHub Desktop.
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