Initial commit.
This commit is contained in:
commit
38afea4824
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
*.dSYM
|
1
.prettierignore
Normal file
1
.prettierignore
Normal file
@ -0,0 +1 @@
|
||||
*.md
|
5
README.md
Normal file
5
README.md
Normal file
@ -0,0 +1,5 @@
|
||||
# Jesse Brault: LeetCode Solutions
|
||||
|
||||
These are my LeetCode solutions. They are a work in progress. I am trying to them in C, since I want to have a good
|
||||
language challenge and get good performance in the process. Also, C is not "batteries included"—I'll have to write my
|
||||
data structures by hand and optimize them as needed.
|
1
two-sum/.gitignore
vendored
Normal file
1
two-sum/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
twoSum
|
5
two-sum/Makefile
Normal file
5
two-sum/Makefile
Normal file
@ -0,0 +1,5 @@
|
||||
twoSum: runTwoSum.c twoSum.c
|
||||
gcc -Wall -std=gnu99 -g runTwoSum.c -o twoSum
|
||||
|
||||
run: twoSum
|
||||
./twoSum
|
26
two-sum/runTwoSum.c
Normal file
26
two-sum/runTwoSum.c
Normal file
@ -0,0 +1,26 @@
|
||||
#include "./twoSum.c"
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
int nums[] = { 0, 4, 3, 0 };
|
||||
int returnSize = 0;
|
||||
int *result = twoSum(nums, 4, 0, &returnSize);
|
||||
bool foundFirst, foundSecond;
|
||||
for (int i = 0; i < returnSize; i++) {
|
||||
switch (result[i]) {
|
||||
case 0:
|
||||
foundFirst = true;
|
||||
break;
|
||||
case 3:
|
||||
foundSecond = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (foundFirst && foundSecond) {
|
||||
return 0;
|
||||
} else {
|
||||
fprintf(stderr, "Did not find indices [0, 1]\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
98
two-sum/twoSum.c
Normal file
98
two-sum/twoSum.c
Normal file
@ -0,0 +1,98 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct {
|
||||
int key, value;
|
||||
} HashMapEntry;
|
||||
|
||||
typedef struct {
|
||||
int size;
|
||||
HashMapEntry **entries;
|
||||
} HashMap;
|
||||
|
||||
HashMap *makeHashMap(int size) {
|
||||
HashMap *m = malloc(sizeof(HashMap));
|
||||
m->size = size;
|
||||
m->entries = malloc(size * sizeof(HashMapEntry*));
|
||||
for (int i = 0; i < size; i++) {
|
||||
m->entries[i] = NULL;
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
int hashCode(HashMap *m, int key) {
|
||||
return ((unsigned int) key * 2654435761U) % m->size;
|
||||
}
|
||||
|
||||
HashMapEntry *makeEntry(int key, int value) {
|
||||
HashMapEntry *e = malloc(sizeof(HashMapEntry));
|
||||
e->key = key;
|
||||
e->value = value;
|
||||
return e;
|
||||
}
|
||||
|
||||
#define PUT_SUCCESS 1
|
||||
#define PUT_FAIL 0
|
||||
|
||||
int putAt(HashMap *m, HashMapEntry *e, int index) {
|
||||
if (m->entries[index] == NULL) {
|
||||
m->entries[index] = e;
|
||||
return PUT_SUCCESS;
|
||||
}
|
||||
return PUT_FAIL;
|
||||
}
|
||||
|
||||
int put(HashMap *m, int key, int value) {
|
||||
int hash = hashCode(m, key);
|
||||
HashMapEntry *e = makeEntry(key, value);
|
||||
for (int i = hash; i < m->size; i++) {
|
||||
if (putAt(m, e, i)) return PUT_SUCCESS;
|
||||
}
|
||||
for (int i = 0; i < hash; i++) {
|
||||
if (putAt(m, e,i)) return PUT_SUCCESS;
|
||||
}
|
||||
return PUT_FAIL;
|
||||
}
|
||||
|
||||
HashMapEntry *get(HashMap *m, int key) {
|
||||
int hash = hashCode(m, key);
|
||||
for (int i = hash; i < m->size; i++) {
|
||||
if (m->entries[i] != NULL) {
|
||||
HashMapEntry *e = m->entries[i];
|
||||
if (e->key == key) {
|
||||
return e;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < hash; i++) {
|
||||
if (m->entries[i] != NULL) {
|
||||
HashMapEntry *e = m->entries[i];
|
||||
if (e->key == key) {
|
||||
return e;
|
||||
}
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Note: The returned array must be malloced, assume caller calls free().
|
||||
*/
|
||||
int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
|
||||
HashMap *m = makeHashMap(numsSize / 0.7);
|
||||
for (int i = 0; i < numsSize; i++) {
|
||||
int complement = target - nums[i];
|
||||
HashMapEntry *e = get(m, complement);
|
||||
if (e != NULL && e->value != i) {
|
||||
int *result = malloc(2 * sizeof(int));
|
||||
result[0] = i;
|
||||
result[1] = e->value;
|
||||
*returnSize = 2;
|
||||
return result;
|
||||
} else if (!put(m, nums[i], i)) {
|
||||
*returnSize = 0;
|
||||
return malloc(0); // fail
|
||||
}
|
||||
}
|
||||
*returnSize = 0;
|
||||
return malloc(0);
|
||||
}
|
Loading…
Reference in New Issue
Block a user