-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray447.java
More file actions
64 lines (60 loc) · 2 KB
/
Array447.java
File metadata and controls
64 lines (60 loc) · 2 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
package array;
import java.util.HashMap;
import java.util.Map;
/**
* @ProjectName: leetcode
* @Package: array
* @ClassName: Array447
* @Author: markey
* @Description:
* @Date: 2020/5/17 23:12
* @Version: 1.0
*/
public class Array447 {
public int numberOfBoomerangs(int[][] points) {
int count = 0;
for (int[] middle: points) {
// 记录每个其他点到这个点的距离,如果距离相同的点有n个,则回旋标的个数是n中选2个的种类数
Map<Double, Integer> map = new HashMap<>();
for (int[] point: points) {
if (point[0] == middle[0] && point[1] == middle[1]) {
// 相同点忽略
continue;
}
double distance = distance(middle, point);
map.put(distance, map.getOrDefault(distance, 0) + 1);
}
// 计算个数
for (double distance : map.keySet()) {
count += map.get(distance) * (map.get(distance) - 1);
}
}
return count;
}
// 暴力遍历,超时
public int numberOfBoomerangs1(int[][] points) {
int count = 0;
for (int[] middle: points) {
for (int[] left: points) {
if (left[0] == middle[0] && left[1] == middle[1]) {
// 相同点忽略
continue;
}
double distance = distance(middle, left);
for (int[] right: points) {
if (left[0] == right[0] && left[1] == right[1]) {
// 相同点忽略
continue;
}
if (distance(middle, right) == distance) {
count++;
}
}
}
}
return count;
}
private double distance(int[] point1, int[] point2) {
return Math.pow((point1[0] -point2[0]), 2.0) + Math.pow((point1[1] -point2[1]), 2);
}
}