Created
March 15, 2017 00:18
-
-
Save gcrfelix/8a21945ce98cc0486b937f85250b8735 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
public class Solution { | |
public boolean isPerfectSquare(int num) { | |
if(num <= 0) return false; | |
int left = 0, right = num; | |
while(left <= right) { | |
long mid = left + (right-left)/2; | |
if(mid * mid == num) { | |
return true; | |
} else if(mid * mid > num) { | |
right = (int)mid - 1; | |
} else { | |
left = (int)mid + 1; | |
} | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment