aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--leetcode.c20
-rw-r--r--leetcode.h12
-rw-r--r--readme.md24
3 files changed, 51 insertions, 5 deletions
diff --git a/leetcode.c b/leetcode.c
index 9ac1b03..0a5925d 100644
--- a/leetcode.c
+++ b/leetcode.c
@@ -1,5 +1,25 @@
#include "leetcode.h"
+void two_d_arr_init(struct two_d_arr *obj, int row_size, int col_size,
+ int arr[row_size][col_size]) {
+ obj->row_size = row_size;
+ obj->col_size = (int *)malloc(row_size * sizeof(int));
+ obj->arr = (int **)malloc(row_size * sizeof(int *));
+ for (int i = 0; i < row_size; i++) {
+ obj->col_size[i] = col_size;
+ obj->arr[i] = (int *)malloc(col_size * sizeof(int));
+ for (int j = 0; j < col_size; j++)
+ obj->arr[i][j] = arr[i][j];
+ }
+}
+
+void two_d_arr_free(struct two_d_arr *obj) {
+ for (int i = 0; i < obj->row_size; i++)
+ free(obj->arr[i]);
+ free(obj->arr);
+ free(obj->col_size);
+}
+
struct ListNode *listnode_create(int val) {
struct ListNode *new = (struct ListNode *)malloc(sizeof(struct ListNode));
new->val = val;
diff --git a/leetcode.h b/leetcode.h
index 0c0794b..cc01748 100644
--- a/leetcode.h
+++ b/leetcode.h
@@ -9,6 +9,8 @@
#include <stdlib.h>
#include <string.h>
+#pragma once
+
#ifdef __cplusplus
#include <bits/stdc++.h>
using namespace std;
@@ -46,6 +48,12 @@ public:
};
#else //__cplusplus
+struct two_d_arr {
+ int row_size;
+ int *col_size;
+ int **arr;
+};
+
struct ListNode {
int val;
struct ListNode *next;
@@ -63,6 +71,10 @@ struct Node {
struct Node **children;
};
+void two_d_arr_init(struct two_d_arr *obj, int row_size, int col_size,
+ int arr[row_size][col_size]);
+void two_d_arr_free(struct two_d_arr *obj);
+
struct ListNode *listnode_create(int val);
void listnode_print(struct ListNode *head);
void listnode_free(struct ListNode *head);
diff --git a/readme.md b/readme.md
index de35a23..5613633 100644
--- a/readme.md
+++ b/readme.md
@@ -1,12 +1,12 @@
## 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 solved in c++ and rust.
+This repository contains my Leetcode solutions for [user 0x6A73](https://leetcode.com/u/0x6A73).
+the daily challenge is solved in both C and Python3, but there are also
+some problems solved 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.
+calculating the size of a C array using `builtin` functions.
```c
#define IS_ARRAY(value) \
(!__builtin_types_compatible_p(typeof((value)), typeof(&(value)[0])))
@@ -20,7 +20,7 @@ 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.
+returned by such functions. For example, see the following `main` functions.
**ListNode data type**
@@ -56,3 +56,17 @@ int main() {
treenode_print(res);
}
```
+
+Also functions to aid in working with 2D C arrays:
+```c
+int func(int **edges, int edgesSize, int *edgesColSize);
+
+int main() {
+ // edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]]
+ int edges_input[6][2] = {{0, 1}, {0, 2}, {1, 3}, {1, 4}, {2, 5}, {2, 6}};
+ struct two_d_arr edges;
+ two_d_arr_init(&edges, 6, 2, edges_input);
+ int res = func(edges.arr, edges.row_size, edges.col_size);
+ two_d_arr_free(&edges);
+}
+```