Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.1k
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
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes from1 commit
Commits
Show all changes
8 commits Select commitHold shift + click to select a range
0a67677
0001 Solution in c language
pranjal03040431851f3
style: format code and docs with prettier
pranjal030404ef84796
changed the c language code and now it will pass all the testcases of it
pranjal030404c2c799d
Merge branch 'main' into main
pranjal030404846747a
Update README_EN.md
yanglbme78f06e2
Update README.md
yanglbmee6642a9
Update Solution.c
yanglbme276efed
Update README_EN.md
yanglbmeFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
NextNext commit
0001 Solution in c language
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
commit0a676772d8af548d73f7fd4910f03c3d4c481155
There are no files selected for viewing
59 changes: 59 additions & 0 deletionssolution/0000-0099/0001.Two Sum/README_EN.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletionssolution/0000-0099/0001.Two Sum/Solution.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff 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; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.