forked from lolosssss/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path349_intersection_of_two_arrays.c
59 lines (49 loc) · 1.11 KB
/
349_intersection_of_two_arrays.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
/**
* Description : Intersection of Two Arrays
* Given two arrays, write a function to compute their intersect-
* ion.
* Author : Evan Lau
* Date : 2016/05/19
*/
#include <stdio.h>
#include <stdlib.h>
int *intersection(int *nums1, int nums1Size, int *nums2, int nums2Size,
int *returnSize)
{
int arr1[1000] = {0};
int arr2[1000] = {0};
int i;
int min = nums1Size > nums2Size ? nums2Size : nums1Size;
int *ret = NULL;
ret = (int *)malloc(sizeof(int) * min);
for (i = 0; i < nums1Size; i++)
{
arr1[nums1[i]] = 1;
}
for (i = 0; i < nums2Size; i++)
{
arr2[nums2[i]] = 1;
}
for (i = 0; i < 1000; i++)
{
if (arr1[i] != 0 && arr2[i] != 0)
{
ret[(*returnSize)++] = i;
}
}
return ret;
}
int main(void)
{
int arr1[] = {2, 1};
int arr2[] = {1, 2};
int *res = NULL;
int size = 0;
res = intersection(arr1, 2, arr2, 2, &size);
for (int i = 0; i < size; i++)
{
printf("%d ", res[i]);
}
printf("\n");
return 0;
}