File tree Expand file tree Collapse file tree 2 files changed +113
-0
lines changed
solution/0000-0099/0001.Two Sum Expand file tree Collapse file tree 2 files changed +113
-0
lines changed Original file line number Diff line number Diff line change @@ -350,6 +350,65 @@ class Solution {
350
350
}
351
351
```
352
352
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
+
353
412
<!-- tabs:end -->
354
413
355
414
<!-- solution:end -->
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments