diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b30a728 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.o +bin diff --git a/problems/1-two-sum/Makefile b/problems/1-two-sum/Makefile new file mode 100644 index 0000000..ab746c4 --- /dev/null +++ b/problems/1-two-sum/Makefile @@ -0,0 +1,14 @@ +all: lib.o main.o + gcc -Wall -o bin lib.o main.o + +lib.o: lib.c + gcc -c -Wall lib.c + +main.o: main.c + gcc -c -Wall main.c + +run: + ./bin + +clean: + rm *.o bin diff --git a/problems/1-two-sum/lib.c b/problems/1-two-sum/lib.c new file mode 100644 index 0000000..465ecb5 --- /dev/null +++ b/problems/1-two-sum/lib.c @@ -0,0 +1,18 @@ +#include +#include "lib.h" + +int* twoSum(int* nums, int numsSize, int target, int* returnSize) { + for (int i=0; i 0) { + free(res1); + } + + int nums2[] = {3, 2, 4}; + int target2 = 6; + int* res2 = twoSum(nums2, 3, target2, &returnSize); + if (returnSize == 2 && res2[0] == 1 && res2[1] == 2) { + printf("Test 2: passed\n"); + } + else { + printf("Test 2: failed\n"); + } + if (returnSize > 0) { + free(res2); + } + + return 0; +} diff --git a/problems/1-two-sum/readme.md b/problems/1-two-sum/readme.md new file mode 100644 index 0000000..6042b65 --- /dev/null +++ b/problems/1-two-sum/readme.md @@ -0,0 +1,9 @@ +# 1. Two sum + +Given an array of integers nums and an integer target, +return indices of the two numbers such that they add up to target. + +You may assume that each input would have exactly one solution, +and you may not use the same element twice. + +You can return the answer in any order.