From 0a676772d8af548d73f7fd4910f03c3d4c481155 Mon Sep 17 00:00:00 2001 From: pranjal030404 Date: Thu, 22 May 2025 17:04:28 +0530 Subject: [PATCH 1/9] 0001 Solution in c language --- solution/0000-0099/0001.Two Sum/README_EN.md | 59 ++++++++++++++++++++ solution/0000-0099/0001.Two Sum/Solution.c | 54 ++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 solution/0000-0099/0001.Two Sum/Solution.c diff --git a/solution/0000-0099/0001.Two Sum/README_EN.md b/solution/0000-0099/0001.Two Sum/README_EN.md index 25b360b51dbb7..ed6b4bae82e66 100644 --- a/solution/0000-0099/0001.Two Sum/README_EN.md +++ b/solution/0000-0099/0001.Two Sum/README_EN.md @@ -350,6 +350,65 @@ class Solution { } ``` +#### C + +```c +#include +#include +#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; +} +``` + diff --git a/solution/0000-0099/0001.Two Sum/Solution.c b/solution/0000-0099/0001.Two Sum/Solution.c new file mode 100644 index 0000000000000..a82341d7393dc --- /dev/null +++ b/solution/0000-0099/0001.Two Sum/Solution.c @@ -0,0 +1,54 @@ +#include +#include +#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; +} From 31851f377dffbf402ec18f0af5c234cfa74003c2 Mon Sep 17 00:00:00 2001 From: pranjal030404 <182807087+pranjal030404@users.noreply.github.com> Date: Thu, 22 May 2025 11:39:45 +0000 Subject: [PATCH 2/9] style: format code and docs with prettier --- solution/0000-0099/0001.Two Sum/README_EN.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solution/0000-0099/0001.Two Sum/README_EN.md b/solution/0000-0099/0001.Two Sum/README_EN.md index ed6b4bae82e66..84934190f58dc 100644 --- a/solution/0000-0099/0001.Two Sum/README_EN.md +++ b/solution/0000-0099/0001.Two Sum/README_EN.md @@ -382,7 +382,7 @@ int contains(Entry *table, int key, int *out_value) { return 1; } idx = (idx + 1) % TABLE_SIZE; - if (idx == start) break; + if (idx == start) break; } return 0; } From ef84796350efc193a1035e61b38d0eaf81e018b1 Mon Sep 17 00:00:00 2001 From: pranjal030404 Date: Thu, 22 May 2025 18:29:30 +0530 Subject: [PATCH 3/9] changed the c language code and now it will pass all the testcases of it --- solution/0000-0099/0001.Two Sum/README_EN.md | 57 ++++---------------- solution/0000-0099/0001.Two Sum/Solution.c | 57 ++++---------------- 2 files changed, 18 insertions(+), 96 deletions(-) diff --git a/solution/0000-0099/0001.Two Sum/README_EN.md b/solution/0000-0099/0001.Two Sum/README_EN.md index 84934190f58dc..0ef294065ecf9 100644 --- a/solution/0000-0099/0001.Two Sum/README_EN.md +++ b/solution/0000-0099/0001.Two Sum/README_EN.md @@ -353,58 +353,19 @@ class Solution { #### C ```c -#include -#include -#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; + for (int i = 0; i < numsSize; i++) { + for (int j = i + 1; j < numsSize; j++) { + if (nums[i] + nums[j] == target) { + int* result = (int*)malloc(2 * sizeof(int)); + result[0] = i; + result[1] = j; + *returnSize = 2; + return result; + } } - insert(map, x, i); } *returnSize = 0; - free(map); - free(result); return NULL; } ``` diff --git a/solution/0000-0099/0001.Two Sum/Solution.c b/solution/0000-0099/0001.Two Sum/Solution.c index a82341d7393dc..050939b5d25ac 100644 --- a/solution/0000-0099/0001.Two Sum/Solution.c +++ b/solution/0000-0099/0001.Two Sum/Solution.c @@ -1,54 +1,15 @@ -#include -#include -#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; + for (int i = 0; i < numsSize; i++) { + for (int j = i + 1; j < numsSize; j++) { + if (nums[i] + nums[j] == target) { + int* result = (int*) malloc(2 * sizeof(int)); + result[0] = i; + result[1] = j; + *returnSize = 2; + return result; + } } - insert(map, x, i); } *returnSize = 0; - free(map); - free(result); return NULL; } From 846747a93bcce250ad0cc8bd095e5c9472a11b97 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 23 May 2025 06:18:08 +0800 Subject: [PATCH 4/9] Update README_EN.md --- solution/0000-0099/0001.Two Sum/README_EN.md | 43 ++++++++++++++++---- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/solution/0000-0099/0001.Two Sum/README_EN.md b/solution/0000-0099/0001.Two Sum/README_EN.md index 0ef294065ecf9..5dc79c7a5dbfd 100644 --- a/solution/0000-0099/0001.Two Sum/README_EN.md +++ b/solution/0000-0099/0001.Two Sum/README_EN.md @@ -353,19 +353,48 @@ class Solution { #### C ```c +=#include + int* twoSum(int* nums, int numsSize, int target, int* returnSize) { - for (int i = 0; i < numsSize; i++) { - for (int j = i + 1; j < numsSize; j++) { - if (nums[i] + nums[j] == target) { - int* result = (int*)malloc(2 * sizeof(int)); - result[0] = i; - result[1] = j; + int capacity = 1; + while (capacity < numsSize * 2) capacity <<= 1; + int* keys = malloc(capacity * sizeof(int)); + int* vals = malloc(capacity * sizeof(int)); + char* used = calloc(capacity, sizeof(char)); + if (!keys || !vals || !used) { + free(keys); + free(vals); + free(used); + *returnSize = 0; + return NULL; + } + for (int i = 0; i < numsSize; ++i) { + int x = nums[i]; + int y = target - x; + unsigned int h = (unsigned int) y & (capacity - 1); + while (used[h]) { + if (keys[h] == y) { + int* res = malloc(2 * sizeof(int)); + res[0] = vals[h]; + res[1] = i; *returnSize = 2; - return result; + free(keys); + free(vals); + free(used); + return res; } + h = (h + 1) & (capacity - 1); } + unsigned int h2 = (unsigned int) x & (capacity - 1); + while (used[h2]) h2 = (h2 + 1) & (capacity - 1); + used[h2] = 1; + keys[h2] = x; + vals[h2] = i; } *returnSize = 0; + free(keys); + free(vals); + free(used); return NULL; } ``` From 78f06e224a44a9c7771506bafb39c06d1b3a05b4 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 23 May 2025 06:18:49 +0800 Subject: [PATCH 5/9] Update README.md --- solution/0000-0099/0001.Two Sum/README.md | 49 +++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/solution/0000-0099/0001.Two Sum/README.md b/solution/0000-0099/0001.Two Sum/README.md index f9a38ce75e350..88c20df42f7fa 100644 --- a/solution/0000-0099/0001.Two Sum/README.md +++ b/solution/0000-0099/0001.Two Sum/README.md @@ -353,6 +353,55 @@ class Solution { } ``` +#### C + +```c +#include + +int* twoSum(int* nums, int numsSize, int target, int* returnSize) { + int capacity = 1; + while (capacity < numsSize * 2) capacity <<= 1; + int* keys = malloc(capacity * sizeof(int)); + int* vals = malloc(capacity * sizeof(int)); + char* used = calloc(capacity, sizeof(char)); + if (!keys || !vals || !used) { + free(keys); + free(vals); + free(used); + *returnSize = 0; + return NULL; + } + for (int i = 0; i < numsSize; ++i) { + int x = nums[i]; + int y = target - x; + unsigned int h = (unsigned int) y & (capacity - 1); + while (used[h]) { + if (keys[h] == y) { + int* res = malloc(2 * sizeof(int)); + res[0] = vals[h]; + res[1] = i; + *returnSize = 2; + free(keys); + free(vals); + free(used); + return res; + } + h = (h + 1) & (capacity - 1); + } + unsigned int h2 = (unsigned int) x & (capacity - 1); + while (used[h2]) h2 = (h2 + 1) & (capacity - 1); + used[h2] = 1; + keys[h2] = x; + vals[h2] = i; + } + *returnSize = 0; + free(keys); + free(vals); + free(used); + return NULL; +} +``` + From e6642a98ef12a57e9d9da7939cb715686977b084 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 23 May 2025 06:19:02 +0800 Subject: [PATCH 6/9] Update Solution.c --- solution/0000-0099/0001.Two Sum/Solution.c | 43 ++++++++++++++++++---- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/solution/0000-0099/0001.Two Sum/Solution.c b/solution/0000-0099/0001.Two Sum/Solution.c index 050939b5d25ac..5ed7119c426ee 100644 --- a/solution/0000-0099/0001.Two Sum/Solution.c +++ b/solution/0000-0099/0001.Two Sum/Solution.c @@ -1,15 +1,44 @@ +#include + int* twoSum(int* nums, int numsSize, int target, int* returnSize) { - for (int i = 0; i < numsSize; i++) { - for (int j = i + 1; j < numsSize; j++) { - if (nums[i] + nums[j] == target) { - int* result = (int*) malloc(2 * sizeof(int)); - result[0] = i; - result[1] = j; + int capacity = 1; + while (capacity < numsSize * 2) capacity <<= 1; + int* keys = malloc(capacity * sizeof(int)); + int* vals = malloc(capacity * sizeof(int)); + char* used = calloc(capacity, sizeof(char)); + if (!keys || !vals || !used) { + free(keys); + free(vals); + free(used); + *returnSize = 0; + return NULL; + } + for (int i = 0; i < numsSize; ++i) { + int x = nums[i]; + int y = target - x; + unsigned int h = (unsigned int) y & (capacity - 1); + while (used[h]) { + if (keys[h] == y) { + int* res = malloc(2 * sizeof(int)); + res[0] = vals[h]; + res[1] = i; *returnSize = 2; - return result; + free(keys); + free(vals); + free(used); + return res; } + h = (h + 1) & (capacity - 1); } + unsigned int h2 = (unsigned int) x & (capacity - 1); + while (used[h2]) h2 = (h2 + 1) & (capacity - 1); + used[h2] = 1; + keys[h2] = x; + vals[h2] = i; } *returnSize = 0; + free(keys); + free(vals); + free(used); return NULL; } From 276efed3dce0ae9e33e72979a2ea3f9aa0bed985 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 23 May 2025 06:19:32 +0800 Subject: [PATCH 7/9] Update README_EN.md --- solution/0000-0099/0001.Two Sum/README_EN.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solution/0000-0099/0001.Two Sum/README_EN.md b/solution/0000-0099/0001.Two Sum/README_EN.md index 5dc79c7a5dbfd..c536e027be740 100644 --- a/solution/0000-0099/0001.Two Sum/README_EN.md +++ b/solution/0000-0099/0001.Two Sum/README_EN.md @@ -353,7 +353,7 @@ class Solution { #### C ```c -=#include +#include int* twoSum(int* nums, int numsSize, int target, int* returnSize) { int capacity = 1; From 3ec82172baba4bb58d5f5d173900a033b11521e0 Mon Sep 17 00:00:00 2001 From: pranjal030404 Date: Fri, 23 May 2025 12:42:30 +0530 Subject: [PATCH 8/9] git commit " solution of 0002 0000-0100 0002 Solution of this in c a=language" --- .../0000-0099/0002.Add Two Numbers/README.md | 51 +++++++++++++++++++ .../0002.Add Two Numbers/README_EN.md | 49 ++++++++++++++++++ .../0000-0099/0002.Add Two Numbers/Solution.c | 40 +++++++++++++++ 3 files changed, 140 insertions(+) create mode 100644 solution/0000-0099/0002.Add Two Numbers/Solution.c diff --git a/solution/0000-0099/0002.Add Two Numbers/README.md b/solution/0000-0099/0002.Add Two Numbers/README.md index 3806f64b53d17..7274d162276cf 100644 --- a/solution/0000-0099/0002.Add Two Numbers/README.md +++ b/solution/0000-0099/0002.Add Two Numbers/README.md @@ -494,6 +494,57 @@ proc addTwoNumbers(l1: var SinglyLinkedList, l2: var SinglyLinkedList): SinglyLi result = aggregate ``` +#### C + +```c +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * struct ListNode *next; + * }; + */ + +struct ListNode *addTwoNumbers(struct ListNode *l1, struct ListNode *l2) +{ + struct ListNode *dummy = (struct ListNode *)malloc(sizeof(struct ListNode)); + dummy->val = 0; + dummy->next = NULL; + struct ListNode *curr = dummy; + int carry = 0; + + while (l1 != NULL || l2 != NULL || carry != 0) + { + int sum = carry; + if (l1 != NULL) + { + sum += l1->val; + l1 = l1->next; + } + if (l2 != NULL) + { + sum += l2->val; + l2 = l2->next; + } + + carry = sum / 10; + int val = sum % 10; + + struct ListNode *newNode = (struct ListNode *)malloc(sizeof(struct ListNode)); + newNode->val = val; + newNode->next = NULL; + curr->next = newNode; + curr = curr->next; + } + + struct ListNode *result = dummy->next; + free(dummy); + return result; +} + +``` + + diff --git a/solution/0000-0099/0002.Add Two Numbers/README_EN.md b/solution/0000-0099/0002.Add Two Numbers/README_EN.md index f6878f0937322..29d03ab30f06b 100644 --- a/solution/0000-0099/0002.Add Two Numbers/README_EN.md +++ b/solution/0000-0099/0002.Add Two Numbers/README_EN.md @@ -490,6 +490,55 @@ proc addTwoNumbers(l1: var SinglyLinkedList, l2: var SinglyLinkedList): SinglyLi result = aggregate ``` +#### C + +```c +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * struct ListNode *next; + * }; + */ + +struct ListNode *addTwoNumbers(struct ListNode *l1, struct ListNode *l2) +{ + struct ListNode *dummy = (struct ListNode *)malloc(sizeof(struct ListNode)); + dummy->val = 0; + dummy->next = NULL; + struct ListNode *curr = dummy; + int carry = 0; + + while (l1 != NULL || l2 != NULL || carry != 0) + { + int sum = carry; + if (l1 != NULL) + { + sum += l1->val; + l1 = l1->next; + } + if (l2 != NULL) + { + sum += l2->val; + l2 = l2->next; + } + + carry = sum / 10; + int val = sum % 10; + + struct ListNode *newNode = (struct ListNode *)malloc(sizeof(struct ListNode)); + newNode->val = val; + newNode->next = NULL; + curr->next = newNode; + curr = curr->next; + } + + struct ListNode *result = dummy->next; + free(dummy); + return result; +} +``` + diff --git a/solution/0000-0099/0002.Add Two Numbers/Solution.c b/solution/0000-0099/0002.Add Two Numbers/Solution.c new file mode 100644 index 0000000000000..08fd89cf9ded0 --- /dev/null +++ b/solution/0000-0099/0002.Add Two Numbers/Solution.c @@ -0,0 +1,40 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * struct ListNode *next; + * }; + */ + +struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) { + struct ListNode* dummy = (struct ListNode*) malloc(sizeof(struct ListNode)); + dummy->val = 0; + dummy->next = NULL; + struct ListNode* curr = dummy; + int carry = 0; + + while (l1 != NULL || l2 != NULL || carry != 0) { + int sum = carry; + if (l1 != NULL) { + sum += l1->val; + l1 = l1->next; + } + if (l2 != NULL) { + sum += l2->val; + l2 = l2->next; + } + + carry = sum / 10; + int val = sum % 10; + + struct ListNode* newNode = (struct ListNode*) malloc(sizeof(struct ListNode)); + newNode->val = val; + newNode->next = NULL; + curr->next = newNode; + curr = curr->next; + } + + struct ListNode* result = dummy->next; + free(dummy); + return result; +} \ No newline at end of file From a54923dcbb6a445b371102f8f8bb9904bd149a57 Mon Sep 17 00:00:00 2001 From: pranjal030404 <182807087+pranjal030404@users.noreply.github.com> Date: Fri, 23 May 2025 07:39:27 +0000 Subject: [PATCH 9/9] style: format code and docs with prettier --- solution/0000-0099/0002.Add Two Numbers/README.md | 3 +-- solution/0000-0099/0002.Add Two Numbers/README_EN.md | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/solution/0000-0099/0002.Add Two Numbers/README.md b/solution/0000-0099/0002.Add Two Numbers/README.md index 7274d162276cf..1fd639e2750ca 100644 --- a/solution/0000-0099/0002.Add Two Numbers/README.md +++ b/solution/0000-0099/0002.Add Two Numbers/README.md @@ -538,13 +538,12 @@ struct ListNode *addTwoNumbers(struct ListNode *l1, struct ListNode *l2) } struct ListNode *result = dummy->next; - free(dummy); + free(dummy); return result; } ``` - diff --git a/solution/0000-0099/0002.Add Two Numbers/README_EN.md b/solution/0000-0099/0002.Add Two Numbers/README_EN.md index 29d03ab30f06b..e9fc9a9bbbc8a 100644 --- a/solution/0000-0099/0002.Add Two Numbers/README_EN.md +++ b/solution/0000-0099/0002.Add Two Numbers/README_EN.md @@ -534,7 +534,7 @@ struct ListNode *addTwoNumbers(struct ListNode *l1, struct ListNode *l2) } struct ListNode *result = dummy->next; - free(dummy); + free(dummy); return result; } ```