-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_367.java
More file actions
47 lines (42 loc) · 1001 Bytes
/
Copy pathP_367.java
File metadata and controls
47 lines (42 loc) · 1001 Bytes
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
package leetcode.easy;
public class P_367 {
// O(log(num))
public boolean newtonMethod(int num) {
long x = num;
while (x * x > num) {
x = (x + num / x) >> 1;
}
return x * x == num;
}
// O(log(num))
public boolean binarySearch(int num) {
long lo = 1, hi = num;
while (lo < hi) {
final long mid = (lo + hi) >>> 1;
if (mid * mid < num) {
lo = mid + 1;
} else {
hi = mid;
}
}
return lo * lo == num;
}
// O(sqrt(num))
public boolean isPerfectSquare(int num) {
int i = 1;
while (num > 0) {
num -= i;
i += 2;
}
return num == 0;
}
// O(sqrt(num))
public boolean isPerfectSquare2(int num) {
for (int i = 1; i <= 46340; i++) {
if (i * i == num) {
return true;
}
}
return false;
}
}