Skip to content
49 changes: 49 additions & 0 deletions solution/0000-0099/0001.Two Sum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,55 @@ class Solution {
}
```

#### C

```c
#include <stdlib.h>

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;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
49 changes: 49 additions & 0 deletions solution/0000-0099/0001.Two Sum/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,55 @@ class Solution {
}
```

#### C

```c
#include <stdlib.h>

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;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
44 changes: 44 additions & 0 deletions solution/0000-0099/0001.Two Sum/Solution.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <stdlib.h>

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;
}
51 changes: 51 additions & 0 deletions solution/0000-0099/0002.Add Two Numbers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

```


<!-- tabs:end -->

<!-- solution:end -->
Expand Down
49 changes: 49 additions & 0 deletions solution/0000-0099/0002.Add Two Numbers/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
40 changes: 40 additions & 0 deletions solution/0000-0099/0002.Add Two Numbers/Solution.c
Original file line number Diff line number Diff line change
@@ -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;
}