Created
March 2, 2018 10:24
-
-
Save sunb0002/137cdbe18b8c30475cedfc0d31e5bb39 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 int mySqrt(int x) { | |
if (x == 0 || x == 1) return x; | |
int start = 0, end = x; | |
while (end - start > 1) { | |
int mid = start + (end - start) / 2; | |
if (mid <= x / mid) { // Avoid int overflow | |
start = mid; | |
} else { | |
end = mid; | |
} | |
} | |
return start; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment