-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0705-design-hashset.c
147 lines (122 loc) · 3.71 KB
/
0705-design-hashset.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
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
// -------------------- Complex solution --------------------
// --------- LinkedList ---------
typedef struct Node {
int value;
struct Node *next;
} Node;
typedef struct LinkedListHead {
Node *next;
} LinkedListHead;
Node *newNode(int key) {
Node *node = (Node *)malloc(sizeof(Node));
node->value = key;
node->next = NULL;
return node;
}
LinkedListHead *newLinkedList() {
LinkedListHead *head = (LinkedListHead *)malloc(sizeof(LinkedListHead));
head->next = NULL;
return head;
}
bool linkedListContains(LinkedListHead *head, int key) {
Node *currentNode = head->next;
while (currentNode) {
if (currentNode->value == key) return true;
currentNode = currentNode->next;
}
return false;
}
void linkedListPush(LinkedListHead *head, int key) {
Node *node = newNode(key);
node->next = head->next;
head->next = node;
}
void linkedListRemove(LinkedListHead *head, int key) {
Node *previousNode = head->next;
if (previousNode == NULL) return;
if (previousNode->value == key) {
head->next = previousNode->next;
free(previousNode);
return;
}
Node *currentNode = previousNode->next;
while (currentNode) {
if (currentNode->value == key) {
previousNode->next = currentNode->next;
free(currentNode);
return;
}
currentNode = currentNode->next;
}
}
void freeLinkedList(LinkedListHead *head) {
Node *previousNode = head->next;
if (previousNode) {
Node *currentNode = previousNode->next;
while (currentNode) {
free(previousNode);
previousNode = currentNode;
currentNode = currentNode->next;
}
free(previousNode);
}
free(head);
}
// --------- LinkedList ---------
// ----------- HashSet ----------
typedef struct {
LinkedListHead *head;
} MyHashSet;
#define HASH_SET_DEFAULT_SIZE 10000
MyHashSet *myHashSetCreate() {
MyHashSet *obj =
(MyHashSet *)malloc(sizeof(MyHashSet) * HASH_SET_DEFAULT_SIZE);
for (int i = 0; i < HASH_SET_DEFAULT_SIZE; i++) {
obj[i].head = newLinkedList();
}
return obj;
}
int getHashPosition(int key) { return key % HASH_SET_DEFAULT_SIZE; }
void myHashSetAdd(MyHashSet *obj, int key) {
int pos = getHashPosition(key);
if (obj[pos].head == NULL) obj[pos].head = newLinkedList();
if (!linkedListContains(obj[pos].head, key))
linkedListPush(obj[pos].head, key);
}
void myHashSetRemove(MyHashSet *obj, int key) {
linkedListRemove(obj[getHashPosition(key)].head, key);
}
bool myHashSetContains(MyHashSet *obj, int key) {
LinkedListHead *head = obj[getHashPosition(key)].head;
if (head == NULL || head->next == NULL) return false;
return linkedListContains(head, key);
}
void myHashSetFree(MyHashSet *obj) {
for (int i = 0; i < HASH_SET_DEFAULT_SIZE; i++)
if (obj[i].head != NULL) freeLinkedList(obj[i].head);
free(obj);
}
// ----------- HashSet ----------
// -------------------- Complex solution --------------------
// -------------------- Simpler and faster solution --------------------
typedef struct {
bool *values;
} MyHashSet;
MyHashSet *myHashSetCreate() {
MyHashSet *obj = (MyHashSet *)malloc(sizeof(MyHashSet));
obj->values = (bool *)calloc(1000001, sizeof(bool));
return obj;
}
void myHashSetAdd(MyHashSet *obj, int key) { obj->values[key] = true; }
void myHashSetRemove(MyHashSet *obj, int key) { obj->values[key] = false; }
bool myHashSetContains(MyHashSet *obj, int key) { return obj->values[key]; }
void myHashSetFree(MyHashSet *obj) { free(obj); }
// -------------------- Simpler and faster solution --------------------
/**
* Your MyHashSet struct will be instantiated and called as such:
* MyHashSet* obj = myHashSetCreate();
* myHashSetAdd(obj, key);
* myHashSetRemove(obj, key);
* bool param_3 = myHashSetContains(obj, key);
* myHashSetFree(obj);
*/