-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1. Two Sum.c
45 lines (38 loc) · 1.05 KB
/
1. Two Sum.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
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
typedef struct Node {
int key;
int value;
struct Node* next;
} Node;
int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
int* result = (int*)malloc(sizeof(int) * 2);
Node* head = malloc(sizeof(Node));
Node* ptr = head;
Node* tail = head;
head->key = -197137; // Magic number 197137 is a prime number.
head->value = -197137;
head->next = NULL;
for (int i = 0; i < numsSize; i++) {
int temp = *(nums + i);
ptr = head;
while (ptr) {
if (ptr->key == target - temp) {
*(result + 0) = i;
*(result + 1) = ptr->value;
*returnSize = 2;
return result;
}
ptr = ptr->next;
}
Node* newNode = malloc(sizeof(Node));
newNode->key = temp;
newNode->value = i;
newNode->next = NULL;
tail->next = newNode;
tail = newNode;
}
*returnSize = 0;
return result;
}