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

0001 Solution in c language#4422

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
yanglbme merged 8 commits intodoocs:mainfrompranjal030404:main
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
NextNext commit
0001 Solution in c language
  • Loading branch information
@pranjal030404
pranjal030404 committedMay 22, 2025
commit0a676772d8af548d73f7fd4910f03c3d4c481155
59 changes: 59 additions & 0 deletionssolution/0000-0099/0001.Two Sum/README_EN.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -350,6 +350,65 @@ class Solution {
}
```

#### C

```c
#include <stdio.h>
#include <stdlib.h>
#define TABLE_SIZE 2048
typedef struct {
int key;
int value;
int used;
} Entry;
int hash(int key) {
return abs(key) % TABLE_SIZE;
}
void insert(Entry *table, int key, int value) {
int idx = hash(key);
while (table[idx].used) {
idx = (idx + 1) % TABLE_SIZE;
}
table[idx].key = key;
table[idx].value = value;
table[idx].used = 1;
}
int contains(Entry *table, int key, int *out_value) {
int idx = hash(key);
int start = idx;
while (table[idx].used) {
if (table[idx].key == key) {
*out_value = table[idx].value;
return 1;
}
idx = (idx + 1) % TABLE_SIZE;
if (idx == start) break;
}
return 0;
}
int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
Entry *map = (Entry*)calloc(TABLE_SIZE, sizeof(Entry));
int *result = (int*)malloc(2 * sizeof(int));
for (int i = 0; i < numsSize; ++i) {
int x = nums[i];
int y = target - x;
int foundIndex;
if (contains(map, y, &foundIndex)) {
result[0] = foundIndex;
result[1] = i;
*returnSize = 2;
free(map);
return result;
}
insert(map, x, i);
}
*returnSize = 0;
free(map);
free(result);
return NULL;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
54 changes: 54 additions & 0 deletionssolution/0000-0099/0001.Two Sum/Solution.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
#include <stdio.h>
#include <stdlib.h>
#define TABLE_SIZE 2048
typedef struct {
int key;
int value;
int used;
} Entry;
int hash(int key) {
return abs(key) % TABLE_SIZE;
}
void insert(Entry* table, int key, int value) {
int idx = hash(key);
while (table[idx].used) {
idx = (idx + 1) % TABLE_SIZE;
}
table[idx].key = key;
table[idx].value = value;
table[idx].used = 1;
}
int contains(Entry* table, int key, int* out_value) {
int idx = hash(key);
int start = idx;
while (table[idx].used) {
if (table[idx].key == key) {
*out_value = table[idx].value;
return 1;
}
idx = (idx + 1) % TABLE_SIZE;
if (idx == start) break;
}
return 0;
}
int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
Entry* map = (Entry*) calloc(TABLE_SIZE, sizeof(Entry));
int* result = (int*) malloc(2 * sizeof(int));
for (int i = 0; i < numsSize; ++i) {
int x = nums[i];
int y = target - x;
int foundIndex;
if (contains(map, y, &foundIndex)) {
result[0] = foundIndex;
result[1] = i;
*returnSize = 2;
free(map);
return result;
}
insert(map, x, i);
}
*returnSize = 0;
free(map);
free(result);
return NULL;
}

[8]ページ先頭

©2009-2025 Movatter.jp