forked from Somersames/JavaBasis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberofBoomerangs.java
More file actions
72 lines (62 loc) · 1.96 KB
/
NumberofBoomerangs.java
File metadata and controls
72 lines (62 loc) · 1.96 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
69
70
71
72
package Leetcode;
import java.util.HashMap;
import java.util.Map;
/**
* @author szh
* @create 2018-09-03 23:54
**/
public class NumberofBoomerangs {
public int numberOfBoomerangs(int[][] points) {
Map<Integer,Integer> map =new HashMap<>();
int res=0;
for(int i =0 ;i< points.length ;i++){
for(int j=0 ;j< points.length ;j++){
if(i == j){
continue;
}
int ditance =getDistance(points[i],points[j]);
map.put(ditance,map.getOrDefault(ditance,0)+1);
}
for(int val :map.values()){
res+=(val*2);
}
map.clear();
}
return res;
}
public int getDistance(int[] a ,int[] b){
// int dx =(a[0]-b[0]) < 0 ? b[0]- a[0] :a[0]-b[0];
int dx =a[0]-b[0];
// int dy=(a[1]-b[1]) < 0 ? b[1]- a[1] :a[1]-b[1];
int dy=a[1]-b[1] ;
return dx* dx + dy *dy;
}
public static void main(String[] args) {
// int[][] a =new int[][]{new int[]{0,0},new int[]{0,0},new int[]{0,0}};
int[][] a =new int[][]{{0,0},{1,0},{2,0}};
new NumberofBoomerangs().numberOfBoomerangs(a);
new NumberofBoomerangs().numberOfBoomerangs2(a);
}
public int numberOfBoomerangs2(int[][] points) {
int res = 0;
Map<Integer, Integer> map = new HashMap<>();
for(int i=0; i<points.length; i++) {
for(int j=0; j<points.length; j++) {
if(i == j)
continue;
int d = getDistance2(points[i], points[j]);
map.put(d, map.getOrDefault(d, 0) + 1);
}
for(int val : map.values()) {
res += val * (val-1);
}
map.clear();
}
return res;
}
private int getDistance2(int[] a, int[] b) {
int dx = a[0] - b[0];
int dy = a[1] - b[1];
return dx*dx + dy*dy;
}
}