File tree Expand file tree Collapse file tree 3 files changed +142
-0
lines changed
solution/0000-0099/0001.Two Sum Expand file tree Collapse file tree 3 files changed +142
-0
lines changed Original file line number Diff line number Diff line change @@ -353,6 +353,55 @@ class Solution {
353353}
354354```
355355
356+ #### C
357+
358+ ``` c
359+ #include < stdlib.h>
360+
361+ int * twoSum (int* nums, int numsSize, int target, int* returnSize) {
362+ int capacity = 1;
363+ while (capacity < numsSize * 2) capacity <<= 1;
364+ int* keys = malloc(capacity * sizeof(int));
365+ int* vals = malloc(capacity * sizeof(int));
366+ char* used = calloc(capacity, sizeof(char));
367+ if (!keys || !vals || !used) {
368+ free(keys);
369+ free(vals);
370+ free(used);
371+ * returnSize = 0;
372+ return NULL;
373+ }
374+ for (int i = 0; i < numsSize; ++i) {
375+ int x = nums[ i] ;
376+ int y = target - x;
377+ unsigned int h = (unsigned int) y & (capacity - 1);
378+ while (used[ h] ) {
379+ if (keys[ h] == y) {
380+ int* res = malloc(2 * sizeof(int));
381+ res[ 0] = vals[ h] ;
382+ res[ 1] = i;
383+ * returnSize = 2;
384+ free(keys);
385+ free(vals);
386+ free(used);
387+ return res;
388+ }
389+ h = (h + 1) & (capacity - 1);
390+ }
391+ unsigned int h2 = (unsigned int) x & (capacity - 1);
392+ while (used[ h2] ) h2 = (h2 + 1) & (capacity - 1);
393+ used[ h2] = 1;
394+ keys[ h2] = x;
395+ vals[ h2] = i;
396+ }
397+ * returnSize = 0;
398+ free(keys);
399+ free(vals);
400+ free(used);
401+ return NULL;
402+ }
403+ ```
404+
356405<!-- tabs:end -->
357406
358407<!-- solution:end -->
Original file line number Diff line number Diff line change @@ -350,6 +350,55 @@ class Solution {
350350}
351351```
352352
353+ #### C
354+
355+ ``` c
356+ #include < stdlib.h>
357+
358+ int * twoSum (int* nums, int numsSize, int target, int* returnSize) {
359+ int capacity = 1;
360+ while (capacity < numsSize * 2) capacity <<= 1;
361+ int* keys = malloc(capacity * sizeof(int));
362+ int* vals = malloc(capacity * sizeof(int));
363+ char* used = calloc(capacity, sizeof(char));
364+ if (!keys || !vals || !used) {
365+ free(keys);
366+ free(vals);
367+ free(used);
368+ * returnSize = 0;
369+ return NULL;
370+ }
371+ for (int i = 0; i < numsSize; ++i) {
372+ int x = nums[ i] ;
373+ int y = target - x;
374+ unsigned int h = (unsigned int) y & (capacity - 1);
375+ while (used[ h] ) {
376+ if (keys[ h] == y) {
377+ int* res = malloc(2 * sizeof(int));
378+ res[ 0] = vals[ h] ;
379+ res[ 1] = i;
380+ * returnSize = 2;
381+ free(keys);
382+ free(vals);
383+ free(used);
384+ return res;
385+ }
386+ h = (h + 1) & (capacity - 1);
387+ }
388+ unsigned int h2 = (unsigned int) x & (capacity - 1);
389+ while (used[ h2] ) h2 = (h2 + 1) & (capacity - 1);
390+ used[ h2] = 1;
391+ keys[ h2] = x;
392+ vals[ h2] = i;
393+ }
394+ * returnSize = 0;
395+ free(keys);
396+ free(vals);
397+ free(used);
398+ return NULL;
399+ }
400+ ```
401+
353402<!-- tabs:end -->
354403
355404<!-- solution:end -->
Original file line number Diff line number Diff line change 1+ #include <stdlib.h>
2+
3+ int * twoSum (int * nums , int numsSize , int target , int * returnSize ) {
4+ int capacity = 1 ;
5+ while (capacity < numsSize * 2 ) capacity <<= 1 ;
6+ int * keys = malloc (capacity * sizeof (int ));
7+ int * vals = malloc (capacity * sizeof (int ));
8+ char * used = calloc (capacity , sizeof (char ));
9+ if (!keys || !vals || !used ) {
10+ free (keys );
11+ free (vals );
12+ free (used );
13+ * returnSize = 0 ;
14+ return NULL ;
15+ }
16+ for (int i = 0 ; i < numsSize ; ++ i ) {
17+ int x = nums [i ];
18+ int y = target - x ;
19+ unsigned int h = (unsigned int ) y & (capacity - 1 );
20+ while (used [h ]) {
21+ if (keys [h ] == y ) {
22+ int * res = malloc (2 * sizeof (int ));
23+ res [0 ] = vals [h ];
24+ res [1 ] = i ;
25+ * returnSize = 2 ;
26+ free (keys );
27+ free (vals );
28+ free (used );
29+ return res ;
30+ }
31+ h = (h + 1 ) & (capacity - 1 );
32+ }
33+ unsigned int h2 = (unsigned int ) x & (capacity - 1 );
34+ while (used [h2 ]) h2 = (h2 + 1 ) & (capacity - 1 );
35+ used [h2 ] = 1 ;
36+ keys [h2 ] = x ;
37+ vals [h2 ] = i ;
38+ }
39+ * returnSize = 0 ;
40+ free (keys );
41+ free (vals );
42+ free (used );
43+ return NULL ;
44+ }
You can’t perform that action at this time.
0 commit comments