Skip to content

Instantly share code, notes, and snippets.

@yin1999
Last active April 23, 2024 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yin1999/a5b96c566730e19c41baf86d9d782ae7 to your computer and use it in GitHub Desktop.
Save yin1999/a5b96c566730e19c41baf86d9d782ae7 to your computer and use it in GitHub Desktop.
class Solution {
public:
int mySqrt(int x) {
int min = 0;
int max = 46340;
while (min < max) {
const int mid = (min + max) >> 1;
const int s = mid * mid;
if (s > x) {
max = mid-1;
} else if (s < x) {
min = mid + 1;
} else {
return mid;
}
}
if (min*min <= x) {
return min;
}
return min - 1;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment