commit 9d3ba816dd3b48f66640122136c78215379a05cb
parent e0dce9c4e0f003323af3b76658a950ce415f0632
Author: Jack Sangdahl <jack@pngu.org>
Date: Thu, 11 Jul 2024 18:11:00 -0600
add readme and more robust helpers
Diffstat:
| A | leetcode.c | | | 33 | +++++++++++++++++++++++++++++++++ |
| M | leetcode.h | | | 31 | ++++++++++++++++++++++++------- |
| A | readme.md | | | 56 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
3 files changed, 113 insertions(+), 7 deletions(-)
diff --git a/leetcode.c b/leetcode.c
@@ -0,0 +1,33 @@
+#include "leetcode.h"
+
+struct ListNode *listnode_create(int val) {
+ struct ListNode *new = (struct ListNode *)malloc(sizeof(struct ListNode));
+ new->val = val;
+ new->next = NULL;
+ return new;
+}
+
+void listnode_print(struct ListNode *head) {
+ struct ListNode *curr = head;
+ while (curr != NULL) {
+ printf("%d ", curr->val);
+ curr = curr->next;
+ }
+ printf("\n");
+}
+
+struct TreeNode *treenode_create(int val) {
+ struct TreeNode *new = (struct TreeNode *)malloc(sizeof(struct TreeNode));
+ new->val = val;
+ new->left = NULL;
+ new->right = NULL;
+ return new;
+}
+
+void treenode_print(struct TreeNode *root) {
+ if (!root)
+ return;
+ treenode_print(root->left);
+ printf("%d ", root->val);
+ treenode_print(root->right);
+}
diff --git a/leetcode.h b/leetcode.h
@@ -1,12 +1,12 @@
+#include "lib/uthash/src/uthash.h"
+#include <ctype.h>
+#include <limits.h>
#include <math.h>
+#include <stdbool.h>
+#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <stdbool.h>
-#include <limits.h>
-#include <ctype.h>
-#include <stdint.h>
-#include "lib/uthash/src/uthash.h"
#ifdef __cplusplus
#include <bits/stdc++.h>
@@ -43,7 +43,24 @@ public:
children = _children;
}
};
-#endif //__cplusplus
+#else //__cplusplus
+
+struct ListNode {
+ int val;
+ struct ListNode *next;
+};
+
+struct TreeNode {
+ int val;
+ struct TreeNode *left;
+ struct TreeNode *right;
+};
+
+struct ListNode *listnode_create(int val);
+void listnode_print(struct ListNode *head);
+
+struct TreeNode *treenode_create(int val);
+void treenode_print(struct TreeNode *root);
#define IS_ARRAY(value) \
(!__builtin_types_compatible_p(typeof((value)), typeof(&(value)[0])))
@@ -51,4 +68,4 @@ public:
#define ARRAY_SIZE(array) \
(__builtin_choose_expr(IS_ARRAY((array)), \
sizeof((array)) / sizeof((array)[0]), (void)0))
-
+#endif
diff --git a/readme.md b/readme.md
@@ -0,0 +1,56 @@
+## leetcode
+this repository contains my leetcode solutions for [user 0x6A73](https://leetcode.com/u/0x6A73).
+the daily challenge is solved in both c and python, but there are also
+some problems soved in c++ and rust.
+
+### notes
+[`leetcode.h`](leetcode.h) contains various includes, standard leetcode
+structs (`ListNode`, `TreeNode`), and some helpful macros, like
+calculating the size of a c array using `builtin` functions.
+```c
+#define IS_ARRAY(value) \
+ (!__builtin_types_compatible_p(typeof((value)), typeof(&(value)[0])))
+
+#define ARRAY_SIZE(array) \
+ (__builtin_choose_expr(IS_ARRAY((array)), \
+ sizeof((array)) / sizeof((array)[0]), (void)0))
+// ...
+int res = func(arr, ARRAY_SIZE(arr));
+```
+
+[`leetcode.c`](leetcode.c) has helpful functions to fill `ListNode` and
+`TreeNode` data structures with provided data, and also print data
+returned by such functions. for example, see the following `main` functions.
+
+**ListNode data type**
+
+```c
+int main() {
+ // head = [0,3,1,0,4,5,2,0]
+ struct ListNode *head = listnode_create(0);
+ head->next = listnode_create(3);
+ head->next->next = listnode_create(1);
+ head->next->next->next = listnode_create(0);
+ head->next->next->next->next = listnode_create(4);
+ head->next->next->next->next->next = listnode_create(5);
+ head->next->next->next->next->next->next = listnode_create(2);
+ head->next->next->next->next->next->next->next = listnode_create(0);
+ struct ListNode *res = func(head);
+ listnode_print(res);
+}
+```
+
+**TreeNode data type**
+
+```c
+int main() {
+ // root = [3,9,20,null,null,15,7]
+ struct TreeNode *root = treenode_create(3);
+ root->left = treenode_create(9);
+ root->right = treenode_create(20);
+ root->right->left = treenode_create(15);
+ root->right->right = treenode_create(7);
+ struct TreeNode *res = func(root);
+ treenode_print(res);
+}
+```