We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e108dc8 commit 1553f95Copy full SHA for 1553f95
src/com/blankj/easy/_069/Solution.java
@@ -10,11 +10,19 @@
10
*/
11
public class Solution {
12
public int mySqrt(int x) {
13
- long n = x;
14
- while (n * n > x) {
15
- n = (n + x / n) >> 1;
+ if (x < 2) {
+ return x;
16
}
17
- return (int) n;
+
+ 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
26
27
28
public static void main(String[] args) {
0 commit comments