Skip to content

Commit 0a67677

Browse files
committed
0001 Solution in c language
1 parent 51a192d commit 0a67677

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

solution/0000-0099/0001.Two Sum/README_EN.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,65 @@ class Solution {
350350
}
351351
```
352352

353+
#### C
354+
355+
```c
356+
#include <stdio.h>
357+
#include <stdlib.h>
358+
#define TABLE_SIZE 2048
359+
typedef struct {
360+
int key;
361+
int value;
362+
int used;
363+
} Entry;
364+
int hash(int key) {
365+
return abs(key) % TABLE_SIZE;
366+
}
367+
void insert(Entry *table, int key, int value) {
368+
int idx = hash(key);
369+
while (table[idx].used) {
370+
idx = (idx + 1) % TABLE_SIZE;
371+
}
372+
table[idx].key = key;
373+
table[idx].value = value;
374+
table[idx].used = 1;
375+
}
376+
int contains(Entry *table, int key, int *out_value) {
377+
int idx = hash(key);
378+
int start = idx;
379+
while (table[idx].used) {
380+
if (table[idx].key == key) {
381+
*out_value = table[idx].value;
382+
return 1;
383+
}
384+
idx = (idx + 1) % TABLE_SIZE;
385+
if (idx == start) break;
386+
}
387+
return 0;
388+
}
389+
int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
390+
Entry *map = (Entry*)calloc(TABLE_SIZE, sizeof(Entry));
391+
int *result = (int*)malloc(2 * sizeof(int));
392+
for (int i = 0; i < numsSize; ++i) {
393+
int x = nums[i];
394+
int y = target - x;
395+
int foundIndex;
396+
if (contains(map, y, &foundIndex)) {
397+
result[0] = foundIndex;
398+
result[1] = i;
399+
*returnSize = 2;
400+
free(map);
401+
return result;
402+
}
403+
insert(map, x, i);
404+
}
405+
*returnSize = 0;
406+
free(map);
407+
free(result);
408+
return NULL;
409+
}
410+
```
411+
353412
<!-- tabs:end -->
354413
355414
<!-- solution:end -->
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#define TABLE_SIZE 2048
4+
typedef struct {
5+
int key;
6+
int value;
7+
int used;
8+
} Entry;
9+
int hash(int key) {
10+
return abs(key) % TABLE_SIZE;
11+
}
12+
void insert(Entry* table, int key, int value) {
13+
int idx = hash(key);
14+
while (table[idx].used) {
15+
idx = (idx + 1) % TABLE_SIZE;
16+
}
17+
table[idx].key = key;
18+
table[idx].value = value;
19+
table[idx].used = 1;
20+
}
21+
int contains(Entry* table, int key, int* out_value) {
22+
int idx = hash(key);
23+
int start = idx;
24+
while (table[idx].used) {
25+
if (table[idx].key == key) {
26+
*out_value = table[idx].value;
27+
return 1;
28+
}
29+
idx = (idx + 1) % TABLE_SIZE;
30+
if (idx == start) break;
31+
}
32+
return 0;
33+
}
34+
int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
35+
Entry* map = (Entry*) calloc(TABLE_SIZE, sizeof(Entry));
36+
int* result = (int*) malloc(2 * sizeof(int));
37+
for (int i = 0; i < numsSize; ++i) {
38+
int x = nums[i];
39+
int y = target - x;
40+
int foundIndex;
41+
if (contains(map, y, &foundIndex)) {
42+
result[0] = foundIndex;
43+
result[1] = i;
44+
*returnSize = 2;
45+
free(map);
46+
return result;
47+
}
48+
insert(map, x, i);
49+
}
50+
*returnSize = 0;
51+
free(map);
52+
free(result);
53+
return NULL;
54+
}

0 commit comments

Comments
 (0)