Skip to content

Instantly share code, notes, and snippets.

@gcrfelix
Created March 15, 2017 00:18
Show Gist options
  • Save gcrfelix/8a21945ce98cc0486b937f85250b8735 to your computer and use it in GitHub Desktop.
Save gcrfelix/8a21945ce98cc0486b937f85250b8735 to your computer and use it in GitHub Desktop.
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