Skip to content

Commit ef84796

Browse files
committed
changed the c language code and now it will pass all the testcases of it
1 parent 31851f3 commit ef84796

File tree

2 files changed

+18
-96
lines changed

2 files changed

+18
-96
lines changed

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

Lines changed: 9 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -353,58 +353,19 @@ class Solution {
353353
#### C
354354

355355
```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-
}
389356
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;
357+
for (int i = 0; i < numsSize; i++) {
358+
for (int j = i + 1; j < numsSize; j++) {
359+
if (nums[i] + nums[j] == target) {
360+
int* result = (int*)malloc(2 * sizeof(int));
361+
result[0] = i;
362+
result[1] = j;
363+
*returnSize = 2;
364+
return result;
365+
}
402366
}
403-
insert(map, x, i);
404367
}
405368
*returnSize = 0;
406-
free(map);
407-
free(result);
408369
return NULL;
409370
}
410371
```
Lines changed: 9 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,15 @@
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-
}
341
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;
2+
for (int i = 0; i < numsSize; i++) {
3+
for (int j = i + 1; j < numsSize; j++) {
4+
if (nums[i] + nums[j] == target) {
5+
int* result = (int*) malloc(2 * sizeof(int));
6+
result[0] = i;
7+
result[1] = j;
8+
*returnSize = 2;
9+
return result;
10+
}
4711
}
48-
insert(map, x, i);
4912
}
5013
*returnSize = 0;
51-
free(map);
52-
free(result);
5314
return NULL;
5415
}

0 commit comments

Comments
 (0)