first commit

This commit is contained in:
Jeffrey Hsu 2024-09-08 14:39:20 +08:00
commit a1127e008f
34 changed files with 1403 additions and 0 deletions

1
.clang-format Normal file
View File

@ -0,0 +1 @@
BasedOnStyle: Microsoft

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
.cache/
build/
.vscode/
.idea/

99
100.c Normal file
View File

@ -0,0 +1,99 @@
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};
struct list_t {
int *data;
size_t cur;
size_t size;
};
struct list_t *init_list() {
struct list_t *list = (struct list_t *)malloc(sizeof(struct list_t));
list->data = (int *)malloc(sizeof(int) * 10);
list->cur = 0;
list->size = 10;
return list;
}
void append(struct list_t *list, int v) {
if (list->cur == list->size) {
list->data = realloc(list->data, sizeof(int) * (list->size + 10));
list->size += 10;
}
list->data[list->cur] = v;
list->cur++;
}
void delete(struct list_t *list) {
if (!list)
return;
free(list->data);
free(list);
list = NULL;
}
bool cmp(struct list_t *a, struct list_t *b) {
if (a->cur != b->cur)
return false;
for (int i = 0; i < a->size; i++)
if (a->data[i] != b->data[i])
return false;
return true;
}
void post(struct TreeNode *node, struct list_t *list) {
if (!node) {
append(list, INT_MIN);
return;
}
append(list, node->val);
post(node->left, list);
post(node->right, list);
}
bool isSameTree(struct TreeNode *p, struct TreeNode *q) {
struct list_t *pl = init_list();
struct list_t *ql = init_list();
post(p, pl);
post(q, ql);
bool ret = cmp(pl, ql);
delete (pl);
delete (ql);
return ret;
}
int main() {
struct TreeNode t1 = {1, 0, 0};
struct TreeNode t2 = {2, 0, 0};
struct TreeNode t3 = {3, 0, 0};
struct TreeNode t4 = {1, 0, 0};
struct TreeNode t5 = {2, 0, 0};
struct TreeNode t6 = {3, 0, 0};
t1.left = &t2;
t2.left = &t3;
t4.right = &t5;
t5.right = &t6;
assert(isSameTree(&t1, &t2) == false);
assert(isSameTree(&t1, &t4) == false);
assert(isSameTree(&t1, &t1) == true);
return 0;
}

123
101.c Normal file
View File

@ -0,0 +1,123 @@
#define S2
struct TreeNode
{
int val;
struct TreeNode *left;
struct TreeNode *right;
};
#ifdef S1
#include <limits.h>
#include <stdbool.h>
#include <stdlib.h>
struct list_t
{
int *data;
size_t idx;
size_t capacity;
void (*append)(struct list_t *, int);
void (*delete)(struct list_t *);
};
void append(struct list_t *self, int v)
{
if (self->idx >= self->capacity)
{
self->data = (int *)realloc(self->data, sizeof(int) * (self->capacity + 5));
self->capacity += 5;
}
self->data[self->idx++] = v;
}
void delete(struct list_t *self)
{
free(self->data);
self->idx = 0;
self->capacity = 0;
}
void init_list(struct list_t *list)
{
if (!list)
return;
list->data = (int *)malloc(sizeof(int) * 5);
list->capacity = 5;
list->idx = 0;
list->append = &append;
list->delete = &delete;
}
void inorder(struct TreeNode *n, struct list_t *l)
{
if (!n)
{
l->append(l, INT_MIN);
return;
}
if (n->left || n->right)
{
inorder(n->left, l);
l->append(l, n->val);
inorder(n->right, l);
}
else
l->append(l, n->val);
}
bool cmp(struct list_t l)
{
int s1 = 0, s2 = l.idx - 1;
for (; s1 < s2; s1++, s2--)
{
if (l.data[s1] != l.data[s2])
return false;
}
return true;
}
bool isSymmetric(struct TreeNode *root)
{
struct list_t l;
bool ret;
init_list(&l);
inorder(root, &l);
ret = cmp(l);
l.delete(&l);
return ret;
}
#endif
#ifdef S2
#include <stdbool.h>
bool recur(struct TreeNode *l, struct TreeNode *r)
{
if (!l && !r)
return true;
if (!l || !r || l->val != r->val)
return false;
return recur(l->left, r->right) && recur(l->right, r->left);
}
bool isSymmetric(struct TreeNode *root)
{
return !root || recur(root->left, root->right);
}
#endif
int main()
{
return 0;
}

19
104.c Normal file
View File

@ -0,0 +1,19 @@
#include "include/TreeNode.h"
int max(const int a, const int b)
{
return a > b ? a : b;
}
int maxDepth(struct TreeNode *root)
{
if (!root)
return 0;
return max(maxDepth(root->left), maxDepth(root->right)) + 1;
}
int main()
{
return 0;
}

20
108.c Normal file
View File

@ -0,0 +1,20 @@
#include "include/TreeNode.h"
#include <stdlib.h>
struct TreeNode *sortedArrayToBST(int *nums, int numsSize)
{
struct TreeNode *node = NULL;
if (numsSize <= 0)
return node;
node = (struct TreeNode *)malloc(sizeof(struct TreeNode));
node->val = nums[numsSize / 2];
node->left = sortedArrayToBST(nums, numsSize / 2);
node->right = sortedArrayToBST(nums + numsSize / 2 + 1, numsSize - numsSize / 2 - 1);
return node;
}
int main()
{
return 0;
}

28
110.c Normal file
View File

@ -0,0 +1,28 @@
#include "include/TreeNode.h"
#include <math.h>
#include <stdbool.h>
#include <stdlib.h>
int maxHight(struct TreeNode *root)
{
if (!root)
return 0;
return fmax(maxHight(root->left), maxHight(root->right)) + 1;
}
bool isBalanced(struct TreeNode *root)
{
if (!root)
return true;
if (!isBalanced(root->left))
return false;
if (!isBalanced(root->right))
return false;
return abs(maxHight(root->left) - maxHight(root->right)) <= 1;
}
int main()
{
return 0;
}

16
111.c Normal file
View File

@ -0,0 +1,16 @@
#include "include/TreeNode.h"
#include <math.h>
int minDepth(struct TreeNode *root)
{
if (!root)
return 0;
if (!root->left || !root->right)
return minDepth(root->left) + minDepth(root->right) + 1;
return fmin(minDepth(root->left), minDepth(root->right)) + 1;
}
int main()
{
return 0;
}

23
112.c Normal file
View File

@ -0,0 +1,23 @@
#include "include/TreeNode.h"
#include <stdbool.h>
bool in_order(struct TreeNode *root, int target, int sum)
{
if (!root)
return false;
if (!root->left && !root->right)
return sum + root->val == target;
return in_order(root->left, target, sum + root->val) || in_order(root->right, target, sum + root->val);
}
bool hasPathSum(struct TreeNode *root, int targetSum)
{
return in_order(root, targetSum, 0);
}
int main()
{
return 0;
}

146
13.c Normal file
View File

@ -0,0 +1,146 @@
#include <assert.h>
/*
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
*/
#define S2
#ifdef S1
int romanToInt(char *s)
{
int ret = 0;
while (*s != '\0')
{
char next = *(s + 1);
switch (*s)
{
case 'I':
if (next == 'V')
{
ret += 4;
s++;
}
else if (next == 'X')
{
ret += 9;
s++;
}
else
ret += 1;
break;
case 'V':
ret += 5;
break;
case 'X':
if (next == 'L')
{
ret += 40;
s++;
}
else if (next == 'C')
{
ret += 90;
s++;
}
else
ret += 10;
break;
case 'L':
ret += 50;
break;
case 'C':
if (next == 'D')
{
ret += 400;
s++;
}
else if (next == 'M')
{
ret += 900;
s++;
}
else
ret += 100;
break;
case 'D':
ret = 500;
break;
case 'M':
ret += 1000;
break;
default:
break;
}
s++;
}
return ret;
}
#endif
#ifdef S2
int getValue(const char c)
{
switch (c)
{
case 'I':
return 1;
case 'V':
return 5;
case 'X':
return 10;
case 'L':
return 50;
case 'C':
return 100;
case 'D':
return 500;
case 'M':
return 1000;
default:
return 0;
}
}
int romanToInt(char *s)
{
int ret = 0;
int preNum = getValue(*s);
for (int i = 1; s[i] != '\0'; i++)
{
int num = getValue(s[i]);
if (preNum < num)
ret -= preNum;
else
ret += preNum;
preNum = num;
}
ret += preNum;
return ret;
}
#endif
int main()
{
char r1[] = "III";
char r2[] = "IV";
char r3[] = "IX";
char r4[] = "LVIII";
char r5[] = "MCMXCIV";
assert(romanToInt(r1) == 3);
assert(romanToInt(r2) == 4);
assert(romanToInt(r3) == 9);
assert(romanToInt(r4) == 58);
assert(romanToInt(r5) == 1994);
return 0;
}

16
136.c Normal file
View File

@ -0,0 +1,16 @@
#include <stdbool.h>
int singleNumber(int *nums, int numsSize)
{
int single = nums[0];
for (int i = 1; i < numsSize; i++)
single ^= nums[i];
return single;
}
int main()
{
return 0;
}

26
141.c Normal file
View File

@ -0,0 +1,26 @@
#include "include/ListNode.h"
#include <stdbool.h>
bool hasCycle(struct ListNode *head)
{
if (!head || !head->next)
return false;
struct ListNode *slow = head;
struct ListNode *fast = head->next;
while (slow != fast)
{
if (!fast || !fast->next)
return false;
slow = slow->next;
fast = fast->next->next;
}
return true;
}
int main()
{
return 0;
}

43
144.c Normal file
View File

@ -0,0 +1,43 @@
#include "include/TreeNode.h"
#include <stdlib.h>
struct list_t
{
int *data;
size_t size;
};
void append(struct list_t *self, int v)
{
if (!self->data)
self->data = (int *)malloc(sizeof(int));
else
self->data = (int *)realloc(self->data, (self->size + 1) * sizeof(int));
self->data[self->size - 1] = v;
self->size += 1;
}
void preorder(struct TreeNode *root, struct list_t *list)
{
if (!root)
return;
append(list, root->val);
preorder(root->left, list);
preorder(root->right, list);
}
int *preorderTraversal(struct TreeNode *root, int *returnSize)
{
struct list_t l = {NULL, 0};
preorder(root, &l);
*returnSize = l.size;
return l.data;
}
int main()
{
return 0;
}

43
145.c Normal file
View File

@ -0,0 +1,43 @@
#include "include/TreeNode.h"
#include <stdlib.h>
struct list_t
{
int *data;
size_t size;
};
void append(struct list_t *self, int v)
{
if (!self->data)
self->data = (int *)malloc(sizeof(int));
else
self->data = (int *)realloc(self->data, (self->size + 1) * sizeof(int));
self->data[self->size] = v;
self->size += 1;
}
void postorder(struct TreeNode *root, struct list_t *list)
{
if (!root)
return;
postorder(root->left, list);
postorder(root->right, list);
append(list, root->val);
}
int *postorderTraversal(struct TreeNode *root, int *returnSize)
{
struct list_t l = {NULL, 0};
postorder(root, &l);
*returnSize = l.size;
return l.data;
}
int main()
{
return 0;
}

23
160.c Normal file
View File

@ -0,0 +1,23 @@
#include <ListNode.h>
#include <stdio.h>
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB)
{
if (headA == NULL || headB == NULL)
return NULL;
struct ListNode *pA = headA, *pB = headB;
while (pA != pB)
{
pA = pA == NULL ? headB : pA->next;
pB = pB == NULL ? headA : pB->next;
}
return pA;
}
int main()
{
return 0;
}

36
169.c Normal file
View File

@ -0,0 +1,36 @@
#include <assert.h>
int majorityElement(int *nums, int numsSize)
{
int candidate = nums[0];
int vote = 1;
for (int i = 1; i < numsSize; i++)
{
if (nums[i] == candidate)
vote++;
else
{
vote--;
if (vote < 0)
{
candidate = nums[i];
vote = 0;
}
}
}
return candidate;
}
int main()
{
int test1[] = {3, 2, 3};
assert(majorityElement(test1, 3) == 3);
int test2[] = {2, 2, 1, 1, 1, 2, 2};
assert(majorityElement(test2, 7) == 2);
return 0;
}

16
190.c Normal file
View File

@ -0,0 +1,16 @@
#include <stdint.h>
uint32_t reverseBits(uint32_t n)
{
uint32_t ret = 0;
for (int i = 0; i < 32 && n > 0; i++, n >>= 1)
ret |= (n & 1) << (31 - i); // n 取低位放入 31 - i 的位置中
return ret;
}
int main()
{
return 0;
}

39
191.c Normal file
View File

@ -0,0 +1,39 @@
#define S2
int hammingWeight(int n)
{
#ifdef S1
int weight = 0;
for (int i = 0; i < 32; i++)
if (n & (1u << i))
weight++;
return weight;
#endif
#ifdef S1_O
int weight = 0;
while (n)
{
n &= n - 1;
weight++;
}
return weight++;
#endif
#ifdef S2
unsigned i = (unsigned)n;
i = i - ((i >> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
i = (i + (i >> 4)) & 0x0f0f0f0f;
i = i + (i >> 8);
i = i + (i >> 16);
return (int)(i & 0x3f);
#endif
}
int main()
{
return 0;
}

32
202.c Normal file
View File

@ -0,0 +1,32 @@
#include <stdbool.h>
int bitSquareSum(int n)
{
int sum = 0;
while (n > 0)
{
int bit = n % 10;
sum += bit * bit;
n /= 10;
}
return sum;
}
bool isHappy(int n)
{
int slow = n, fast = n;
do
{
slow = bitSquareSum(slow);
fast = bitSquareSum(fast);
fast = bitSquareSum(fast);
} while (slow != fast);
return slow == 1;
}
int main()
{
return 0;
}

68
225.c Normal file
View File

@ -0,0 +1,68 @@
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#define INIT_STACK_SIZE 10
#define INC_STACK_SIZE 5
typedef struct
{
int *data;
size_t size;
size_t cur;
} MyStack;
MyStack *myStackCreate()
{
MyStack *s = malloc(sizeof(MyStack));
s->data = malloc(sizeof(int) * INIT_STACK_SIZE);
s->size = INIT_STACK_SIZE;
s->cur = 0;
return s;
}
void myStackPush(MyStack *obj, int x)
{
if (obj->cur == obj->size)
{
obj->data = realloc(obj->data, sizeof(int) * (obj->size + INC_STACK_SIZE));
obj->size += INC_STACK_SIZE;
}
obj->data[obj->cur] = x;
obj->cur++;
}
int myStackPop(MyStack *obj)
{
return obj->data[--obj->cur];
}
int myStackTop(MyStack *obj)
{
return obj->data[obj->cur - 1];
}
bool myStackEmpty(MyStack *obj)
{
return obj->cur == 0;
}
void myStackFree(MyStack *obj)
{
free(obj->data);
free(obj);
}
int main()
{
MyStack *s = myStackCreate();
myStackPush(s, 1);
myStackPush(s, 2);
assert(myStackTop(s) == 2);
assert(myStackPop(s) == 2);
assert(myStackEmpty(s) == false);
return 0;
}

125
228.c Normal file
View File

@ -0,0 +1,125 @@
#include <stdio.h>
#include <stdlib.h>
#define NO
#ifdef NO
void buildRangeString(int a, int b, char ***list, int *listSize)
{
char *buffer = malloc(32);
if (a != b)
sprintf(buffer, "%d->%d", a, b);
else
sprintf(buffer, "%d", a);
*list = realloc(*list, (*listSize + 1) * sizeof(char *));
(*list)[*listSize] = buffer;
*listSize += 1;
}
char **summaryRanges(int *nums, int numsSize, int *returnSize)
{
*returnSize = 0;
if (numsSize == 0)
return NULL;
char **ss = NULL;
if (numsSize == 1)
{
buildRangeString(*nums, *nums, &ss, returnSize);
return ss;
}
int first = *nums;
for (int i = 1; i < numsSize; i++)
{
if (nums[i - 1] + 1 != nums[i])
{
buildRangeString(first, nums[i - 1], &ss, returnSize);
first = nums[i];
}
}
buildRangeString(first, nums[numsSize - 1], &ss, returnSize);
return ss;
}
#else
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
#include <string.h>
void buildRangeString(int a, int b, char **list, int *listSize)
{
char *buffer = calloc(256 * sizeof(char), sizeof(char));
if (a != b)
sprintf(buffer, "%d->%d", a, b);
else
sprintf(buffer, "%d", a);
list[*listSize] = buffer;
*listSize += 1;
}
const char empty[1][0] = {""};
char **summaryRanges(int *nums, int numsSize, int *returnSize)
{
if (numsSize == 0)
{
*returnSize = 0;
return NULL;
}
char **ss = calloc(numsSize * sizeof(char *), sizeof(char *));
if (numsSize == 1)
{
buildRangeString(*nums, *nums, ss, returnSize);
goto nonull;
}
int first = *nums;
for (int i = 1; i < numsSize; i++)
{
if (nums[i] != 1 + nums[i - 1])
{
buildRangeString(first, nums[i - 1], ss, returnSize);
first = nums[i];
}
}
buildRangeString(first, nums[numsSize - 1], ss, returnSize);
nonull:
char **ret = calloc(numsSize * sizeof(char *), sizeof(char *));
int j = 0;
for (int i = 0; i < *returnSize; i++)
{
if (ss[i] == NULL)
continue;
int len = strlen(ss[i]);
ret[j] = calloc((len + 1) * sizeof(char), sizeof(char));
for (int x = 0; x < len; x++)
{
ret[j][x] = ss[i][x];
}
j++;
}
*returnSize = j;
return ret;
}
#endif
int main()
{
int a[] = {-2147483648, -2147483647, 2147483647};
int size = 0;
char **s = summaryRanges(a, sizeof(a) / sizeof(int), &size);
for (int i = 0; i < size; i++)
{
printf("%s\n", s[i]);
free(s[i]);
}
free(s);
return 0;
}

27
231.c Normal file
View File

@ -0,0 +1,27 @@
//
// Created by aurora on 24-8-14.
//
#include <assert.h>
#include <stdbool.h>
bool isPowerOfTwo(int n)
{
if (n <= 0)
return false;
while (n / 2)
{
if (n % 2 != 0)
return false;
n /= 2;
}
return true;
}
int main()
{
assert(isPowerOfTwo(2) == true);
assert(isPowerOfTwo(0) == false);
assert(isPowerOfTwo(1) == true);
}

31
263.c Normal file
View File

@ -0,0 +1,31 @@
#include <assert.h>
#include <stdbool.h>
/*
`n` `n = 2^a * 3^b * 5^c`
235 `a = b = c = 0` `a = 1`
*/
bool isUgly(int n)
{
if (n <= 0)
return false;
int num[] = {2, 3, 5};
for (int i = 0; i < 3; i++)
{
while (n % num[i] == 0)
n /= num[i];
}
return n == 1;
}
int main()
{
assert(isUgly(6) == 1);
assert(isUgly(1) == 1);
assert(isUgly(14) == 0);
assert(isUgly(8) == 1);
return 0;
}

39
268.c Normal file
View File

@ -0,0 +1,39 @@
#include <leetcode/utils.h>
#include <assert.h>
int cmp(const void *a, const void *b)
{
return *(const int *)a - *(const int *)b;
}
int missingNumber(int *nums, int numsSize)
{
qsort(nums, numsSize, sizeof(int), cmp);
if (nums[0] != 0)
return 0;
for (int i = 1; i < numsSize; i++)
{
if (nums[i] - nums[i - 1] != 1)
return nums[i] - 1;
}
return nums[numsSize - 1] + 1;
}
int main()
{
int num[] = {3, 0, 1};
ASSERT(missingNumber(num, sizeof(num) / sizeof(int)), 2, fail);
int num2[] = {0, 1};
ASSERT(missingNumber(num2, sizeof(num2) / sizeof(int)), 2, fail);
return 0;
fail:
return -1;
}

48
278.c Normal file
View File

@ -0,0 +1,48 @@
#include <leetcode/utils.h>
#include <stdbool.h>
#define BAD 1000000000
[[gnu::__always_inline__]] inline bool isBadVersion(int version)
{
return version >= BAD;
}
#ifdef V1
int firstBadVersion(int n)
{
if (isBadVersion(n) == false)
return n + 1;
return firstBadVersion(n - 1);
}
#else
int firstBadVersion(int n)
{
int i = 0, j = n;
while (1)
{
if (!isBadVersion((j - i) / 4) && isBadVersion(j / 2))
i = (j - i) / 4, j /= 2;
else
break;
}
for (; i <= n && j > 0; i++, j--)
{
if (isBadVersion(i))
return i;
if (!isBadVersion(j))
return j + 1;
}
return i;
}
#endif
int main()
{
ASSERT(firstBadVersion(2000000000), BAD, fail);
return 0;
fail:
return -1;
}

9
2786.c Normal file
View File

@ -0,0 +1,9 @@
#include <stdio.h>
long long maxScore(int *nums, int numsSize, int x) { return 0; }
int main() {
int nums[] = {2, 3, 6, 1, 9, 2};
long long result = maxScore(nums, 6, 5);
printf("%lld\n", result);
return 0;
}

52
374.c Normal file
View File

@ -0,0 +1,52 @@
#include <leetcode/utils.h>
#include <stdio.h>
static int pick = 0;
/**
* Forward declaration of guess API.
* @param num your guess
* @return -1 if num is higher than the picked number
* 1 if num is lower than the picked number
* otherwise return 0
* int guess(int num);
*/
int guess(int num)
{
if (num > pick)
return -1;
if (num < pick)
return 1;
return 0;
}
int guessNumber(int n)
{
int low = 1, high = n;
while (low < high)
{
int res = guess((low + high) / 2);
if (res == -1)
high = (n + 1) / 2;
else if (res == 1)
low = (low + high) / 2;
else
return res;
}
return high + guess(high);
}
int main()
{
pick = 6;
ASSERT(guessNumber(10), pick, fail);
pick = 1;
ASSERT(guessNumber(2), pick, fail);
return 0;
fail:
return -1;
}

41
88.c Normal file
View File

@ -0,0 +1,41 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void merge(int *nums1, int nums1Size, int m, int *nums2, int nums2Size, int n) {
int *base = malloc(sizeof(int) * (m + n));
int idx1 = 0;
int idx2 = 0;
int idx = 0;
while (idx1 < m && idx2 < n) {
if (nums1[idx1] < nums2[idx2])
base[idx++] = nums1[idx1++];
else
base[idx++] = nums2[idx2++];
}
for (int i = idx1; i < m; i++)
base[idx++] = nums1[i];
for (int i = idx2; i < n; i++)
base[idx++] = nums2[i];
memcpy(nums1, base, nums1Size * sizeof(int));
free(base);
}
int main() {
int nums1[1] = {1};
int nums2[0] = {};
merge(nums1, 1, 1, nums2, 0, 0);
for (int i = 0; i < 1; i++)
printf("%d ", nums1[i]);
printf("\n");
return 0;
}

50
CMakeLists.txt Normal file
View File

@ -0,0 +1,50 @@
cmake_minimum_required(VERSION 3.30)
project(leetcode C)
set(CMAKE_C_STANDARD 11)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
include_directories(include)
add_compile_options(
-Wall
-Wextra
-O0
-g3
-fsanitize=address
-fsanitize=undefined
-fno-omit-frame-pointer
)
add_link_options(
-fsanitize=address
-fsanitize=undefined
)
add_executable(13 13.c)
add_executable(88 88.c)
add_executable(100 100.c)
add_executable(101 101.c)
add_executable(104 104.c)
add_executable(108 108.c)
add_executable(110 110.c)
add_executable(111 111.c)
add_executable(112 112.c)
add_executable(136 136.c)
add_executable(141 141.c)
add_executable(144 144.c)
add_executable(145 145.c)
add_executable(160 160.c)
add_executable(169 169.c)
add_executable(190 190.c)
add_executable(191 191.c)
add_executable(202 202.c)
add_executable(225 225.c)
add_executable(228 228.c)
add_executable(231 231.c)
add_executable(263 263.c)
add_executable(268 268.c)
add_executable(278 278.c)
add_executable(374 374.c)
add_executable(2786 2786.c)

9
README.md Normal file
View File

@ -0,0 +1,9 @@
## 力扣习题库
练习用的,随便写写
### 编译
```bash
mkdir build && cd build
cmake ..
cmake --build .
```

10
include/ListNode.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef LINKNODE_H
#define LINKNODE_H
struct ListNode
{
int val;
struct ListNode *next;
};
#endif // LINKNODE_H

72
include/Stack.h Normal file
View File

@ -0,0 +1,72 @@
#ifndef STACK_H
#define STACK_H
#include <stdlib.h>
struct stack_t
{
struct stack_node_t *head;
struct stack_node_t *rear;
void (*enqueue)(struct stack_t *, int);
void (*dequeue)(struct stack_t *, int *);
void (*delete)(struct stack_t *);
};
struct stack_node_t
{
int data;
struct stack_node_t *next;
};
void enqueue(struct stack_t *self, int v)
{
struct stack_node_t *n = (struct stack_node_t *)malloc(sizeof(struct stack_node_t));
n->data = v;
n->next = NULL;
if (!self->head)
{
self->head = self->rear = n;
return;
}
self->rear->next = n;
self->rear = n;
}
void dequeue(struct stack_t *self, int *r)
{
struct stack_node_t *p = self->head;
while (p)
{
if (p->next == self->rear)
break;
p = p->next;
}
*r = self->rear->data;
free(self->rear);
p->next = NULL;
self->rear = p;
}
void delete(struct stack_t *self)
{
while (self->head)
{
struct stack_node_t *tmp = self->head;
self->head = self->head->next;
free(tmp);
}
self->head = NULL;
self->rear = NULL;
}
void init_stack(struct stack_t *s)
{
s->head = NULL;
s->rear = NULL;
}
#endif // STACK_H

11
include/TreeNode.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef TREENODE_H
#define TREENODE_H
struct TreeNode
{
int val;
struct TreeNode *left;
struct TreeNode *right;
};
#endif // TREENODE_H

58
include/leetcode/utils.h Normal file
View File

@ -0,0 +1,58 @@
#ifndef UTILS_H
#define UTILS_H
#include <stdio.h>
#include <stdlib.h>
#ifndef FREE
#define FREE(p) \
do \
{ \
free(p); \
p = NULL; \
} while (0)
#endif
#ifndef MALLOC
#define MALLOC(p, s, err) \
do \
{ \
p = malloc(s); \
if (p == NULL) \
goto err; \
} while (0)
#endif
#ifndef REALLOC
#define REALLOC(p, s, err) \
do \
{ \
p = realloc(p, s); \
if (p == NULL) \
goto err; \
}
#endif
#ifndef CALLOC
#define CALLOC(p, s, err) \
do \
{ \
p = calloc(s, 1); \
if (p == NULL) \
goto err; \
} while (0)
#endif
#ifndef ASSERT
#define ASSERT(cond, except, err) \
do \
{ \
if (cond != except) \
{ \
printf("assert failed! at line: %d in %s\n", __LINE__, __FUNCTION__); \
goto err; \
} \
} while (0)
#endif
#endif // UTILS_H