Last active
April 23, 2024 17:30
-
-
Save yin1999/a5b96c566730e19c41baf86d9d782ae7 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
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