Compare commits
27 Commits
f6e998beff
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 6e2206cabe | |||
| 89a41445ef | |||
| cdb5602c47 | |||
| 3559f83475 | |||
| 6f30721f1b | |||
| 9a8260a3b6 | |||
| fc2fd50bac | |||
| aa3f652a8f | |||
| b6a1850f48 | |||
| 737c8e17dd | |||
| 2cf5db11bd | |||
| 7ae0aeb744 | |||
| cf633352b5 | |||
| 45162bb934 | |||
| dc222231e6 | |||
| 8fadb725e1 | |||
| e862aea336 | |||
| e580f911cb | |||
| 727096b8e6 | |||
| 36c5339b1f | |||
| 2e8537eb7f | |||
| 6731384638 | |||
| 91167d0d3b | |||
| fc2d54f926 | |||
| c0a4f0649b | |||
| 050164b61b | |||
| af0fe8a168 |
36
.gitea/workflows/test.yaml
Normal file
36
.gitea/workflows/test.yaml
Normal 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)
|
||||
@@ -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)
|
||||
@@ -32,7 +34,7 @@ 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)
|
||||
@@ -73,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
16
include/solution/1025.h
Normal 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
14
include/solution/1039.h
Normal 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
14
include/solution/1137.h
Normal 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
|
||||
14
include/solution/1668.h
Normal file
14
include/solution/1668.h
Normal 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
|
||||
14
include/solution/3397.h
Normal file
14
include/solution/3397.h
Normal 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
|
||||
14
include/solution/509.h
Normal file
14
include/solution/509.h
Normal 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
14
include/solution/746.h
Normal 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
11
include/solution/976.h
Normal 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
|
||||
49
mkfile.bat
49
mkfile.bat
@@ -1,49 +0,0 @@
|
||||
@echo off
|
||||
chcp 65001
|
||||
setlocal enabledelayedexpansion
|
||||
|
||||
:: 检查参数是否存在
|
||||
if "%~1"=="" (
|
||||
echo 用法: %~nx0 文件名(不带扩展名)
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
set "NAME=%~1"
|
||||
set "NAME_UPPER=%NAME:_=_%"
|
||||
set "NAME_UPPER=%NAME_UPPER:"=%"
|
||||
for %%A in (%NAME_UPPER%) do (
|
||||
set "NAME_UPPER=!NAME_UPPER:%%A=%%~A!"
|
||||
)
|
||||
set "NAME_UPPER=!NAME_UPPER:.=_!"
|
||||
|
||||
:: 创建目录结构
|
||||
mkdir include\solution 2>nul
|
||||
mkdir src 2>nul
|
||||
mkdir tests 2>nul
|
||||
|
||||
:: 生成 include/solution/a.h
|
||||
(
|
||||
echo #ifndef INC_!NAME_UPPER!_H
|
||||
echo #define INC_!NAME_UPPER!_H
|
||||
echo #ifdef __cplusplus
|
||||
echo extern "C" {
|
||||
echo #endif
|
||||
echo.
|
||||
echo #ifdef __cplusplus
|
||||
echo }
|
||||
echo #endif
|
||||
echo #endif
|
||||
) > include\solution\%NAME%.h
|
||||
|
||||
:: 生成 src/a.c
|
||||
(
|
||||
echo #include ^<solution/%NAME%.h^>
|
||||
) > src\%NAME%.c
|
||||
|
||||
:: 生成 tests/test_a.cpp
|
||||
(
|
||||
echo #include ^<gtest/gtest.h^>
|
||||
echo #include ^<solution/%NAME%.h^>
|
||||
) > tests\test_%NAME%.cpp
|
||||
|
||||
echo 文件创建完成。
|
||||
41
mkfile.py
Executable file
41
mkfile.py
Executable 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()
|
||||
39
mkfile.sh
39
mkfile.sh
@@ -1,39 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
# 检查参数
|
||||
if [ $# -ne 1 ]; then
|
||||
echo "用法: $0 <文件名(不带扩展名)>"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
filename="$1"
|
||||
upper_filename=$(echo "$filename" | tr '[:lower:]' '[:upper:]')
|
||||
|
||||
# 创建目录(如果不存在)
|
||||
mkdir -p ./include/solution ./src ./tests
|
||||
|
||||
# 创建 ./include/solution/a.h
|
||||
cat > ./include/solution/"$filename".h <<EOF
|
||||
#ifndef INC_${upper_filename}_H
|
||||
#define INC_${upper_filename}_H
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
EOF
|
||||
|
||||
# 创建 ./src/a.c
|
||||
cat > ./src/"$filename".c <<EOF
|
||||
#include <solution/$filename.h>
|
||||
EOF
|
||||
|
||||
# 创建 ./tests/test_a.cpp
|
||||
cat > ./tests/test_"$filename".cpp <<EOF
|
||||
#include <gtest/gtest.h>
|
||||
#include <solution/$filename.h>
|
||||
EOF
|
||||
7
src/1025.c
Normal file
7
src/1025.c
Normal 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
30
src/1039.c
Normal 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
17
src/1137.c
Normal 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];
|
||||
}
|
||||
38
src/1668.c
Normal file
38
src/1668.c
Normal 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;
|
||||
}
|
||||
37
src/338.c
37
src/338.c
@@ -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
41
src/3397.c
Normal 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;
|
||||
}
|
||||
14
src/509.c
Normal file
14
src/509.c
Normal 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
15
src/746.c
Normal 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
27
src/976.c
Normal 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;
|
||||
}
|
||||
@@ -10,5 +10,3 @@ foreach (TEST_SRC ${TEST_SOURCES})
|
||||
|
||||
add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME})
|
||||
endforeach ()
|
||||
|
||||
enable_testing()
|
||||
45
tests/test_1025.cpp
Normal file
45
tests/test_1025.cpp
Normal 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
45
tests/test_1039.cpp
Normal 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
41
tests/test_1137.cpp
Normal 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);
|
||||
}
|
||||
49
tests/test_1668.cpp
Normal file
49
tests/test_1668.cpp
Normal 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);
|
||||
}
|
||||
32
tests/test_3397.cpp
Normal file
32
tests/test_3397.cpp
Normal 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);
|
||||
}
|
||||
39
tests/test_509.cpp
Normal file
39
tests/test_509.cpp
Normal 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
47
tests/test_746.cpp
Normal 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
65
tests/test_976.cpp
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user