leetcode

Leetcode Solutions (0x6A73)
git clone https://git.pngu.org/leetcode
Log | Files | Refs | Submodules | README

leetcode.c (3527B)


#include "leetcode.h"

struct two_d_arr *two_d_arr_init(int row_size, int col_size,
                                 int arr[][col_size]) {
  struct two_d_arr *obj = (struct two_d_arr *)malloc(sizeof(struct two_d_arr));
  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));
    memcpy(obj->arr[i], arr[i], col_size * sizeof(int));
  }
  return obj;
}

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;
  new->next = NULL;
  return new;
}

struct ListNode *listnode_build(int *vals, int size) {
  if (!size)
    return NULL;
  struct ListNode *head = listnode_create(vals[0]), *curr = head;
  for (int i = 1; i < size; i++) {
    curr->next = listnode_create(vals[i]);
    curr = curr->next;
  }
  return head;
}

void listnode_print(struct ListNode *head, int *expected) {
  struct ListNode *curr = head;
  int n = 0;
  while (curr != NULL) {
    printf("%d ", curr->val);
    assert(curr->val == expected[n]);
    curr = curr->next;
    n++;
  }
  printf("\n");
}

void listnode_free(struct ListNode *head) {
  struct ListNode *tmp;
  while (head) {
    tmp = head;
    head = head->next;
    free(tmp);
  }
}

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

struct TreeNode *treenode_build(int *vals, int size) {
  if (!size || vals[0] == -1)
    return NULL;
  struct TreeNode *root = treenode_create(vals[0]);
  struct TreeNode **queue =
      (struct TreeNode **)malloc(size * sizeof(struct TreeNode *));
  queue[0] = root;
  int front = 0, rear = 1;
  for (int i = 1; i < size; i++) {
    struct TreeNode *curr = queue[front];
    if (vals[i] != -1) {
      curr->left = treenode_create(vals[i]);
      queue[rear++] = curr->left;
    }
    if (i + 1 < size && vals[i + 1] != -1) {
      curr->right = treenode_create(vals[i + 1]);
      queue[rear++] = curr->right;
    }
    i++;
    front++;
  }
  free(queue);
  return root;
}

void treenode_print(struct TreeNode *root) {
  if (!root)
    return;
  treenode_print(root->left);
  printf("%d ", root->val);
  treenode_print(root->right);
}

void treenode_free(struct TreeNode *root) {
  if (!root)
    return;
  treenode_free(root->left);
  treenode_free(root->right);
  free(root);
}

struct Node *node_create(int val, int numChildren) {
  struct Node *new = (struct Node *)malloc(sizeof(struct Node));
  new->val = val;
  new->numChildren = numChildren;
  if (numChildren) {
    new->children = (struct Node **)malloc(numChildren * sizeof(struct Node *));
    for (int i = 0; i < numChildren; i++)
      new->children[i] = NULL;
  } else
    new->children = NULL;
  return new;
}

void node_print(struct Node *root, int depth) {
  if (!root)
    return;
  printf("%d\n", root->val);
  for (int i = 0; i < root->numChildren; i++)
    node_print(root->children[i], depth + 1);
}

void node_free(struct Node *root) {
  if (!root)
    return;
  for (int i = 0; i < root->numChildren; i++)
    node_free(root->children[i]);
  free(root->children);
  free(root);
}