-
Notifications
You must be signed in to change notification settings - Fork 20
/
SqrtX.java
executable file
·46 lines (41 loc) · 989 Bytes
/
SqrtX.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* Implement int sqrt(int x).
* Compute and return the square root of x.
* <p>
* Accepted.
*/
public class SqrtX {
public int mySqrt(int x) {
// A tricky way to solve the problem.
// return (int) Math.sqrt(x);
if (x <= 0) {
return 0;
}
if (x <= 3) {
return 1;
}
int high = x / 2, low = 1;
// To avoid overflow.
if (x >= 46340 * 46340) {
return 46340;
}
if (high > 46340) {
high = 46340;
}
int mid = (high + 1) / 2;
do {
if (mid * mid > x) {
high = mid - 1;
} else if (mid * mid < x) {
if ((mid + 1) * (mid + 1) > x) {
return mid;
}
low = mid + 1;
} else {
return mid;
}
mid = (low + high) / 2;
} while (high > low);
return mid;
}
}