Skip to content

Commit

Permalink
arr_size
Browse files Browse the repository at this point in the history
  • Loading branch information
Chong Liu committed Jan 27, 2024
1 parent 57e39c4 commit 55bc74f
Show file tree
Hide file tree
Showing 18 changed files with 102 additions and 102 deletions.
14 changes: 7 additions & 7 deletions F01_Common_Functions/inc/common_array.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*
* @FilePath : \Practice_LeetCode_C\F01_Common_Functions\inc\commonArray.h
* @FilePath : \Practice_LeetCode_C\F01_Common_Functions\inc\common_array.h
* @Author : Chong Liu
* @CreateDate : 2024-01-09 17:28:45
* @LastEditors : Chong Liu
* @LastEditTime : 2024-01-20 08:30:39
* @LastEditTime : 2024-01-27 14:24:48
* =================================================================================
* Copyright (c) 2024 by Chong Liu, All Rights Reserved.
* =================================================================================
Expand Down Expand Up @@ -38,19 +38,19 @@
/* */
/**********************************************************************************/
int Array_CmpElement(const void *a, const void *b); /* Compare array element */
int Array_Sort(int *arr, int arrSize); /* Sort array in place */
int Array_Sort2D(int **arr, int arrSize, int *arrColSizes); /* Sort 2D array in place */
int Array_Sort(int *arr, int arr_size); /* Sort array in place */
int Array_Sort2D(int **arr, int arr_size, int *arrColSizes); /* Sort 2D array in place */
int Array_Reverse(int *arr, int leftIndex, int rightIndex); /* Reverse array in place */

/**********************************************************************************/
/* */
/* HELPER FUNCTIONS */
/* */
/**********************************************************************************/
int Array_Print(int *arr, int arrSize); /* Print array */
int Array_Print2D(int **arr, int arrSize, int *arrColSizes); /* Print 2D array */
int Array_Print(int *arr, int arr_size); /* Print array */
int Array_Print2D(int **arr, int arr_size, int *arrColSizes); /* Print 2D array */
int Array_Free(int **arrPtr); /* Free array */
int Array_Free2D(int ***arrPtr, int arrSize); /* Free 2D array */
int Array_Free2D(int ***arrPtr, int arr_size); /* Free 2D array */

/**********************************************************************************/
/* */
Expand Down
2 changes: 1 addition & 1 deletion F01_Common_Functions/inc/common_binary_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct TreeNode {

/* Tree node operations */
struct TreeNode *BTree_InitNode(int value); /* Initialize a tree node */
struct TreeNode *BTree_ArrayInit(int *arr, int arrSize); /* Initialize a tree from array */
struct TreeNode *BTree_ArrayInit(int *arr, int arr_size); /* Initialize a tree from array */
int *BTree_ToArray(struct TreeNode *root, int *returnSize); /* Transform a tree to array */
void BTree_PrintTestTree(void); /* Print test tree */
void BTree_PrintValue(struct TreeNode *root); /* Print tree value as an array */
Expand Down
44 changes: 22 additions & 22 deletions F01_Common_Functions/src/common_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,37 +63,37 @@ STATIC_FUNC int CompareRows(const void *a, const void *b) {
* @description: 数组排序
* =================================================================================
* @param {int} *arr 数组
* @param {int} arrSize 数组大小
* @param {int} arr_size 数组大小
* @return {int} 排序结果
*/
int Array_Sort(int *arr, int arrSize) {
if (arr == NULL || arrSize <= 0) {
int Array_Sort(int *arr, int arr_size) {
if (arr == NULL || arr_size <= 0) {
return COMMON_ERR;
}
qsort(arr, arrSize, sizeof(int), Array_CmpElement);
qsort(arr, arr_size, sizeof(int), Array_CmpElement);
return COMMON_OK;
}

/**
* @description: 二维数组排序
* =================================================================================
* @param {int} **arr 返回数组
* @param {int} arrSize 返回数组大小
* @param {int} arr_size 返回数组大小
* @param {int} *arrColSizes 返回数组列大小
* @return {int} 排序结果
*/
int Array_Sort2D(int **arr, int arrSize, int *arrColSizes) {
if (arr == NULL || arrSize <= 0 || arrColSizes == NULL) {
int Array_Sort2D(int **arr, int arr_size, int *arrColSizes) {
if (arr == NULL || arr_size <= 0 || arrColSizes == NULL) {
return COMMON_ERR;
}
for (int i = 0; i < arrSize; i++) {
for (int i = 0; i < arr_size; i++) {
if (arr[i] == NULL || arrColSizes[i] <= 0) {
return COMMON_ERR;
}
qsort(arr[i], arrColSizes[i], sizeof(int), Array_CmpElement);
}
g_2DArrRowSize = arrSize;
qsort(arr, arrSize, sizeof(int *), CompareRows);
g_2DArrRowSize = arr_size;
qsort(arr, arr_size, sizeof(int *), CompareRows);
return COMMON_OK;
}

Expand All @@ -106,18 +106,18 @@ int Array_Sort2D(int **arr, int arrSize, int *arrColSizes) {
* @description: 打印数组
* =================================================================================
* @param {int} *arr 数组
* @param {int} arrSize 数组大小
* @param {int} arr_size 数组大小
* @return {int} 打印结果
*/
int Array_Print(int *arr, int arrSize) {
if (printf("Arr (len: %d): [", arrSize) < 0) {
int Array_Print(int *arr, int arr_size) {
if (printf("Arr (len: %d): [", arr_size) < 0) {
return COMMON_ERR;
}
for (int i = 0; i < arrSize; i++) {
for (int i = 0; i < arr_size; i++) {
if (printf("%d", arr[i]) < 0) {
return COMMON_ERR;
}
if (i < arrSize - 1) {
if (i < arr_size - 1) {
if (printf(",\t") < 0) {
return COMMON_ERR;
}
Expand All @@ -133,15 +133,15 @@ int Array_Print(int *arr, int arrSize) {
* @description: 打印二维数组
* =================================================================================
* @param {int} **2DArr 返回数组
* @param {int} arrSize 返回数组大小
* @param {int} arr_size 返回数组大小
* @param {int} *arrColSizes 返回数组列大小
* @return {int} 打印结果
*/
int Array_Print2D(int **arr, int arrSize, int *arrColSizes) {
if (printf("Ans: (row: %d, col: %d) [\n", arrSize, arrColSizes[0]) < 0) {
int Array_Print2D(int **arr, int arr_size, int *arrColSizes) {
if (printf("Ans: (row: %d, col: %d) [\n", arr_size, arrColSizes[0]) < 0) {
return COMMON_ERR;
}
for (int i = 0; i < arrSize; i++) {
for (int i = 0; i < arr_size; i++) {
if (printf(" [") < 0) {
return COMMON_ERR;
}
Expand Down Expand Up @@ -183,12 +183,12 @@ int Array_Free(int **arrPtr) {
* @description: 释放二维数组
* =================================================================================
* @param {int} ***arrPtr 返回数组
* @param {int} arrSize 返回数组大小
* @param {int} arr_size 返回数组大小
* @return {int} 打印结果
*/
int Array_Free2D(int ***arrPtr, int arrSize) {
int Array_Free2D(int ***arrPtr, int arr_size) {
if (*arrPtr != NULL) {
for (int i = 0; i < arrSize; i++) {
for (int i = 0; i < arr_size; i++) {
if ((*arrPtr)[i] != NULL) {
free((*arrPtr)[i]);
(*arrPtr)[i] = NULL;
Expand Down
14 changes: 7 additions & 7 deletions F01_Common_Functions/src/common_binary_tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -766,11 +766,11 @@ struct TreeNode *BTree_InitNode(int value) {
* @description: 将数组转换为二叉树
* =================================================================================
* @param {int*} arr 数组指针
* @param {int} arrSize 数组大小
* @param {int} arr_size 数组大小
* @return {TreeNode} *node 根节点指针
*/
struct TreeNode *BTree_ArrayInit(int* arr, int arrSize) {
if (arrSize == 0) {
struct TreeNode *BTree_ArrayInit(int* arr, int arr_size) {
if (arr_size == 0) {
return NULL;
}

Expand All @@ -779,19 +779,19 @@ struct TreeNode *BTree_ArrayInit(int* arr, int arrSize) {
root->left = NULL;
root->right = NULL;

TreeArrQueue *queue = BTree_InitArrQueue(arrSize);
TreeArrQueue *queue = BTree_InitArrQueue(arr_size);
queue->array[queue->tail++] = root;

for (int i = 1; i < arrSize; i++) {
for (int i = 1; i < arr_size; i++) {
struct TreeNode *node = queue->array[queue->head++];
if (i < arrSize) {
if (i < arr_size) {
node->left = (struct TreeNode *)malloc(sizeof(struct TreeNode));
node->left->val = arr[i];
node->left->left = NULL;
node->left->right = NULL;
queue->array[queue->tail++] = node->left;
}
if (++i < arrSize) {
if (++i < arr_size) {
node->right = (struct TreeNode *)malloc(sizeof(struct TreeNode));
node->right->val = arr[i];
node->right->left = NULL;
Expand Down
4 changes: 2 additions & 2 deletions F01_Common_Functions/test/common_array_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ TEST_F(TestCommonArray, TestArrSize) {
int arr[5] = {1, 2, 3, 4, 5};

/* 运行结果 */
int arrSize = ARR_SIZE(arr);
int arr_size = ARR_SIZE(arr);

/* Validate result (验证函数调用结果) */
EXPECT_EQ(arrSize, 5);
EXPECT_EQ(arr_size, 5);
}

/**********************************************************************************/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ struct ListNode *reverseList(struct ListNode* head){
void Test1(void) {
/* Function call (函数调用) */
int arr[] = {1, 2, 3, 4, 5};
int arrSize = ARR_SIZE(arr);
struct ListNode *arrList = LinkedList_ArrayInit(arr, arrSize);
int arr_size = ARR_SIZE(arr);
struct ListNode *arrList = LinkedList_ArrayInit(arr, arr_size);
struct ListNode *ansList = reverseList(arrList);

/* Expect result (期望结果) */
Expand All @@ -82,8 +82,8 @@ void Test1(void) {
void Test2(void) {
/* Function call (函数调用) */
int arr[] = {1};
int arrSize = ARR_SIZE(arr);
struct ListNode *arrList = LinkedList_ArrayInit(arr, arrSize);
int arr_size = ARR_SIZE(arr);
struct ListNode *arrList = LinkedList_ArrayInit(arr, arr_size);
struct ListNode *ansList = reverseList(arrList);

/* Expect result (期望结果) */
Expand All @@ -107,8 +107,8 @@ void Test2(void) {
void Test3(void) {
/* Function call (函数调用) */
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int arrSize = ARR_SIZE(arr);
struct ListNode *arrList = LinkedList_ArrayInit(arr, arrSize);
int arr_size = ARR_SIZE(arr);
struct ListNode *arrList = LinkedList_ArrayInit(arr, arr_size);
struct ListNode *ansList = reverseList(arrList);

/* Expect result (期望结果) */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ struct ListNode *swapPairs(struct ListNode* head){
void Test1(void) {
/* Function call (函数调用) */
int arr[] = {1, 2, 3, 4};
int arrSize = ARR_SIZE(arr);
struct ListNode *arrList = LinkedList_ArrayInit(arr, arrSize);
int arr_size = ARR_SIZE(arr);
struct ListNode *arrList = LinkedList_ArrayInit(arr, arr_size);
struct ListNode *ansList = swapPairs(arrList);

/* Expect result (期望结果) */
Expand All @@ -92,8 +92,8 @@ void Test1(void) {
void Test2(void) {
/* Function call (函数调用) */
int arr[] = {1, 2, 3, 4, 5};
int arrSize = ARR_SIZE(arr);
struct ListNode *arrList = LinkedList_ArrayInit(arr, arrSize);
int arr_size = ARR_SIZE(arr);
struct ListNode *arrList = LinkedList_ArrayInit(arr, arr_size);
struct ListNode *ansList = swapPairs(arrList);

/* Expect result (期望结果) */
Expand All @@ -117,8 +117,8 @@ void Test2(void) {
void Test3(void) {
/* Function call (函数调用) */
int arr[] = {1};
int arrSize = ARR_SIZE(arr);
struct ListNode *arrList = LinkedList_ArrayInit(arr, arrSize);
int arr_size = ARR_SIZE(arr);
struct ListNode *arrList = LinkedList_ArrayInit(arr, arr_size);
struct ListNode *ansList = swapPairs(arrList);

/* Expect result (期望结果) */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ struct ListNode *removeNthFromEnd(struct ListNode* head, int n){
void Test1(void) {
/* Function call (函数调用) */
int arr[] = {1, 2, 3, 4, 5};
int arrSize = ARR_SIZE(arr);
struct ListNode *newList = LinkedList_ArrayInit(arr, arrSize);
int arr_size = ARR_SIZE(arr);
struct ListNode *newList = LinkedList_ArrayInit(arr, arr_size);
int n = 2;
struct ListNode *ansList = removeNthFromEnd(newList, n);

Expand Down Expand Up @@ -109,8 +109,8 @@ void Test1(void) {
void Test2(void) {
/* Function call (函数调用) */
int arr[] = {1, 2, 3, 4, 5};
int arrSize = ARR_SIZE(arr);
struct ListNode *newList = LinkedList_ArrayInit(arr, arrSize);
int arr_size = ARR_SIZE(arr);
struct ListNode *newList = LinkedList_ArrayInit(arr, arr_size);
int n = 7;
struct ListNode *ansList = removeNthFromEnd(newList, n);

Expand Down Expand Up @@ -138,8 +138,8 @@ void Test2(void) {
void Test3(void) {
/* Function call (函数调用) */
int arr[] = {1};
int arrSize = ARR_SIZE(arr);
struct ListNode *newList = LinkedList_ArrayInit(arr, arrSize);
int arr_size = ARR_SIZE(arr);
struct ListNode *newList = LinkedList_ArrayInit(arr, arr_size);
int n = 1;
struct ListNode *ansList = removeNthFromEnd(newList, n);

Expand All @@ -166,8 +166,8 @@ void Test3(void) {
void Test4(void) {
/* Function call (函数调用) */
int arr[] = {1, 2, 3, 4, 5};
int arrSize = ARR_SIZE(arr);
struct ListNode *newList = LinkedList_ArrayInit(arr, arrSize);
int arr_size = ARR_SIZE(arr);
struct ListNode *newList = LinkedList_ArrayInit(arr, arr_size);
int n = 5;
struct ListNode *ansList = removeNthFromEnd(newList, n);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ struct ListNode *detectCycle(struct ListNode *head) {
void Test1(void) {
/* Function call (函数调用) */
int arr[] = {3, 2, 0, -4};
int arrSize = ARR_SIZE(arr);
struct ListNode *arrList = LinkedList_ArrayInit(arr, arrSize);
int arr_size = ARR_SIZE(arr);
struct ListNode *arrList = LinkedList_ArrayInit(arr, arr_size);
arrList->next->next->next->next = arrList->next; /* 将尾节点指向指定节点 */
int actual = detectCycle(arrList)->val;

Expand All @@ -87,8 +87,8 @@ void Test1(void) {
void Test2(void) {
/* Function call (函数调用) */
int arr[] = {1, 2};
int arrSize = ARR_SIZE(arr);
struct ListNode *arrList = LinkedList_ArrayInit(arr, arrSize);
int arr_size = ARR_SIZE(arr);
struct ListNode *arrList = LinkedList_ArrayInit(arr, arr_size);
arrList->next->next = arrList; /* 将尾节点指向指定节点 */
int actual = detectCycle(arrList)->val;

Expand All @@ -110,8 +110,8 @@ void Test2(void) {
void Test3(void) {
/* Function call (函数调用) */
int arr[] = {1};
int arrSize = ARR_SIZE(arr);
struct ListNode *arrList = LinkedList_ArrayInit(arr, arrSize);
int arr_size = ARR_SIZE(arr);
struct ListNode *arrList = LinkedList_ArrayInit(arr, arr_size);
struct ListNode *actual = detectCycle(arrList);

/* Expect result (期望结果) */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
#ifndef __THREE_SUM_H
#define __THREE_SUM_H

int **threeSum(int *arr, int arrSize, int *returnSize, int **returnColumnSizes);
int **threeSum(int *arr, int arr_size, int *returnSize, int **returnColumnSizes);

#endif /* __THREE_SUM_H */
Loading

0 comments on commit 55bc74f

Please sign in to comment.