diff options
author | Jack Sangdahl <[email protected]> | 2024-10-19 13:08:08 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2024-10-19 13:08:08 -0600 |
commit | 725a6a2c9ab947c32543bccc15aca2ca966c857b (patch) | |
tree | 9b082336500d640f08962891ffb12b86e0d5aa82 | |
parent | 2d28c29526ea98941861059604ae326247b6a827 (diff) | |
download | leetcode-725a6a2c9ab947c32543bccc15aca2ca966c857b.tar.gz leetcode-725a6a2c9ab947c32543bccc15aca2ca966c857b.zip |
add 2d array helper functions
-rw-r--r-- | leetcode.c | 20 | ||||
-rw-r--r-- | leetcode.h | 12 | ||||
-rw-r--r-- | readme.md | 24 |
3 files changed, 51 insertions, 5 deletions
@@ -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; @@ -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); @@ -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); +} +``` |