-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0697-degree-of-an-array.c
More file actions
39 lines (37 loc) · 884 Bytes
/
Copy path0697-degree-of-an-array.c
File metadata and controls
39 lines (37 loc) · 884 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
//uthash散列表
typedef struct {
int num;
int count;
int start;
int end;
UT_hash_handle hh;
} numhash;
numhash *numh = NULL;
int findShortestSubArray(int* nums, int numsSize) {
numhash *s = NULL;
numhash *tmp = NULL;
int max = 1;
int size = numsSize;
numh = NULL;
for(int i = 0; i < numsSize; i++) {
HASH_FIND_INT(numh, &nums[i], s);
if(s == NULL) {
s = (numhash*)malloc(sizeof(numhash));
s->num = nums[i];
s->count = 1;
s->start = i;
s->end = i;
HASH_ADD_INT(numh, num, s);
} else {
s->count++;
s->end = i;
max = fmax(max, s->count);
}
}
HASH_ITER(hh, numh, s, tmp) {
if(s->count == max) {
size = fmin(size, s->end - s->start + 1);
}
}
return size;
}