Created
January 16, 2017 10:54
-
-
Save NodoFox/88c56bc34040b48b146c6ab4d510b551 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public boolean isPerfectSquare(int num) { | |
long low = 0, high = num; | |
while(low <= high) { | |
long mid = low + (high - low)/2; | |
//int product = mid * mid; | |
// if(mid != 0 && product/mid != mid) { | |
// high = mid - 1; // integer overflow; | |
// continue; | |
// } | |
if(mid * mid == num) { | |
return true; | |
} | |
if(mid * mid > num) { | |
high = mid - 1; | |
} else { | |
low = mid + 1; | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment