Compare commits
16 Commits
e862aea336
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 6e2206cabe | |||
| 89a41445ef | |||
| cdb5602c47 | |||
| 3559f83475 | |||
| 6f30721f1b | |||
| 9a8260a3b6 | |||
| fc2fd50bac | |||
| aa3f652a8f | |||
| b6a1850f48 | |||
| 737c8e17dd | |||
| 2cf5db11bd | |||
| 7ae0aeb744 | |||
| cf633352b5 | |||
| 45162bb934 | |||
| dc222231e6 | |||
| 8fadb725e1 |
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,4 +1,4 @@
|
||||
cmake_minimum_required(VERSION 3.30)
|
||||
cmake_minimum_required(VERSION 3.25)
|
||||
project(leetcode C CXX)
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
@@ -7,6 +7,10 @@ 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)
|
||||
|
||||
include(FetchContent)
|
||||
@@ -14,9 +18,6 @@ include(FetchContent)
|
||||
add_compile_options(
|
||||
-Wall
|
||||
-Wextra
|
||||
-O0
|
||||
-g3
|
||||
-fno-omit-frame-pointer
|
||||
)
|
||||
|
||||
if (UNIX)
|
||||
|
||||
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/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
|
||||
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];
|
||||
}
|
||||
@@ -14,19 +14,20 @@ int maxRepeating(char *sequence, char *word)
|
||||
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] != word[j])
|
||||
if (sequence[i - m + 1 + j] != word[j])
|
||||
{
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (valid)
|
||||
dp[i] = (i == m - 1 ? 0 : dp[i - m] + 1);
|
||||
dp[i] = (i == m - 1 ? 0 : dp[i - m]) + 1;
|
||||
}
|
||||
|
||||
int ret = dp[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;
|
||||
}
|
||||
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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
Reference in New Issue
Block a user