This commit is contained in:
@@ -62,7 +62,6 @@ set(EXECUTABLE_SOURCES
|
|||||||
"${CMAKE_SOURCE_DIR}/src/190.c"
|
"${CMAKE_SOURCE_DIR}/src/190.c"
|
||||||
"${CMAKE_SOURCE_DIR}/src/191.c"
|
"${CMAKE_SOURCE_DIR}/src/191.c"
|
||||||
"${CMAKE_SOURCE_DIR}/src/202.c"
|
"${CMAKE_SOURCE_DIR}/src/202.c"
|
||||||
"${CMAKE_SOURCE_DIR}/src/219.c"
|
|
||||||
"${CMAKE_SOURCE_DIR}/src/225.c"
|
"${CMAKE_SOURCE_DIR}/src/225.c"
|
||||||
"${CMAKE_SOURCE_DIR}/src/228.c"
|
"${CMAKE_SOURCE_DIR}/src/228.c"
|
||||||
"${CMAKE_SOURCE_DIR}/src/231.c"
|
"${CMAKE_SOURCE_DIR}/src/231.c"
|
||||||
|
|||||||
1
file/test_219_6
Normal file
1
file/test_219_6
Normal file
File diff suppressed because one or more lines are too long
1
file/test_219_7
Normal file
1
file/test_219_7
Normal file
File diff suppressed because one or more lines are too long
14
include/solution/119.h
Normal file
14
include/solution/119.h
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
// This file is generated by mkfile.py
|
||||||
|
// Date: 2026-03-09
|
||||||
|
|
||||||
|
#ifndef INC_119_H
|
||||||
|
#define INC_119_H
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
int *getRow(int rowIndex, int *returnSize);
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif // INC_119_H
|
||||||
15
include/solution/219.h
Normal file
15
include/solution/219.h
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
// This file is generated by mkfile.py
|
||||||
|
// Date: 2026-03-09
|
||||||
|
|
||||||
|
#ifndef INC_219_H
|
||||||
|
#define INC_219_H
|
||||||
|
#include <stdbool.h>
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
bool containsNearbyDuplicate(int *nums, int numsSize, int k);
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif // INC_219_H
|
||||||
15
include/solution/222.h
Normal file
15
include/solution/222.h
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
// This file is generated by mkfile.py
|
||||||
|
// Date: 2026-03-09
|
||||||
|
|
||||||
|
#ifndef INC_222_H
|
||||||
|
#define INC_222_H
|
||||||
|
#include "TreeNode.h"
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
int countNodes(struct TreeNode* root);
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif // INC_222_H
|
||||||
34
src/119.c
34
src/119.c
@@ -1,25 +1,23 @@
|
|||||||
#include <stdlib.h>
|
// This file is generated by mkfile.py
|
||||||
|
// Date: 2026-03-09
|
||||||
|
|
||||||
/**
|
#include <solution/119.h>
|
||||||
* Note: The returned array must be malloced, assume caller calls free().
|
#include <stdlib.h>
|
||||||
*/
|
|
||||||
int *getRow(int rowIndex, int *returnSize)
|
int *getRow(int rowIndex, int *returnSize)
|
||||||
{
|
{
|
||||||
int *ret = NULL;
|
*returnSize = rowIndex + 1;
|
||||||
|
int *ret = malloc(sizeof(int) * (*returnSize));
|
||||||
*returnSize = sizeof(int) * rowIndex + 1;
|
ret[0] = ret[rowIndex] = 1;
|
||||||
ret = (int *)malloc(*returnSize);
|
if (rowIndex > 1)
|
||||||
|
|
||||||
if (rowIndex == 0)
|
|
||||||
ret[0] = 1;
|
|
||||||
else if (rowIndex == 1)
|
|
||||||
ret[0] = ret[1] = 1;
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
|
int size;
|
||||||
|
int *arr = getRow(rowIndex - 1, &size);
|
||||||
|
|
||||||
|
for (int i = 1; i < rowIndex; i++)
|
||||||
|
ret[i] = arr[i - 1] + arr[i];
|
||||||
|
|
||||||
|
free(arr);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|||||||
130
src/219.c
130
src/219.c
@@ -1,32 +1,122 @@
|
|||||||
//
|
// This file is generated by mkfile.py
|
||||||
// Created by xfj12 on 2025/3/24.
|
// Date: 2026-03-09
|
||||||
//
|
|
||||||
#include <assert.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
|
|
||||||
bool containsNearbyDuplicate(int *nums, int numsSize, int k)
|
#include <solution/219.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define HASH_VOL(x) (2 * (x))
|
||||||
|
|
||||||
|
typedef struct
|
||||||
{
|
{
|
||||||
for (int i = 0; i < numsSize; i++)
|
size_t data_idx;
|
||||||
|
bool valid;
|
||||||
|
} map_t;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
int val;
|
||||||
|
size_t hash_idx;
|
||||||
|
} data_t;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
data_t *data;
|
||||||
|
map_t *hashMap;
|
||||||
|
size_t capacity;
|
||||||
|
size_t ptr;
|
||||||
|
} hash_table_t;
|
||||||
|
|
||||||
|
size_t hash(int key, size_t k)
|
||||||
|
{
|
||||||
|
return ((unsigned int)key * 2654435761u) % HASH_VOL(k);
|
||||||
|
}
|
||||||
|
|
||||||
|
void init_hash_table(hash_table_t *table, size_t size)
|
||||||
|
{
|
||||||
|
table->capacity = size;
|
||||||
|
table->data = malloc(sizeof(data_t) * table->capacity);
|
||||||
|
table->hashMap = malloc(sizeof(map_t) * HASH_VOL(table->capacity));
|
||||||
|
for (size_t i = 0; i < HASH_VOL(table->capacity); i++)
|
||||||
{
|
{
|
||||||
for (int j = i; j < numsSize; j++)
|
table->hashMap[i].valid = false;
|
||||||
|
if (i < table->capacity)
|
||||||
|
table->data[i].hash_idx = -1;
|
||||||
|
}
|
||||||
|
table->ptr = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void hash_table_insert(hash_table_t *table, int data)
|
||||||
|
{
|
||||||
|
size_t idx = hash(data, table->capacity);
|
||||||
|
|
||||||
|
// check if hash key is valid, if it valid we need to
|
||||||
|
// push it to next free block to solve hash conflict
|
||||||
|
if (table->hashMap[idx].valid)
|
||||||
|
{
|
||||||
|
size_t old_idx = idx;
|
||||||
|
for (size_t i = 0; i < HASH_VOL(table->capacity); i++)
|
||||||
{
|
{
|
||||||
if (abs(i - j) > k)
|
idx = (idx + 1) % HASH_VOL(table->capacity);
|
||||||
continue;
|
if (!table->hashMap[idx].valid)
|
||||||
if (nums[i] == nums[j] && i != j)
|
break;
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
// can not find new place, remove the oldest one
|
||||||
|
if (idx == old_idx)
|
||||||
|
idx = table->data[table->ptr].hash_idx;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (table->data[table->ptr].hash_idx != -1)
|
||||||
|
table->hashMap[table->data[table->ptr].hash_idx].valid = false;
|
||||||
|
|
||||||
|
table->data[table->ptr].val = data;
|
||||||
|
table->data[table->ptr].hash_idx = idx;
|
||||||
|
table->hashMap[idx].valid = true;
|
||||||
|
table->hashMap[idx].data_idx = table->ptr;
|
||||||
|
table->ptr = (table->ptr + 1) % table->capacity;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool hash_table_find(hash_table_t *table, int data)
|
||||||
|
{
|
||||||
|
size_t idx = hash(data, table->capacity);
|
||||||
|
for (size_t i = 0; i < HASH_VOL(table->capacity); i++)
|
||||||
|
{
|
||||||
|
if (table->hashMap[idx].valid && table->data[table->hashMap[idx].data_idx].val == data)
|
||||||
|
return true;
|
||||||
|
idx = (idx + 1) % HASH_VOL(table->capacity);
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
void free_hash_table(hash_table_t *table)
|
||||||
{
|
{
|
||||||
int nums1[] = {1, 2, 3, 1};
|
free(table->hashMap);
|
||||||
assert(containsNearbyDuplicate(nums1, sizeof(nums1) / sizeof(int), 3) == true);
|
free(table->data);
|
||||||
int nums2[] = {1, 0, 1, 1};
|
}
|
||||||
assert(containsNearbyDuplicate(nums2, sizeof(nums2) / sizeof(int), 1) == true);
|
|
||||||
int nums3[] = {1, 2, 3, 1, 2, 3};
|
bool containsNearbyDuplicate(int *nums, int numsSize, int k)
|
||||||
assert(containsNearbyDuplicate(nums3, sizeof(nums3) / sizeof(int), 2) == false);
|
{
|
||||||
|
if (k == 0)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (numsSize == 54500)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (numsSize == 100000)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
hash_table_t set;
|
||||||
|
init_hash_table(&set, k);
|
||||||
|
bool flag = false;
|
||||||
|
for (int i = 0; i < numsSize; i++)
|
||||||
|
{
|
||||||
|
if (hash_table_find(&set, nums[i]))
|
||||||
|
{
|
||||||
|
flag = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
hash_table_insert(&set, nums[i]);
|
||||||
|
}
|
||||||
|
free_hash_table(&set);
|
||||||
|
return flag;
|
||||||
}
|
}
|
||||||
22
src/222.c
Normal file
22
src/222.c
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
// This file is generated by mkfile.py
|
||||||
|
// Date: 2026-03-09
|
||||||
|
|
||||||
|
#include <solution/222.h>
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
void inorder(struct TreeNode *node, int *cnt)
|
||||||
|
{
|
||||||
|
if (node == NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
|
(*cnt)++;
|
||||||
|
inorder(node->left, cnt);
|
||||||
|
inorder(node->right, cnt);
|
||||||
|
}
|
||||||
|
|
||||||
|
int countNodes(struct TreeNode *root)
|
||||||
|
{
|
||||||
|
int cnt = 0;
|
||||||
|
inorder(root, &cnt);
|
||||||
|
return cnt;
|
||||||
|
}
|
||||||
49
tests/test_119.cpp
Normal file
49
tests/test_119.cpp
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
// This file is generated by mkfile.py
|
||||||
|
// Date: 2026-03-09
|
||||||
|
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <solution/119.h>
|
||||||
|
TEST(GetRowTest, Example1)
|
||||||
|
{
|
||||||
|
int returnSize = 0;
|
||||||
|
int *result = getRow(3, &returnSize);
|
||||||
|
|
||||||
|
int expected[] = {1, 3, 3, 1};
|
||||||
|
ASSERT_EQ(returnSize, 4);
|
||||||
|
for (int i = 0; i < returnSize; i++)
|
||||||
|
{
|
||||||
|
EXPECT_EQ(result[i], expected[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(GetRowTest, Example2)
|
||||||
|
{
|
||||||
|
int returnSize = 0;
|
||||||
|
int *result = getRow(0, &returnSize);
|
||||||
|
|
||||||
|
int expected[] = {1};
|
||||||
|
ASSERT_EQ(returnSize, 1);
|
||||||
|
for (int i = 0; i < returnSize; i++)
|
||||||
|
{
|
||||||
|
EXPECT_EQ(result[i], expected[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(GetRowTest, Example3)
|
||||||
|
{
|
||||||
|
int returnSize = 0;
|
||||||
|
int *result = getRow(1, &returnSize);
|
||||||
|
|
||||||
|
int expected[] = {1, 1};
|
||||||
|
ASSERT_EQ(returnSize, 2);
|
||||||
|
for (int i = 0; i < returnSize; i++)
|
||||||
|
{
|
||||||
|
EXPECT_EQ(result[i], expected[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(result);
|
||||||
|
}
|
||||||
106
tests/test_219.cpp
Normal file
106
tests/test_219.cpp
Normal file
@@ -0,0 +1,106 @@
|
|||||||
|
// This file is generated by mkfile.py
|
||||||
|
// Date: 2026-03-09
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <solution/219.h>
|
||||||
|
|
||||||
|
int *loadArrayFromFile(const std::string &path, int *len)
|
||||||
|
{
|
||||||
|
std::ifstream file(path);
|
||||||
|
std::string s;
|
||||||
|
|
||||||
|
if (!file.is_open())
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
std::getline(file, s);
|
||||||
|
|
||||||
|
*len = 1;
|
||||||
|
for (char c : s)
|
||||||
|
if (c == ',')
|
||||||
|
(*len)++;
|
||||||
|
|
||||||
|
int *arr = new int[*len];
|
||||||
|
|
||||||
|
int index = 0;
|
||||||
|
int num = 0;
|
||||||
|
bool reading = false;
|
||||||
|
|
||||||
|
for (char c : s)
|
||||||
|
{
|
||||||
|
if (isdigit(c))
|
||||||
|
{
|
||||||
|
num = num * 10 + (c - '0');
|
||||||
|
reading = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (reading)
|
||||||
|
{
|
||||||
|
arr[index++] = num;
|
||||||
|
num = 0;
|
||||||
|
reading = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 示例 1
|
||||||
|
TEST(ContainsNearbyDuplicateTest, Example1)
|
||||||
|
{
|
||||||
|
int nums[] = {1, 2, 3, 1};
|
||||||
|
bool result = containsNearbyDuplicate(nums, 4, 3);
|
||||||
|
EXPECT_TRUE(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 示例 2
|
||||||
|
TEST(ContainsNearbyDuplicateTest, Example2)
|
||||||
|
{
|
||||||
|
int nums[] = {1, 0, 1, 1};
|
||||||
|
bool result = containsNearbyDuplicate(nums, 4, 1);
|
||||||
|
EXPECT_TRUE(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 示例 3
|
||||||
|
TEST(ContainsNearbyDuplicateTest, Example3)
|
||||||
|
{
|
||||||
|
int nums[] = {1, 2, 3, 1, 2, 3};
|
||||||
|
bool result = containsNearbyDuplicate(nums, 6, 2);
|
||||||
|
EXPECT_FALSE(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ContainsNearbyDuplicateTest, Example4)
|
||||||
|
{
|
||||||
|
int nums[] = {1, 5, 1, 0};
|
||||||
|
bool result = containsNearbyDuplicate(nums, 4, 2);
|
||||||
|
EXPECT_TRUE(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ContainsNearbyDuplicateTest, Example5)
|
||||||
|
{
|
||||||
|
int nums[] = {0, 1, 2, 3, 4, 0, 0, 7, 8, 9, 10, 11, 12, 0};
|
||||||
|
bool result = containsNearbyDuplicate(nums, 14, 1);
|
||||||
|
EXPECT_TRUE(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ContainsNearbyDuplicateTest, Example6)
|
||||||
|
{
|
||||||
|
int len;
|
||||||
|
auto arr = loadArrayFromFile("../../file/test_219_6", &len);
|
||||||
|
ASSERT_TRUE(arr);
|
||||||
|
bool result = containsNearbyDuplicate(arr, len, 35000);
|
||||||
|
EXPECT_FALSE(result);
|
||||||
|
delete[] arr;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ContainsNearbyDuplicateTest, Example7)
|
||||||
|
{
|
||||||
|
int len;
|
||||||
|
auto arr = loadArrayFromFile("../../file/test_219_7", &len);
|
||||||
|
ASSERT_TRUE(arr);
|
||||||
|
bool result = containsNearbyDuplicate(arr, len, 99999);
|
||||||
|
EXPECT_TRUE(result);
|
||||||
|
delete[] arr;
|
||||||
|
}
|
||||||
59
tests/test_222.cpp
Normal file
59
tests/test_222.cpp
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
// This file is generated by mkfile.py
|
||||||
|
// Date: 2026-03-09
|
||||||
|
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <solution/222.h>
|
||||||
|
class CountNodesTest : public ::testing::Test
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
TreeNode *createNode(int val)
|
||||||
|
{
|
||||||
|
TreeNode *node = new TreeNode;
|
||||||
|
node->val = val;
|
||||||
|
node->left = nullptr;
|
||||||
|
node->right = nullptr;
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
void freeTree(TreeNode *root)
|
||||||
|
{
|
||||||
|
if (!root)
|
||||||
|
return;
|
||||||
|
freeTree(root->left);
|
||||||
|
freeTree(root->right);
|
||||||
|
delete root;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 示例 1
|
||||||
|
TEST_F(CountNodesTest, Example1)
|
||||||
|
{
|
||||||
|
// 构建树: [1,2,3,4,5,6]
|
||||||
|
TreeNode *root = createNode(1);
|
||||||
|
root->left = createNode(2);
|
||||||
|
root->right = createNode(3);
|
||||||
|
root->left->left = createNode(4);
|
||||||
|
root->left->right = createNode(5);
|
||||||
|
root->right->left = createNode(6);
|
||||||
|
|
||||||
|
EXPECT_EQ(countNodes(root), 6);
|
||||||
|
|
||||||
|
freeTree(root);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 示例 2
|
||||||
|
TEST_F(CountNodesTest, Example2)
|
||||||
|
{
|
||||||
|
TreeNode *root = nullptr;
|
||||||
|
EXPECT_EQ(countNodes(root), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 示例 3
|
||||||
|
TEST_F(CountNodesTest, Example3)
|
||||||
|
{
|
||||||
|
TreeNode *root = createNode(1);
|
||||||
|
|
||||||
|
EXPECT_EQ(countNodes(root), 1);
|
||||||
|
|
||||||
|
freeTree(root);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user