Compare commits

..

40 Commits

Author SHA1 Message Date
6e2206cabe Update Ubuntu mirror in test.yaml for dependency installation
Some checks failed
Gitea CTest Workflow / test (push) Failing after 1m34s
2026-02-05 00:07:55 +08:00
89a41445ef Update minimum CMake version to 3.25 in CMakeLists.txt
Some checks failed
Gitea CTest Workflow / test (push) Has been cancelled
2026-02-05 00:05:54 +08:00
cdb5602c47 Add step to install dependencies in test.yaml
Some checks failed
Gitea CTest Workflow / test (push) Failing after 6m10s
2026-02-04 23:58:46 +08:00
3559f83475 Replace checkout action with direct git clone command in test.yaml
Some checks failed
Gitea CTest Workflow / test (push) Failing after 4s
2026-02-04 23:57:04 +08:00
6f30721f1b Update checkout action URL in test.yaml to use version 6
Some checks failed
Gitea CTest Workflow / test (push) Failing after 42s
2026-02-04 23:55:46 +08:00
9a8260a3b6 Remove token from test.yaml
Some checks failed
Gitea CTest Workflow / test (push) Failing after 32s
2026-02-04 23:53:07 +08:00
fc2fd50bac Update test.yaml to specify server URL and repository for Git operations
Some checks failed
Gitea CTest Workflow / test (push) Failing after 4s
2026-02-04 23:52:24 +08:00
aa3f652a8f Update repository URL in test.yaml
Some checks failed
Gitea CTest Workflow / test (push) Failing after 4s
2026-02-04 23:47:52 +08:00
b6a1850f48 Update checkout action URL in test.yaml to use the mirror repository
Some checks failed
Gitea CTest Workflow / test (push) Failing after 44s
2026-02-04 23:06:18 +08:00
737c8e17dd Add Gitea CTest workflow configuration
Some checks failed
Gitea CTest Workflow / test (push) Has been cancelled
2026-02-04 22:59:05 +08:00
2cf5db11bd Remove debug compile options from CMakeLists.txt 2025-12-06 00:05:53 +08:00
7ae0aeb744 Implement dynamic programming solution for tribonacci and fix index calculation in sequence matching 2025-12-06 00:03:28 +08:00
cf633352b5 1137 2025-12-05 23:10:18 +08:00
45162bb934 修复CMakeLists.txt中的MSVC支持检查,并调整代码格式 2025-12-05 23:10:08 +08:00
dc222231e6 3397 2025-12-05 22:41:20 +08:00
8fadb725e1 1039 2025-12-05 22:41:12 +08:00
e862aea336 1668 2025-12-05 21:59:22 +08:00
e580f911cb 1025 2025-12-05 21:32:57 +08:00
727096b8e6 509 2025-12-05 21:18:40 +08:00
36c5339b1f 746 2025-12-05 21:05:14 +08:00
2e8537eb7f 在 CMakeLists.txt 中启用测试功能,并清理测试目录的多余代码 2025-12-05 21:00:05 +08:00
6731384638 更新 mkfile.py 脚本的 shebang 行,确保使用正确的 Python 解释器 2025-12-05 20:13:46 +08:00
91167d0d3b 重构 countBits 函数,修复返回值并添加 ASSERT 宏以增强测试 2025-12-05 20:09:10 +08:00
fc2d54f926 更新 googletest 下载链接以使用 gh-proxy 2025-12-05 20:08:47 +08:00
c0a4f0649b new mkfile script 2025-09-29 11:14:37 +08:00
050164b61b format files 2025-09-29 10:56:26 +08:00
af0fe8a168 976 2025-09-29 10:56:06 +08:00
f6e998beff 1717 2025-07-24 18:31:35 +08:00
5f7ef7b880 1957 2025-07-23 20:19:00 +08:00
b9a758a025 1233 2025-07-21 18:14:17 +08:00
cb718130f7 345 2025-07-20 11:06:49 +08:00
f54c320ebf 392 2025-07-18 22:37:04 +08:00
6df3e30818 387 2025-07-18 22:23:57 +08:00
2a7d5ed0cc 2163 2025-07-18 22:14:43 +08:00
882ac55f7f fix build error on Linux 2025-07-17 21:39:55 +08:00
bb4071ea04 326 2025-07-17 20:55:46 +08:00
4b5d8c5c22 3202 2025-07-17 20:43:48 +08:00
e924811683 mkfile 2025-07-17 17:24:50 +08:00
e3b069be8c 3201 2025-07-17 17:19:52 +08:00
084c5f7e11 3136 2025-07-15 19:06:57 +08:00
89 changed files with 1635 additions and 44 deletions

View File

@@ -1 +1 @@
BasedOnStyle: Microsoft
BasedOnStyle: Microsoft

View File

@@ -0,0 +1,36 @@
name: Gitea CTest Workflow
on:
push:
branches: [ "main", "master" ]
pull_request:
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
run: |
git clone https://cantyonion.site/git/cantyonion/leetcode.git .
- name: Install Dependencies
run: |
sudo sed -i 's@//.*archive.ubuntu.com@//mirrors.ustc.edu.cn@g' /etc/apt/sources.list.d/ubuntu.sources
sudo apt-get update
sudo apt-get install -y build-essential cmake
- name: Configure CMake
# -B build 创建构建目录,-S . 指定源代码在当前目录
run: cmake -B build -S . -DCMAKE_BUILD_TYPE=Release
- name: Build with CMake
# 使用 --parallel 充分利用 Runner 核心数
run: cmake --build build --parallel $(nproc)
- name: Run CTest via CMake
# 在 build 目录下运行 ctest
# --output-on-failure 可以在测试失败时直接看到 log
run: |
cd build
ctest --output-on-failure --parallel $(nproc)

View File

@@ -1,10 +1,15 @@
cmake_minimum_required(VERSION 3.30)
cmake_minimum_required(VERSION 3.25)
project(leetcode C CXX)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(GTEST_VERSION 1.17.0)
enable_testing()
if (MSVC)
message(FATAL_ERROR "MSVC is not supported. Please use GCC or Clang.")
endif ()
include_directories(include)
@@ -13,9 +18,6 @@ include(FetchContent)
add_compile_options(
-Wall
-Wextra
-O0
-g3
-fno-omit-frame-pointer
)
if (UNIX)
@@ -26,12 +28,13 @@ if (UNIX)
add_link_options(
-fsanitize=address
-fsanitize=undefined
-lm
)
endif ()
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/releases/download/v${GTEST_VERSION}/googletest-${GTEST_VERSION}.tar.gz
URL https://gh-proxy.org/https://github.com/google/googletest/releases/download/v${GTEST_VERSION}/googletest-${GTEST_VERSION}.tar.gz
)
FetchContent_MakeAvailable(googletest)
@@ -72,10 +75,10 @@ set(EXECUTABLE_SOURCES
"${CMAKE_SOURCE_DIR}/src/3110.c"
)
foreach(src_file IN LISTS EXECUTABLE_SOURCES)
foreach (src_file IN LISTS EXECUTABLE_SOURCES)
get_filename_component(exe_name "${src_file}" NAME_WE)
add_executable(${exe_name} ${src_file})
endforeach()
endforeach ()
set(LIBRARY_SOURCES)

16
include/solution/1025.h Normal file
View File

@@ -0,0 +1,16 @@
// This file is generated by mkfile.py
// Date: 2025-12-05
#ifndef INC_1025_H
#define INC_1025_H
#include <stdbool.h>
#ifdef __cplusplus
extern "C"
{
#endif
bool divisorGame(int n);
#ifdef __cplusplus
}
#endif
#endif // INC_1025_H

14
include/solution/1039.h Normal file
View File

@@ -0,0 +1,14 @@
// This file is generated by mkfile.py
// Date: 2025-09-29
#ifndef INC_1039_H
#define INC_1039_H
#ifdef __cplusplus
extern "C"
{
#endif
int minScoreTriangulation(int *values, int valuesSize);
#ifdef __cplusplus
}
#endif
#endif // INC_1039_H

14
include/solution/1137.h Normal file
View File

@@ -0,0 +1,14 @@
// This file is generated by mkfile.py
// Date: 2025-12-05
#ifndef INC_1137_H
#define INC_1137_H
#ifdef __cplusplus
extern "C"
{
#endif
int tribonacci(int n);
#ifdef __cplusplus
}
#endif
#endif // INC_1137_H

11
include/solution/1233.h Normal file
View File

@@ -0,0 +1,11 @@
#ifndef INC_1233_H
#define INC_1233_H
#ifdef __cplusplus
extern "C"
{
#endif
char **removeSubfolders(char **folder, int folderSize, int *returnSize);
#ifdef __cplusplus
}
#endif
#endif

14
include/solution/1668.h Normal file
View File

@@ -0,0 +1,14 @@
// This file is generated by mkfile.py
// Date: 2025-12-05
#ifndef INC_1668_H
#define INC_1668_H
#ifdef __cplusplus
extern "C"
{
#endif
int maxRepeating(char *sequence, char *word);
#ifdef __cplusplus
}
#endif
#endif // INC_1668_H

11
include/solution/1717.h Normal file
View File

@@ -0,0 +1,11 @@
#ifndef INC_1717_H
#define INC_1717_H
#ifdef __cplusplus
extern "C"
{
#endif
int maximumGain(char *s, int x, int y);
#ifdef __cplusplus
}
#endif
#endif

11
include/solution/1957.h Normal file
View File

@@ -0,0 +1,11 @@
#ifndef INC_1957_H
#define INC_1957_H
#ifdef __cplusplus
extern "C"
{
#endif
char *makeFancyString(char *s);
#ifdef __cplusplus
}
#endif
#endif

11
include/solution/2163.h Normal file
View File

@@ -0,0 +1,11 @@
#ifndef INC_2163_H
#define INC_2163_H
#ifdef __cplusplus
extern "C"
{
#endif
long long minimumDifference(int *nums, int numsSize);
#ifdef __cplusplus
}
#endif
#endif

12
include/solution/3136.h Normal file
View File

@@ -0,0 +1,12 @@
#ifndef INC_3136_H
#define INC_3136_H
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdbool.h>
bool isValid(char *word);
#ifdef __cplusplus
}
#endif
#endif

11
include/solution/3201.h Normal file
View File

@@ -0,0 +1,11 @@
#ifndef INC_3201_H
#define INC_3201_H
#ifdef __cplusplus
extern "C"
{
#endif
int maximumLength(int *nums, int numsSize);
#ifdef __cplusplus
}
#endif
#endif

11
include/solution/3202.h Normal file
View File

@@ -0,0 +1,11 @@
#ifndef INC_3202_H
#define INC_3202_H
#ifdef __cplusplus
extern "C"
{
#endif
int maximumLength(int *nums, int numsSize, int k);
#ifdef __cplusplus
}
#endif
#endif

12
include/solution/326.h Normal file
View File

@@ -0,0 +1,12 @@
#ifndef INC_326_H
#define INC_326_H
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdbool.h>
bool isPowerOfThree(int n);
#ifdef __cplusplus
}
#endif
#endif

14
include/solution/3397.h Normal file
View File

@@ -0,0 +1,14 @@
// This file is generated by mkfile.py
// Date: 2025-10-18
#ifndef INC_3397_H
#define INC_3397_H
#ifdef __cplusplus
extern "C"
{
#endif
int maxDistinctElements(int* nums, int numsSize, int k);
#ifdef __cplusplus
}
#endif
#endif // INC_3397_H

11
include/solution/345.h Normal file
View File

@@ -0,0 +1,11 @@
#ifndef INC_345_H
#define INC_345_H
#ifdef __cplusplus
extern "C"
{
#endif
char *reverseVowels(char *s);
#ifdef __cplusplus
}
#endif
#endif

11
include/solution/387.h Normal file
View File

@@ -0,0 +1,11 @@
#ifndef INC_387_H
#define INC_387_H
#ifdef __cplusplus
extern "C"
{
#endif
int firstUniqChar(char *s);
#ifdef __cplusplus
}
#endif
#endif

12
include/solution/392.h Normal file
View File

@@ -0,0 +1,12 @@
#ifndef INC_392_H
#define INC_392_H
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdbool.h>
bool isSubsequence(char *s, char *t);
#ifdef __cplusplus
}
#endif
#endif

14
include/solution/509.h Normal file
View File

@@ -0,0 +1,14 @@
// This file is generated by mkfile.py
// Date: 2025-12-05
#ifndef INC_509_H
#define INC_509_H
#ifdef __cplusplus
extern "C"
{
#endif
int fib(int n);
#ifdef __cplusplus
}
#endif
#endif // INC_509_H

14
include/solution/746.h Normal file
View File

@@ -0,0 +1,14 @@
// This file is generated by mkfile.py
// Date: 2025-12-05
#ifndef INC_746_H
#define INC_746_H
#ifdef __cplusplus
extern "C"
{
#endif
int minCostClimbingStairs(int *cost, int costSize);
#ifdef __cplusplus
}
#endif
#endif // INC_746_H

11
include/solution/976.h Normal file
View File

@@ -0,0 +1,11 @@
#ifndef INC_976_H
#define INC_976_H
#ifdef __cplusplus
extern "C"
{
#endif
int largestPerimeter(int* nums, int numsSize);
#ifdef __cplusplus
}
#endif
#endif

41
mkfile.py Executable file
View File

@@ -0,0 +1,41 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import datetime
if len(sys.argv) != 2:
print("Usage: mkfile.py <filename>")
sys.exit(1)
filename = sys.argv[1]
# create header file
with open(f"./include/solution/{filename.lower()}.h", "w") as f:
f.write("// This file is generated by mkfile.py\n")
f.write(f"// Date: {datetime.datetime.now().strftime('%Y-%m-%d')}\n\n")
f.write(f"#ifndef INC_{filename.upper()}_H\n")
f.write(f"#define INC_{filename.upper()}_H\n")
f.write("#ifdef __cplusplus\n")
f.write("extern \"C\"\n")
f.write("{\n")
f.write("#endif\n\n")
f.write("#ifdef __cplusplus\n")
f.write("}\n")
f.write("#endif\n")
f.write(f"#endif // INC_{filename.upper()}_H\n")
f.close()
# create source file
with open(f"./src/{filename.lower()}.c", "w") as f:
f.write("// This file is generated by mkfile.py\n")
f.write(f"// Date: {datetime.datetime.now().strftime('%Y-%m-%d')}\n\n")
f.write(f'#include <solution/{filename.lower()}.h>\n')
f.close()
# create test file
with open(f"./tests/test_{filename.lower()}.cpp", "w") as f:
f.write("// This file is generated by mkfile.py\n")
f.write(f"// Date: {datetime.datetime.now().strftime('%Y-%m-%d')}\n\n")
f.write(f'#include <solution/{filename.lower()}.h>\n')
f.write("#include <gtest/gtest.h>\n")
f.close()

7
src/1025.c Normal file
View File

@@ -0,0 +1,7 @@
// This file is generated by mkfile.py
// Date: 2025-12-05
#include <solution/1025.h>
bool divisorGame(int n)
{
return n % 2 == 0;
}

30
src/1039.c Normal file
View File

@@ -0,0 +1,30 @@
// This file is generated by mkfile.py
// Date: 2025-09-29
#include <limits.h>
#include <solution/1039.h>
#define min(x, y) (x) < (y) ? (x) : (y)
int minScoreTriangulation(int *values, int valuesSize)
{
int dp[valuesSize][valuesSize];
for (int i = 0; i < valuesSize; i++)
for (int j = 0; j < valuesSize; j++)
dp[i][j] = 0;
for (int len = 2; len < valuesSize; len++)
{
for (int i = 0; i + len < valuesSize; i++)
{
int j = i + len;
dp[i][j] = INT_MAX;
for (int k = i + 1; k < j; k++)
{
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j] + values[i] * values[j] * values[k]);
}
}
}
return dp[0][valuesSize - 1];
}

17
src/1137.c Normal file
View File

@@ -0,0 +1,17 @@
// This file is generated by mkfile.py
// Date: 2025-12-05
#include <solution/1137.h>
int tribonacci(int n)
{
int dp[n + 1];
dp[0] = 0;
if (n >= 1)
dp[1] = 1;
if (n >= 2)
dp[2] = 1;
for (int i = 3; i <= n; i++)
dp[i] = dp[i - 3] + dp[i - 2] + dp[i - 1];
return dp[n];
}

View File

@@ -22,4 +22,4 @@ int *getRow(int rowIndex, int *returnSize)
int main()
{
}
}

30
src/1233.c Normal file
View File

@@ -0,0 +1,30 @@
#include <solution/1233.h>
#include <stdbool.h>
#include <stdlib.h>
bool startwith(char *p, char *s)
{
for (; *p && *s; p++, s++)
if (*p != *s)
return false;
return (!*p && *s) || (!*p && !*s);
}
char **removeSubfolders(char **folder, int folderSize, int *returnSize)
{
char **ret = malloc(sizeof(char *));
*returnSize = 1;
ret[0] = folder[0];
for (int i = 1; i < folderSize; i++)
{
if (startwith(ret[*(returnSize)-1], folder[i]))
continue;
ret = realloc(ret, sizeof(char *) * ++(*returnSize));
ret[*returnSize - 1] = folder[i];
}
return ret;
}

View File

@@ -9,4 +9,4 @@ int getDecimalValue(struct ListNode *head)
head = head->next;
}
return x;
}
}

View File

@@ -1,7 +1,7 @@
//
// Created by xfj12 on 2025/4/14.
//
#include <math.h>
#include <stdlib.h>
#include <solution/1534.h>
int countGoodTriplets(int *arr, int arrSize, int a, int b, int c)
{
@@ -14,4 +14,4 @@ int countGoodTriplets(int *arr, int arrSize, int a, int b, int c)
count++;
return count;
}
}

38
src/1668.c Normal file
View File

@@ -0,0 +1,38 @@
// This file is generated by mkfile.py
// Date: 2025-12-05
#include <solution/1668.h>
#include <stdbool.h>
#include <string.h>
#define max(x, y) ((x) > (y) ? (x) : (y))
int maxRepeating(char *sequence, char *word)
{
int n = strlen(sequence), m = strlen(word);
if (n < m)
return 0;
int dp[n];
memset(dp, 0, sizeof(dp));
for (int i = m - 1; i < n; i++)
{
bool valid = true;
for (int j = 0; j < m; j++)
{
if (sequence[i - m + 1 + j] != word[j])
{
valid = false;
break;
}
}
if (valid)
dp[i] = (i == m - 1 ? 0 : dp[i - m]) + 1;
}
int ret = dp[0];
for (int i = 1; i < n; i++)
ret = max(ret, dp[i]);
return ret;
}

View File

@@ -35,4 +35,4 @@ int main()
printf("%s\n", ans);
free(ans);
return 0;
}
}

View File

@@ -22,4 +22,4 @@ int main()
assert(titleToNumber("ZY") == 701);
return 0;
}
}

91
src/1717.c Normal file
View File

@@ -0,0 +1,91 @@
#include <solution/1717.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
char *base;
size_t size;
size_t top;
} stack_t;
void init_stack(stack_t *s)
{
s->base = malloc(sizeof(char) * 10);
s->size = 10;
s->top = 0;
}
void push(stack_t *s, char c)
{
if (s->top + 1 >= s->size)
{
s->size += 10;
s->base = realloc(s->base, sizeof(char) * s->size);
}
s->base[++(s->top)] = c;
}
char pop(stack_t *s)
{
if (s->top == 0)
return '\0';
return s->base[(s->top)--];
}
void clear(stack_t *s)
{
s->top = 0;
}
char top(stack_t *s)
{
return s->base[s->top];
}
char *sts(stack_t *s)
{
char *ret = malloc(sizeof(char) * (s->top + 1));
memcpy(ret, s->base + 1, s->top);
ret[s->top] = '\0';
return ret;
}
void delete_stack(stack_t *s)
{
free(s->base);
}
char *del(char *s, char m1, char m2, int *score, int v)
{
stack_t st;
init_stack(&st);
while (*s)
{
if (top(&st) == m1 && *s == m2)
{
s += 1, *score += v;
pop(&st);
}
else
push(&st, *(s++));
}
char *r = sts(&st);
delete_stack(&st);
return r;
}
int maximumGain(char *s, int x, int y)
{
int score = 0;
if (x > y)
del(del(s, 'a', 'b', &score, x), 'b', 'a', &score, y);
else
del(del(s, 'b', 'a', &score, y), 'a', 'b', &score, x);
return score;
}

View File

@@ -3,4 +3,4 @@
int *earliestAndLatest(int n, int firstPlayer, int secondPlayer, int *returnSize)
{
return 0;
}
}

13
src/1957.c Normal file
View File

@@ -0,0 +1,13 @@
#include <solution/1957.h>
#include <string.h>
char *makeFancyString(char *s)
{
int d[26] = {0};
int len = strlen(s);
for (int i = len - 1; i >= 0; i--)
if (++(d[s[i] - 'a']) > 3)
for (int j = i; j < len - 1; j++)
s[j] = s[j + 1];
return s;
}

4
src/2163.c Normal file
View File

@@ -0,0 +1,4 @@
#include <solution/2163.h>
long long minimumDifference(int *nums, int numsSize)
{
}

View File

@@ -2,7 +2,7 @@
// Created by xfj12 on 2025/3/24.
//
#include <assert.h>
#include <math.h>
#include <stdlib.h>
#include <stdbool.h>
bool containsNearbyDuplicate(int *nums, int numsSize, int k)
@@ -29,4 +29,4 @@ int main()
assert(containsNearbyDuplicate(nums2, sizeof(nums2) / sizeof(int), 1) == true);
int nums3[] = {1, 2, 3, 1, 2, 3};
assert(containsNearbyDuplicate(nums3, sizeof(nums3) / sizeof(int), 2) == false);
}
}

View File

@@ -65,4 +65,4 @@ int main()
assert(myStackEmpty(s) == false);
return 0;
}
}

View File

@@ -122,4 +122,4 @@ int main()
free(s);
return 0;
}
}

View File

@@ -24,4 +24,4 @@ int main()
assert(isPowerOfTwo(2) == true);
assert(isPowerOfTwo(0) == false);
assert(isPowerOfTwo(1) == true);
}
}

View File

@@ -69,4 +69,4 @@ void myQueueFree(MyQueue *obj)
free(obj->s1);
free(obj->s2);
free(obj);
}
}

View File

@@ -27,4 +27,4 @@ int matchPlayersAndTrainers(int *players, int playersSize, int *trainers, int tr
}
return match;
}
}

View File

@@ -28,4 +28,4 @@ int main()
assert(isUgly(14) == 0);
assert(isUgly(8) == 1);
return 0;
}
}

View File

@@ -36,4 +36,4 @@ int main()
fail:
return -1;
}
}

View File

@@ -15,4 +15,4 @@ long long minimumCost(char *s)
ans += min(i, n - i);
return ans;
}
}

View File

@@ -45,4 +45,4 @@ int main()
return 0;
fail:
return -1;
}
}

View File

@@ -70,4 +70,4 @@ bool wordPattern(char *pattern, char *s)
freeDict(d);
return true;
}
}

View File

@@ -49,4 +49,4 @@ int *numberGame(int *nums, int numsSize, int *returnSize)
printf("%d ", result[i]);
return result;
}
}

View File

@@ -41,4 +41,4 @@ void numArrayFree(NumArray *obj)
{
free(obj->data);
free(obj);
}
}

View File

@@ -1,5 +1,5 @@
#include <assert.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
//
// Created by xfj12 on 2025/3/24.
@@ -18,4 +18,4 @@ int main()
{
assert(scoreOfString("hello") == 13);
assert(scoreOfString("zaz") == 50);
}
}

33
src/3136.c Normal file
View File

@@ -0,0 +1,33 @@
#include <ctype.h>
#include <solution/3136.h>
bool isVowel(char c)
{
return c == 'a' || c == 'A' || c == 'e' || c == 'E' || c == 'i' || c == 'I' || c == 'o' || c == 'O' || c == 'u' ||
c == 'U';
}
bool isConsonant(char c)
{
return ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) && !isVowel(c);
}
bool isValid(char *word)
{
int length = 0;
bool haveVowel = false;
bool haveConsonant = false;
for (; *word != '\0'; word++, length++)
{
haveVowel = haveVowel || isVowel(*word);
haveConsonant = haveConsonant || isConsonant(*word);
if (!isdigit(*word) && !isVowel(*word) && !isConsonant(*word))
return false;
}
if (length < 3)
return false;
return haveVowel && haveConsonant;
}

View File

@@ -39,4 +39,4 @@ int countDays(int days, int **meetings, int meetingsSize, int *meetingsColSize)
return count + days - meetings[meetingsSize - 1][1];
return count;
}
}

13
src/3201.c Normal file
View File

@@ -0,0 +1,13 @@
#include <solution/3201.h>
#define max(x, y) ((x) > (y) ? (x) : (y))
int maximumLength(int *nums, int numsSize)
{
int odd = 0, even = 0, odd_cnt = 0;
for (int i = 0; i < numsSize; i++)
nums[i] & 1 ? (odd = even + 1, odd_cnt++) : (even = odd + 1);
return max(max(even, odd), max(odd_cnt, numsSize - odd_cnt));
}

5
src/3202.c Normal file
View File

@@ -0,0 +1,5 @@
#include <solution/3202.h>
int maximumLength(int *nums, int numsSize, int k)
{
}

23
src/326.c Normal file
View File

@@ -0,0 +1,23 @@
#define METHOD 2
#include <solution/326.h>
bool isPowerOfThree(int n)
{
#if METHOD == 1
if (n <= 0)
return false;
while (n != 1)
{
if (n % 3 != 0)
return false;
n /= 3;
}
return true;
#elif METHOD == 2
// 在题目给定的 32 位有符号整数的范围内,最大的 3 的幂为 3^19 = 1162261467。
// 我们只需要判断 n 是否是 3^19 的约数即可。
return n > 0 && 1162261467 % n == 0;
#endif
}

View File

@@ -1,13 +1,44 @@
//
// Created by aurora on 2024/9/16.
//
#include <assert.h>
#include <solution/338.h>
#include <stdlib.h>
#define ASSERT(x, expect) \
do \
{ \
int retSize; \
int *ret; \
ret = countBits(x, &retSize); \
for (int i = 0; i < retSize; i++) \
assert(ret[i] == expect[i]); \
free(ret); \
} while (0)
int *countBits(int n, int *returnSize)
{
return 0;
*returnSize = n + 1;
int *ret = (int *)malloc(sizeof(int) * (*returnSize));
ret[0] = 0;
for (int i = 1; i <= n; i++)
{
ret[i] = ret[i >> 1] + (i & 1);
// equal to the following expression
// if (i % 2 == 0)
// ret[i] = ret[i / 2];
// else
// ret[i] = ret[i - 1] + 1;
}
return ret;
}
int main() {
int main()
{
int t1[] = {0, 1, 1};
ASSERT(2, t1);
int t2[] = {0, 1, 1, 2, 1, 2};
ASSERT(5, t2);
return 0;
}
}

41
src/3397.c Normal file
View File

@@ -0,0 +1,41 @@
// This file is generated by mkfile.py
// Date: 2025-10-18
#include <solution/3397.h>
#include <stdlib.h>
int cmp(const void *a, const void *b)
{
return *(int *)a - *(int *)b;
}
int maxDistinctElements(int *nums, int numsSize, int k)
{
if (numsSize == 0)
return 0;
qsort(nums, numsSize, sizeof(int), cmp);
int result = 1;
int prev = nums[0] - k;
for (int i = 1; i < numsSize; i++)
{
int min = nums[i] - k;
int max = nums[i] + k;
if (prev >= min)
{
if (max <= prev)
{
continue;
}
prev += 1;
}
else
{
prev = min;
}
result++;
}
return result;
}

View File

@@ -12,4 +12,4 @@ int subarraySum(int *nums, int numsSize)
for (int j = max(0, i - nums[i]); j <= i; j++)
sum += nums[j];
return sum;
}
}

32
src/345.c Normal file
View File

@@ -0,0 +1,32 @@
#include <solution/345.h>
#include <string.h>
#define equal(x) \
((x) == 'a' || (x) == 'A' || (x) == 'e' || (x) == 'E' || (x) == 'i' || (x) == 'I' || (x) == 'o' || (x) == 'O' || \
(x) == 'u' || (x) == 'U')
#define swap(x, y, cb) \
do \
{ \
char t = x; \
x = y; \
y = t; \
cb; \
} while (0)
char *reverseVowels(char *s)
{
for (int i = 0, j = strlen(s) - 1, f = 1; i < j;)
{
if (f && equal(s[i]))
f = 0;
else if (f)
i++;
else if (!f && equal(s[j]))
swap(s[i], s[j], (f = 1, i++, j--));
else
j--;
}
return s;
}

View File

@@ -13,4 +13,4 @@ bool isPerfectSquare(int num)
if (i * i == num)
return true;
}
}
}

View File

@@ -49,4 +49,4 @@ int main()
fail:
return -1;
}
}

17
src/387.c Normal file
View File

@@ -0,0 +1,17 @@
#include <solution/387.h>
#include <string.h>
int firstUniqChar(char *s)
{
int hash[26];
memset(hash, 0, sizeof(int) * 26);
for (int idx = 0; s[idx]; idx++)
hash[s[idx] - 'a']++;
for (int idx = 0; s[idx]; idx++)
if (hash[s[idx] - 'a'] == 1)
return idx;
return -1;
}

10
src/392.c Normal file
View File

@@ -0,0 +1,10 @@
#include <solution/392.h>
bool isSubsequence(char *s, char *t)
{
for (; *s && *t; t++)
if (*s == *t)
s++;
return !*s;
}

14
src/509.c Normal file
View File

@@ -0,0 +1,14 @@
// This file is generated by mkfile.py
// Date: 2025-12-05
#include <solution/509.h>
int fib(int n)
{
int dp[n + 1];
dp[0] = 0;
if (n >= 1)
dp[1] = 1;
for (int i = 2; i <= n; i++)
dp[i] = dp[i - 1] + dp[i - 2];
return dp[n];
}

15
src/746.c Normal file
View File

@@ -0,0 +1,15 @@
// This file is generated by mkfile.py
// Date: 2025-12-05
#include <solution/746.h>
#define min(x, y) ((x) < (y) ? (x) : (y))
int minCostClimbingStairs(int *cost, int costSize)
{
int dp[costSize + 1];
dp[0] = dp[1] = 0;
for (int i = 2; i <= costSize; i++)
dp[i] = min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
return dp[costSize];
}

27
src/976.c Normal file
View File

@@ -0,0 +1,27 @@
#include <solution/976.h>
#include <stdlib.h>
int cmp(const void *a, const void *b)
{
return *(const int *)b - *(const int *)a;
}
int largestPerimeter(int *nums, int numsSize)
{
qsort(nums, numsSize, sizeof(int), cmp);
int i = 0, j = 1;
while (i < numsSize - 2 && j < numsSize - 1)
{
int k = j + 1;
if (nums[i] < nums[j] + nums[k])
return nums[i] + nums[j] + nums[k];
else
{
j++;
i++;
}
}
return 0;
}

View File

@@ -34,4 +34,4 @@ struct TreeNode *expandBinaryTree(struct TreeNode *root)
{
inorder(root);
return root;
}
}

View File

@@ -9,6 +9,4 @@ foreach (TEST_SRC ${TEST_SOURCES})
target_link_libraries(${TEST_NAME} PUBLIC ${TEST_LIBS})
add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME})
endforeach ()
enable_testing()
endforeach ()

45
tests/test_1025.cpp Normal file
View File

@@ -0,0 +1,45 @@
// This file is generated by mkfile.py
// Date: 2025-12-05
#include <gtest/gtest.h>
#include <solution/1025.h>
// Tests for divisorGame (LeetCode 1025)
class DivisorGameTest : public ::testing::Test
{
protected:
void AssertDivisorGame(int n, bool expected)
{
ASSERT_EQ(divisorGame(n), expected);
}
};
TEST_F(DivisorGameTest, One)
{
AssertDivisorGame(1, false);
}
TEST_F(DivisorGameTest, Two)
{
AssertDivisorGame(2, true);
}
TEST_F(DivisorGameTest, Three)
{
AssertDivisorGame(3, false);
}
TEST_F(DivisorGameTest, Four)
{
AssertDivisorGame(4, true);
}
TEST_F(DivisorGameTest, LargeEven)
{
AssertDivisorGame(100, true);
}
TEST_F(DivisorGameTest, LargeOdd)
{
AssertDivisorGame(101, false);
}

45
tests/test_1039.cpp Normal file
View File

@@ -0,0 +1,45 @@
// This file is generated by mkfile.py
// Date: 2025-09-29
#include <gtest/gtest.h>
#include <solution/1039.h>
class MinScoreTriangulationTest : public ::testing::Test
{
protected:
void AssertMinScoreTriangulation(std::vector<int> input, int expected)
{
int *values = new int[input.size()];
std::copy(input.begin(), input.end(), values);
int result = minScoreTriangulation(values, input.size());
ASSERT_EQ(result, expected);
delete[] values;
}
};
// 示例 1
TEST_F(MinScoreTriangulationTest, Example1)
{
// 输入: values = [1,2,3]
// 输出: 6
AssertMinScoreTriangulation({1, 2, 3}, 6);
}
// 示例 2
TEST_F(MinScoreTriangulationTest, Example2)
{
// 输入: values = [3,7,4,5]
// 输出: 144
AssertMinScoreTriangulation({3, 7, 4, 5}, 144);
}
// 示例 3
TEST_F(MinScoreTriangulationTest, Example3)
{
// 输入: values = [1,3,1,4,1,5]
// 输出: 13
AssertMinScoreTriangulation({1, 3, 1, 4, 1, 5}, 13);
}

41
tests/test_1137.cpp Normal file
View File

@@ -0,0 +1,41 @@
// This file is generated by mkfile.py
// Date: 2025-12-05
#include <gtest/gtest.h>
#include <solution/1137.h>
// Tests for tribonacci (LeetCode 1137)
class TribonacciTest : public ::testing::Test
{
protected:
void AssertTribo(int n, int expected)
{
ASSERT_EQ(tribonacci(n), expected);
}
};
TEST_F(TribonacciTest, BaseCases)
{
AssertTribo(0, 0);
AssertTribo(1, 1);
AssertTribo(2, 1);
}
TEST_F(TribonacciTest, SmallNumbers)
{
AssertTribo(3, 2);
AssertTribo(4, 4);
AssertTribo(5, 7);
}
TEST_F(TribonacciTest, MediumNumbers)
{
AssertTribo(10, 149);
AssertTribo(15, 3136);
}
TEST_F(TribonacciTest, UpperBound)
{
AssertTribo(25, 1389537);
AssertTribo(37, 2082876103);
}

52
tests/test_1233.cpp Normal file
View File

@@ -0,0 +1,52 @@
#include <algorithm>
#include <gtest/gtest.h>
#include <solution/1233.h>
#include <string>
#include <vector>
bool ArrayEquals(char **actual, int actualSize, const std::vector<std::string> &expected)
{
if (actualSize != expected.size())
return false;
std::vector<std::string> actualVec(actual, actual + actualSize);
return std::is_permutation(actualVec.begin(), actualVec.end(), expected.begin());
}
// 测试类
class RemoveSubfoldersTest : public ::testing::Test
{
protected:
void AssertFolders(std::vector<std::string> input, std::vector<std::string> expected)
{
int returnSize = 0;
std::vector<char *> c_input;
for (auto &s : input)
c_input.push_back(const_cast<char *>(s.c_str()));
char **result = removeSubfolders(c_input.data(), (int)c_input.size(), &returnSize);
ASSERT_TRUE(ArrayEquals(result, returnSize, expected));
// 可选释放 result 内存如果实现中是动态分配的free
// 例如for (int i = 0; i < returnSize; ++i) free(result[i]);
// free(result);
}
};
// 示例 1
TEST_F(RemoveSubfoldersTest, Example1)
{
AssertFolders({"/a", "/a/b", "/c/d", "/c/d/e", "/c/f"}, {"/a", "/c/d", "/c/f"});
}
// 示例 2
TEST_F(RemoveSubfoldersTest, Example2)
{
AssertFolders({"/a", "/a/b/c", "/a/b/d"}, {"/a"});
}
// 示例 3
TEST_F(RemoveSubfoldersTest, Example3)
{
AssertFolders({"/a/b/c", "/a/b/ca", "/a/b/d"}, {"/a/b/c", "/a/b/ca", "/a/b/d"});
}

49
tests/test_1668.cpp Normal file
View File

@@ -0,0 +1,49 @@
// This file is generated by mkfile.py
// Date: 2025-12-05
#include <gtest/gtest.h>
#include <solution/1668.h>
#include <string>
// Tests for maxRepeating (LeetCode 1668)
class MaxRepeatingTest : public ::testing::Test
{
protected:
void AssertMaxRepeating(const std::string &sequence, const std::string &word, int expected)
{
int result = maxRepeating(const_cast<char *>(sequence.c_str()), const_cast<char *>(word.c_str()));
ASSERT_EQ(result, expected);
}
};
TEST_F(MaxRepeatingTest, Example1)
{
AssertMaxRepeating("ababc", "ab", 2);
}
TEST_F(MaxRepeatingTest, Example2)
{
AssertMaxRepeating("ababc", "ba", 1);
}
TEST_F(MaxRepeatingTest, RepeatedMany)
{
AssertMaxRepeating("abababab", "ab", 4);
}
TEST_F(MaxRepeatingTest, OverlappingNotAllowed)
{
// word = "aa", repeated twice is "aaaa" which appears, so answer 2
AssertMaxRepeating("aaaaa", "aa", 2);
}
TEST_F(MaxRepeatingTest, ShorterThanWord)
{
AssertMaxRepeating("a", "aa", 0);
}
TEST_F(MaxRepeatingTest, SingleCharWord)
{
AssertMaxRepeating("aaaa", "a", 4);
}

30
tests/test_1717.cpp Normal file
View File

@@ -0,0 +1,30 @@
#include <gtest/gtest.h>
#include <solution/1717.h>
class MaximumGainTest : public ::testing::Test
{
protected:
void AssertMaximumGain(const std::string &input, int x, int y, int expected)
{
char *s = strdup(input.c_str()); // 复制字符串,防止修改原始数据
int result = maximumGain(s, x, y);
ASSERT_EQ(result, expected);
free(s);
}
};
// 示例 1
TEST_F(MaximumGainTest, Example1)
{
// 输入s = "cdbcbbaaabab", x = 4, y = 5
// 输出19
AssertMaximumGain("cdbcbbaaabab", 4, 5, 19);
}
// 示例 2
TEST_F(MaximumGainTest, Example2)
{
// 输入s = "aabbaaxybbaabb", x = 5, y = 4
// 输出20
AssertMaximumGain("aabbaaxybbaabb", 5, 4, 20);
}

37
tests/test_1957.cpp Normal file
View File

@@ -0,0 +1,37 @@
#include <gtest/gtest.h>
#include <solution/1957.h>
#include <string>
class MakeFancyStringTest : public ::testing::Test
{
protected:
void AssertFancyString(const std::string &input, const std::string &expected)
{
char *c_input = strdup(input.c_str()); // 复制一份以防原始修改
char *result = makeFancyString(c_input);
ASSERT_STREQ(result, expected.c_str());
// 如函数返回的是 malloc 分配的,则应释放
// free(result);
free(c_input);
}
};
// 示例 1leeetcode → leetcode
TEST_F(MakeFancyStringTest, Example1)
{
AssertFancyString("leeetcode", "leetcode");
}
// 示例 2aaabaaaa → aabaa
TEST_F(MakeFancyStringTest, Example2)
{
AssertFancyString("aaabaaaa", "aabaa");
}
// 示例 3aab → aab
TEST_F(MakeFancyStringTest, Example3)
{
AssertFancyString("aab", "aab");
}

26
tests/test_2163.cpp Normal file
View File

@@ -0,0 +1,26 @@
#include <gtest/gtest.h>
#include <solution/2163.h>
class MinimumDifferenceTest : public ::testing::Test
{
protected:
void AssertMinimumDifference(std::vector<int> input, long long expected)
{
long long result = minimumDifference(input.data(), static_cast<int>(input.size()));
EXPECT_EQ(result, expected);
}
};
// 示例 1
TEST_F(MinimumDifferenceTest, Example1)
{
std::vector<int> nums = {3, 1, 2};
AssertMinimumDifference(nums, -1);
}
// 示例 2
TEST_F(MinimumDifferenceTest, Example2)
{
std::vector<int> nums = {7, 9, 5, 8, 1, 3};
AssertMinimumDifference(nums, 1);
}

35
tests/test_3136.cpp Normal file
View File

@@ -0,0 +1,35 @@
#include <gtest/gtest.h>
#include <solution/3136.h>
// 测试类定义
class IsValidTest : public ::testing::Test
{
};
// 示例 1输入 "234Adas",输出 true
TEST_F(IsValidTest, ValidWord)
{
char word[] = "234Adas";
EXPECT_TRUE(isValid(word));
}
// 示例 2输入 "b3",输出 false长度 < 3 且无元音)
TEST_F(IsValidTest, TooShortNoVowel)
{
char word[] = "b3";
EXPECT_FALSE(isValid(word));
}
// 示例 3输入 "a3$e",输出 false包含非法字符 $ 且无辅音)
TEST_F(IsValidTest, HasSymbolNoConsonant)
{
char word[] = "a3$e";
EXPECT_FALSE(isValid(word));
}
// 示例 3输入 "3pp",输出 false无元音
TEST_F(IsValidTest, NoConsonant)
{
char word[] = "3pp";
EXPECT_FALSE(isValid(word));
}

38
tests/test_3201.cpp Normal file
View File

@@ -0,0 +1,38 @@
#include <gtest/gtest.h>
#include <solution/3201.h>
class MaximumLengthTest : public ::testing::Test
{
protected:
// 提供统一断言接口
void AssertMaximumLength(const std::vector<int> &input, int expected)
{
int *arr = const_cast<int *>(input.data());
int result = maximumLength(arr, input.size());
EXPECT_EQ(result, expected);
}
};
// 示例 1输入 [1,2,3,4],输出 4
TEST_F(MaximumLengthTest, IncreasingSequence)
{
AssertMaximumLength({1, 2, 3, 4}, 4);
}
// 示例 2输入 [1,2,1,1,2,1,2],输出 6
TEST_F(MaximumLengthTest, AlternatingSequence)
{
AssertMaximumLength({1, 2, 1, 1, 2, 1, 2}, 6);
}
// 示例 3输入 [1,3],输出 2
TEST_F(MaximumLengthTest, ShortSequence)
{
AssertMaximumLength({1, 3}, 2);
}
// 示例 4输入 [4,51,68],输出 3
TEST_F(MaximumLengthTest, TEST4)
{
AssertMaximumLength({4, 51, 68}, 3);
}

25
tests/test_3202.cpp Normal file
View File

@@ -0,0 +1,25 @@
#include <gtest/gtest.h>
#include <solution/3202.h>
class MaximumLengthWithKTest : public ::testing::Test
{
protected:
void AssertMaximumLength(const std::vector<int> &nums, int k, int expected)
{
int *arr = const_cast<int *>(nums.data());
int result = maximumLength(arr, nums.size(), k);
EXPECT_EQ(result, expected);
}
};
// 示例 1nums = [1,2,3,4,5], k = 2 => 输出 5
TEST_F(MaximumLengthWithKTest, AllIncreasing)
{
AssertMaximumLength({1, 2, 3, 4, 5}, 2, 5);
}
// 示例 2nums = [1,4,2,3,1,4], k = 3 => 输出 4
TEST_F(MaximumLengthWithKTest, AlternatingValidPairs)
{
AssertMaximumLength({1, 4, 2, 3, 1, 4}, 3, 4);
}

35
tests/test_326.cpp Normal file
View File

@@ -0,0 +1,35 @@
#include <climits>
#include <gtest/gtest.h>
#include <solution/326.h>
class IsPowerOfThreeTest : public ::testing::Test
{
protected:
void AssertIsPowerOfThree(int input, bool expected)
{
EXPECT_EQ(isPowerOfThree(input), expected);
}
};
// 示例测试用例
TEST_F(IsPowerOfThreeTest, ExampleTrueCases)
{
AssertIsPowerOfThree(27, true);
AssertIsPowerOfThree(9, true);
AssertIsPowerOfThree(1, true); // 3^0 = 1
AssertIsPowerOfThree(3, true);
}
TEST_F(IsPowerOfThreeTest, ExampleFalseCases)
{
AssertIsPowerOfThree(0, false);
AssertIsPowerOfThree(45, false);
AssertIsPowerOfThree(-3, false);
AssertIsPowerOfThree(10, false);
}
TEST_F(IsPowerOfThreeTest, EdgeCases)
{
AssertIsPowerOfThree(INT_MIN, false);
AssertIsPowerOfThree(INT_MAX, false); // INT_MAX = 2^31 - 1不是3的幂
}

32
tests/test_3397.cpp Normal file
View File

@@ -0,0 +1,32 @@
// This file is generated by mkfile.py
// Date: 2025-10-18
#include <solution/3397.h>
#include <gtest/gtest.h>
class MaxDistinctElementsTest : public ::testing::Test {
protected:
void AssertMaxDistinctElements(std::vector<int> nums, int k, int expected) {
int* arr = new int[nums.size()];
std::copy(nums.begin(), nums.end(), arr);
int result = maxDistinctElements(arr, nums.size(), k);
ASSERT_EQ(result, expected);
delete[] arr;
}
};
// 示例 1
TEST_F(MaxDistinctElementsTest, Example1) {
// 输入nums = [1,2,2,3,3,4], k = 2
// 输出6
AssertMaxDistinctElements({1, 2, 2, 3, 3, 4}, 2, 6);
}
// 示例 2
TEST_F(MaxDistinctElementsTest, Example2) {
// 输入nums = [4,4,4,4], k = 1
// 输出3
AssertMaxDistinctElements({4, 4, 4, 4}, 1, 3);
}

29
tests/test_345.cpp Normal file
View File

@@ -0,0 +1,29 @@
#include <gtest/gtest.h>
#include <solution/345.h>
class ReverseVowelsTest : public ::testing::Test
{
protected:
// 测试包装器
void AssertReverseVowels(const std::string &input, const std::string &expected)
{
char buffer[1024];
strncpy(buffer, input.c_str(), sizeof(buffer));
buffer[sizeof(buffer) - 1] = '\0'; // 保证结尾
char *result = reverseVowels(buffer);
EXPECT_STREQ(result, expected.c_str());
}
};
// 示例 1
TEST_F(ReverseVowelsTest, Example1)
{
AssertReverseVowels("IceCreAm", "AceCreIm");
}
// 示例 2
TEST_F(ReverseVowelsTest, Example2)
{
AssertReverseVowels("leetcode", "leotcede");
}

51
tests/test_387.cpp Normal file
View File

@@ -0,0 +1,51 @@
#include <gtest/gtest.h>
#include <solution/387.h>
class FirstUniqCharTest : public ::testing::Test
{
protected:
void AssertFirstUniqChar(const std::string &input, int expected)
{
char buf[1001];
strncpy(buf, input.c_str(), sizeof(buf));
buf[1000] = '\0'; // 防止越界
int result = firstUniqChar(buf);
EXPECT_EQ(result, expected);
}
};
// 示例 1leetcode → 'l' 是第一个唯一字符,索引 0
TEST_F(FirstUniqCharTest, Example1)
{
AssertFirstUniqChar("leetcode", 0);
}
// 示例 2loveleetcode → 'v' 是第一个唯一字符,索引 2
TEST_F(FirstUniqCharTest, Example2)
{
AssertFirstUniqChar("loveleetcode", 2);
}
// 示例 3aabb → 没有唯一字符,返回 -1
TEST_F(FirstUniqCharTest, Example3)
{
AssertFirstUniqChar("aabb", -1);
}
// 边界测试:空字符串
TEST_F(FirstUniqCharTest, EmptyString)
{
AssertFirstUniqChar("", -1);
}
// 边界测试:单字符
TEST_F(FirstUniqCharTest, SingleChar)
{
AssertFirstUniqChar("z", 0);
}
// 更多测试:末尾唯一字符
TEST_F(FirstUniqCharTest, UniqueAtEnd)
{
AssertFirstUniqChar("aabbc", 4); // 'c' 是唯一字符
}

66
tests/test_392.cpp Normal file
View File

@@ -0,0 +1,66 @@
#include <gtest/gtest.h>
#include <solution/392.h>
class IsSubsequenceTest : public ::testing::Test
{
protected:
void AssertIsSubsequence(const std::string &s, const std::string &t, bool expected)
{
char s_buf[1001], t_buf[1001];
strncpy(s_buf, s.c_str(), sizeof(s_buf));
s_buf[1000] = '\0';
strncpy(t_buf, t.c_str(), sizeof(t_buf));
t_buf[1000] = '\0';
bool result = isSubsequence(s_buf, t_buf);
EXPECT_EQ(result, expected);
}
};
// 示例 1s = "abc", t = "ahbgdc" → true
TEST_F(IsSubsequenceTest, Example1)
{
AssertIsSubsequence("abc", "ahbgdc", true);
}
// 示例 2s = "axc", t = "ahbgdc" → false
TEST_F(IsSubsequenceTest, Example2)
{
AssertIsSubsequence("axc", "ahbgdc", false);
}
// 边界测试s 为空 → 总是 true
TEST_F(IsSubsequenceTest, EmptyS)
{
AssertIsSubsequence("", "anystring", true);
}
// 边界测试t 为空 → 只有 s 也为空才为 true
TEST_F(IsSubsequenceTest, EmptyT)
{
AssertIsSubsequence("a", "", false);
AssertIsSubsequence("", "", true);
}
// s 比 t 长 → false
TEST_F(IsSubsequenceTest, SLongerThanT)
{
AssertIsSubsequence("abcdefg", "abc", false);
}
// s 与 t 完全相同 → true
TEST_F(IsSubsequenceTest, SameString)
{
AssertIsSubsequence("abcde", "abcde", true);
}
// 非连续但顺序正确 → true
TEST_F(IsSubsequenceTest, SparseSubsequence)
{
AssertIsSubsequence("ace", "abcde", true);
}
// 非连续顺序错误 → false
TEST_F(IsSubsequenceTest, WrongOrder)
{
AssertIsSubsequence("aec", "abcde", false);
}

39
tests/test_509.cpp Normal file
View File

@@ -0,0 +1,39 @@
// This file is generated by mkfile.py
// Date: 2025-12-05
#include <gtest/gtest.h>
#include <solution/509.h>
// Tests for fib (LeetCode 509)
class FibTest : public ::testing::Test
{
protected:
void AssertFib(int n, int expected)
{
ASSERT_EQ(fib(n), expected);
}
};
TEST_F(FibTest, Zero)
{
AssertFib(0, 0);
}
TEST_F(FibTest, One)
{
AssertFib(1, 1);
}
TEST_F(FibTest, Two)
{
AssertFib(2, 1);
}
TEST_F(FibTest, Ten)
{
AssertFib(10, 55);
}
TEST_F(FibTest, Twenty)
{
AssertFib(20, 6765);
}

47
tests/test_746.cpp Normal file
View File

@@ -0,0 +1,47 @@
// This file is generated by mkfile.py
// Date: 2025-12-05
#include <gtest/gtest.h>
#include <solution/746.h>
#include <algorithm>
#include <vector>
// Tests for minCostClimbingStairs (LeetCode 746)
class MinCostClimbingStairsTest : public ::testing::Test
{
protected:
void AssertMinCost(const std::vector<int> &input, int expected)
{
int *cost = new int[input.size()];
std::copy(input.begin(), input.end(), cost);
int result = minCostClimbingStairs(cost, (int)input.size());
ASSERT_EQ(result, expected);
delete[] cost;
}
};
TEST_F(MinCostClimbingStairsTest, Example1)
{
// cost = [10,15,20] -> expected 15
AssertMinCost({10, 15, 20}, 15);
}
TEST_F(MinCostClimbingStairsTest, Example2)
{
// cost = [1,100,1,1,1,100,1,1,100,1] -> expected 6
AssertMinCost({1, 100, 1, 1, 1, 100, 1, 1, 100, 1}, 6);
}
TEST_F(MinCostClimbingStairsTest, TwoSteps)
{
// Two steps: choose cheaper one
AssertMinCost({5, 3}, 3);
}
TEST_F(MinCostClimbingStairsTest, ZeroCost)
{
AssertMinCost({0, 0, 0}, 0);
}

65
tests/test_976.cpp Normal file
View File

@@ -0,0 +1,65 @@
#include <gtest/gtest.h>
#include <solution/976.h>
// 测试类定义
class LargestPerimeterTest : public ::testing::Test
{
protected:
void AssertLargestPerimeter(std::vector<int> input, int expected)
{
int *nums = new int[input.size()];
std::copy(input.begin(), input.end(), nums);
int result = largestPerimeter(nums, input.size());
ASSERT_EQ(result, expected);
delete[] nums;
}
};
// 示例 1
TEST_F(LargestPerimeterTest, Example1)
{
// 输入: nums = [2,1,2]
// 输出: 5
AssertLargestPerimeter({2, 1, 2}, 5);
}
// 示例 2
TEST_F(LargestPerimeterTest, Example2)
{
// 输入: nums = [1,2,1,10]
// 输出: 0
AssertLargestPerimeter({1, 2, 1, 10}, 0);
}
// 边界测试:最小输入
TEST_F(LargestPerimeterTest, SmallestInput)
{
// 输入: nums = [3,4,5]
// 输出: 12
AssertLargestPerimeter({3, 4, 5}, 12);
}
// 边界测试:无法组成三角形的情况
TEST_F(LargestPerimeterTest, CannotFormTriangle)
{
// 输入: nums = [1,1,2]
// 输出: 0
AssertLargestPerimeter({1, 1, 2}, 0);
}
// 边界测试:重复元素
TEST_F(LargestPerimeterTest, RepeatedElements)
{
// 输入: nums = [3,3,3,3,3]
// 输出: 9
AssertLargestPerimeter({3, 3, 3, 3, 3}, 9);
}
// 边界测试:无有效三角形
TEST_F(LargestPerimeterTest, NoValidTriangle)
{
// 输入: nums = [1,1,1,10]
// 输出: 3
AssertLargestPerimeter({1, 1, 1, 10}, 3);
}

View File

@@ -1,8 +1,8 @@
//
// Created by xfj12 on 2025/3/25.
//
#include <climits>
#include <gtest/gtest.h>
#include <queue>
#include <solution/lcp_67.h>
bool areTreesEqual(TreeNode *t1, TreeNode *t2)