-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode69.java
More file actions
68 lines (60 loc) · 1.63 KB
/
LeetCode69.java
File metadata and controls
68 lines (60 loc) · 1.63 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
public class LeetCode69 {
public static void main(String[] args) {
// 输入:x = 4
// 输出:2
System.out.println(new Solution69_1().mySqrt(4));
// 输入:x = 8
// 输出:2
System.out.println(new Solution69_1().mySqrt(8));
}
}
class Solution69_1 {
public int mySqrt(int x) {
int l = 0, r = x;
int mid;
while (l <= r) {
mid = l + (r - l) / 2;
if ((long) mid * mid < x) {
l = mid + 1;
} else if ((long) mid * mid > x) {
r = mid - 1;
} else {
return mid;
}
}
return l - 1;
}
}
class Solution69_2 {
// 「袖珍计算器算法」是一种用指数函数exp和对数函数ln代替平方根函数的方法。我们通过有限的可以使用的数学函数,得到我们想要计算的结果。
public int mySqrt(int x) {
if (x == 0) {
return 0;
}
int ans = (int) Math.exp(0.5 * Math.log(x)); // 浮点数计算会带来误差,需要进一步处理
if ((ans + 1) * (ans + 1) == x) {
return ans + 1;
}
if (ans * ans > x) {
return ans - 1;
}
return ans;
}
}
class Solution69_3 {
// 牛顿迭代法
public int mySqrt(int x) {
if (x == 0) {
return 0;
}
double C = x, x0 = x;
while (true) {
double xi = 0.5 * (x0 + C / x0);
if (Math.abs(x0 - xi) < 1e-7) {
break;
}
x0 = xi;
}
return (int) x0;
}
}