Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9eb9ca9

Browse files
feat: add c solution to lc problem: No.0001 (#4422)
1 parentbd0eb2c commit9eb9ca9

File tree

3 files changed

+142
-0
lines changed

3 files changed

+142
-0
lines changed

‎solution/0000-0099/0001.Two Sum/README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff 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 -->

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff 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 -->
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include<stdlib.h>
2+
3+
int*twoSum(int*nums,intnumsSize,inttarget,int*returnSize) {
4+
intcapacity=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+
returnNULL;
15+
}
16+
for (inti=0;i<numsSize;++i) {
17+
intx=nums[i];
18+
inty=target-x;
19+
unsignedinth= (unsignedint)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+
returnres;
30+
}
31+
h= (h+1)& (capacity-1);
32+
}
33+
unsignedinth2= (unsignedint)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+
returnNULL;
44+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp