Skip to content

Commit 1553f95

Browse files
author
tyreke.xu
committed
add
1 parent e108dc8 commit 1553f95

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/com/blankj/easy/_069/Solution.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,19 @@
1010
*/
1111
public class Solution {
1212
public int mySqrt(int x) {
13-
long n = x;
14-
while (n * n > x) {
15-
n = (n + x / n) >> 1;
13+
if (x < 2) {
14+
return x;
1615
}
17-
return (int) n;
16+
17+
double x0 = x;
18+
double x1 = (x0 + x / x0) / 2.0;
19+
while (Math.abs(x0 - x1) >= 1) {
20+
x0 = x1;
21+
x1 = (x0 + x / x0) / 2.0;
22+
}
23+
24+
return (int) x1;
25+
1826
}
1927

2028
public static void main(String[] args) {

0 commit comments

Comments
 (0)