You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
412 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package class29;
public class Problem_0069_SqrtX {
// x一定非负输入可以保证
public static int mySqrt(int x) {
if (x == 0) {
return 0;
}
if (x < 3) {
return 1;
}
// x >= 3
long ans = 1;
long L = 1;
long R = x;
long M = 0;
while (L <= R) {
M = (L + R) / 2;
if (M * M <= x) {
ans = M;
L = M + 1;
} else {
R = M - 1;
}
}
return (int) ans;
}
}