-
Notifications
You must be signed in to change notification settings - Fork 0
/
350. Intersection of Two Arrays II.c
65 lines (61 loc) · 2.3 KB
/
350. Intersection of Two Arrays II.c
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
/**
* Return an array of size *returnSize.
* Note: The returned array must be malloced, assume caller calls free().
*/
int* intersect(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize) {
int Size=(nums1Size>nums2Size)?nums1Size:nums2Size; // 先找最大值,及最小值
int smaller=(nums1Size>nums2Size)?nums2Size:nums1Size;
int *preturn=malloc(sizeof(int)*Size); // 分配最大空間
int i,j;
(*returnSize)=0; // 初始回傳大小為0
if (Size==nums2Size) { // 外圈為小值,內圈為大值
for (i=0;i<smaller;i++) {
for (j=0;j<Size;j++) {
if (*(nums1+i)==*(nums2+j)) { // 如果找到相同數字,意謂交集
*(preturn+(*returnSize))=*(nums1+i); // 回傳空間加1,並將交集放入回傳空間之中
*(nums2+j)=-1; // 改數值為-1,避免重複取數
(*returnSize)++;
break;
}
}
}
}else {
for (i=0;i<smaller;i++) {
for (j=0;j<Size;j++) {
if (*(nums2+i)==*(nums1+j)) {
*(preturn+(*returnSize))=*(nums2+i);
*(nums1+j)=-1;
(*returnSize)++;
break;
}
}
}
}
return preturn;
}
/**
* Return an array of size *returnSize.
* Note: The returned array must be malloced, assume caller calls free().
*/
/** C faster
int comparator(const void *a, const void *b) {
return *(int *) a > *(int *) b;
}
int* intersect(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize) {
qsort(nums1, nums1Size, sizeof(int), comparator);
qsort(nums2, nums2Size, sizeof(int), comparator);
*returnSize = 0;
int *res = malloc(nums1Size * sizeof(int));
for (int i = 0, j = 0; i < nums1Size && j < nums2Size; ) {
if (nums1[i] == nums2[j]) {
res[(*returnSize)++] = nums1[i];
i++, j++;
}
else if (nums1[i] < nums2[j])
i++;
else
j++;
}
return realloc(res, *returnSize * sizeof(int));
}
*/