-
Notifications
You must be signed in to change notification settings - Fork 481
/
two_sum.c
91 lines (84 loc) · 1.9 KB
/
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
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
/* Note: The returned array must be malloced, assume caller calls free().
*/
#include <stdio.h>
#include <stdlib.h>
#define SIZE 100;
struct hashtable_item
{
int key;
int value;
struct hashtable_item *next;
};
int get_hashindex(int key)
{
key = abs(key);
return ((key%100));
}
void insert(struct hashtable_item *hashmap[], int num, int loc)
{
int hash_index;
struct hashtable_item *temp, *current;
hash_index = get_hashindex(num);
temp = malloc(sizeof(struct hashtable_item));
temp->key = num;
temp->value = loc;
temp->next = NULL;
if (hashmap[hash_index] == NULL)
{
hashmap[hash_index] = temp;
}
else
{
current = hashmap[hash_index];
while (current->next != NULL)
current = current->next;
current->next = temp;
}
}
int get_value(struct hashtable_item *hashmap[], int num)
{
int hash_index, value;
struct hashtable_item *current;
value = INT_MAX;
//found = 0;
hash_index = get_hashindex(num);
current = hashmap[hash_index];
while (current != NULL)
{
if (current->key == num)
{
//found=1;
value = current->value;
break;
}
current = current->next;
}
return value;
}
int* twoSum(int* nums, int numsSize, int target)
{
int found;
//int SIZE = 100;
struct hashtable_item *hashmap[100];
int *sum;
sum = malloc(2*sizeof(int));
sum[0] = -1;
sum[1] = -1;
for (int i = 0; i < 100; ++i)
hashmap[i] = NULL;
for (int i = 0; i < numsSize; ++i)
{
if (get_value(hashmap, nums[i]) == INT_MAX) //if doesnt exist
{
insert(hashmap, target-nums[i], i);
}
else
{
found = 1;
sum[0] = i;
sum[1] = get_value(hashmap, nums[i]);
break;
}
}
return sum;
}