-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1_two_sum.c
More file actions
148 lines (116 loc) · 2.66 KB
/
1_two_sum.c
File metadata and controls
148 lines (116 loc) · 2.66 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <stdio.h>
#include <stdlib.h>
const int MEMORY_ALLOCATION_ERROR = 1;
typedef struct Node
{
int key;
int value;
struct Node *next;
} Node;
typedef struct HashMap
{
Node **nodes;
int size;
} HashMap;
int hash(int key, int size)
{
return abs(key) % size;
}
HashMap *createHashMap(int size)
{
HashMap *hashMap = (HashMap *)malloc(sizeof(HashMap));
if (hashMap == NULL)
{
perror("Error allocating memory for hashMap");
exit(MEMORY_ALLOCATION_ERROR);
}
hashMap->nodes = (Node **)malloc(sizeof(Node *) * size);
if (hashMap->nodes == NULL)
{
perror("Error allocating memory for nodes in hashMap");
// Free previously allocated memory
free(hashMap);
exit(MEMORY_ALLOCATION_ERROR);
}
hashMap->size = size;
for (int i = 0; i < size; i++)
{
hashMap->nodes[i] = NULL;
}
return hashMap;
}
void addToHashMap(HashMap *hashMap, int key, int value)
{
Node *newNode = (Node *)malloc(sizeof(Node));
if (newNode == NULL)
{
perror("Error allocating memory for newNode");
exit(MEMORY_ALLOCATION_ERROR);
}
newNode->key = key;
newNode->value = value;
int hashedKey = hash(key, hashMap->size);
newNode->next = hashMap->nodes[hashedKey];
hashMap->nodes[hashedKey] = newNode;
}
int getFromHashMap(HashMap *hashMap, int key)
{
int hashedKey = hash(key, hashMap->size);
Node *node = hashMap->nodes[hashedKey];
while (node != NULL && node->key != key)
{
node = node->next;
}
return node != NULL ? node->value : -1;
}
void freeHashMap(HashMap *hashMap)
{
for (int i = 0; i < hashMap->size; i++)
{
Node *nodeToFree = hashMap->nodes[i];
while (nodeToFree != NULL)
{
Node *temp = nodeToFree;
nodeToFree = nodeToFree->next;
free(temp);
}
}
free(hashMap->nodes);
free(hashMap);
}
int *twoSum(int *nums, int numsSize, int target, int *returnSize)
{
*returnSize = 2;
int *result = (int *)malloc(sizeof(int) * (*returnSize));
if (result == NULL)
{
perror("Error allocating memory for result");
exit(MEMORY_ALLOCATION_ERROR);
}
HashMap *hashMap = createHashMap(numsSize);
for (int i = 0; i < numsSize; i++)
{
int complement = target - nums[i];
int complementIndex = getFromHashMap(hashMap, complement);
if (complementIndex != -1)
{
result[0] = complementIndex;
result[1] = i;
break;
}
addToHashMap(hashMap, nums[i], i);
}
freeHashMap(hashMap);
return result;
}
int main()
{
int nums[] = {2, 7, 11, 15};
int numsSize = 4;
int target = 9;
int returnSize;
int *result = twoSum(nums, numsSize, target, &returnSize);
printf("[%d, %d]\n", result[0], result[1]);
free(result);
return 0;
}