File tree Expand file tree Collapse file tree 1 file changed +91
-0
lines changed Expand file tree Collapse file tree 1 file changed +91
-0
lines changed Original file line number Diff line number Diff line change 1+ struct node {
2+ int key ;
3+ int value ;
4+ struct node * left ;
5+ struct node * right ;
6+ };
7+
8+ struct node * node_add (struct node * head , int key )
9+ {
10+ struct node * new_node , * cur ;
11+ new_node = malloc (sizeof (struct node ));
12+ new_node -> key = key ;
13+ new_node -> value = 1 ;
14+ new_node -> left = NULL ;
15+ new_node -> right = NULL ;
16+ if (head == NULL )
17+ return new_node ;
18+
19+ cur = head ;
20+ while (cur ){
21+ if (cur -> key == key ){
22+ free (new_node );
23+ cur -> value ++ ;
24+ break ;
25+ }else if (cur -> key > key ){
26+ if (cur -> left == NULL ){
27+ cur -> left = new_node ;
28+ break ;
29+ }
30+ cur = cur -> left ;
31+ }else {
32+ if (cur -> right == NULL ){
33+ cur -> right = new_node ;
34+ break ;
35+ }
36+ cur = cur -> right ;
37+ }
38+ }
39+
40+ return head ;
41+ }
42+
43+ void node_free (struct node * head )
44+ {
45+ if (head == NULL )
46+ return ;
47+ node_free (head -> left );
48+ node_free (head -> right );
49+ free (head );
50+ }
51+
52+ int get_value_by_key (struct node * head , int key )
53+ {
54+ struct node * cur = head ;
55+ while (cur ){
56+ if (cur -> key == key )
57+ break ;
58+ if (cur -> key > key )
59+ cur = cur -> left ;
60+ else
61+ cur = cur -> right ;
62+ }
63+
64+ return cur ? cur -> value : 0 ;
65+ }
66+
67+ int dist (int x1 , int y1 , int x2 , int y2 )
68+ {
69+ return (x2 - x1 ) * (x2 - x1 ) + (y2 - y1 ) * (y2 - y1 );
70+ }
71+
72+ int numberOfBoomerangs (int * * points , int pointsRowSize , int pointsColSize ) {
73+ int i ,j ;
74+ struct node * head ;
75+ int ret = 0 ;
76+ int key ;
77+
78+ for (i = 0 ; i < pointsRowSize ; ++ i ){
79+ head = NULL ;
80+ for (j = 0 ; j < pointsRowSize ; ++ j ){
81+ if (i == j )
82+ continue ;
83+ key = dist (points [i ][0 ], points [i ][1 ], points [j ][0 ], points [j ][1 ]);
84+ ret += (2 * get_value_by_key (head , key ));
85+ head = node_add (head , key );
86+ }
87+ node_free (head );
88+ }
89+
90+ return ret ;
91+ }
You can’t perform that action at this time.
0 commit comments