Compare commits
70 Commits
1e9de7c9c2
...
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 | |||
| f6e998beff | |||
| 5f7ef7b880 | |||
| b9a758a025 | |||
| cb718130f7 | |||
| f54c320ebf | |||
| 6df3e30818 | |||
| 2a7d5ed0cc | |||
| 882ac55f7f | |||
| bb4071ea04 | |||
| 4b5d8c5c22 | |||
| e924811683 | |||
| e3b069be8c | |||
| 084c5f7e11 | |||
| 33f3b72bfa | |||
| 3fcbb0e5ad | |||
| 72843d1812 | |||
| ec59538a98 | |||
| ed2072aef8 | |||
| 7325a22989 | |||
| 321cb6c439 | |||
| 98b0294f80 | |||
| 58a56e3926 | |||
| 44e5b938c9 | |||
| 3d6aa12717 | |||
| 5f09521d43 | |||
| c69a6b5ca9 | |||
| 572479248d | |||
| 93f367dee9 | |||
| a09362480a | |||
| a89bec24e9 | |||
| cbd9bd31a9 | |||
| 79b6fbe7e3 | |||
| e0f249d1d9 | |||
| c4ad87ecea | |||
| 59f589894b | |||
| aabf07d9ec | |||
| 33a8c8b586 | |||
| 7619dbdaaa | |||
| 5100a4390c | |||
| 209fb31445 | |||
| 2cdaeb0e65 | |||
| 5ffc8c8eb0 | |||
| b1893c001e |
@@ -1 +1 @@
|
|||||||
BasedOnStyle: Microsoft
|
BasedOnStyle: Microsoft
|
||||||
|
|||||||
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,30 +1,93 @@
|
|||||||
cmake_minimum_required(VERSION 3.30)
|
cmake_minimum_required(VERSION 3.25)
|
||||||
project(leetcode C)
|
project(leetcode C CXX)
|
||||||
|
|
||||||
set(CMAKE_C_STANDARD 11)
|
set(CMAKE_C_STANDARD 11)
|
||||||
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
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_directories(include)
|
||||||
|
|
||||||
|
include(FetchContent)
|
||||||
|
|
||||||
add_compile_options(
|
add_compile_options(
|
||||||
-Wall
|
-Wall
|
||||||
-Wextra
|
-Wextra
|
||||||
-O0
|
|
||||||
-g3
|
|
||||||
-fsanitize=address
|
|
||||||
-fsanitize=undefined
|
|
||||||
-fno-omit-frame-pointer
|
|
||||||
)
|
)
|
||||||
|
|
||||||
add_link_options(
|
if (UNIX)
|
||||||
-fsanitize=address
|
add_compile_options(
|
||||||
-fsanitize=undefined
|
-fsanitize=address
|
||||||
|
-fsanitize=undefined
|
||||||
|
)
|
||||||
|
add_link_options(
|
||||||
|
-fsanitize=address
|
||||||
|
-fsanitize=undefined
|
||||||
|
-lm
|
||||||
|
)
|
||||||
|
endif ()
|
||||||
|
|
||||||
|
FetchContent_Declare(
|
||||||
|
googletest
|
||||||
|
URL https://gh-proxy.org/https://github.com/google/googletest/releases/download/v${GTEST_VERSION}/googletest-${GTEST_VERSION}.tar.gz
|
||||||
)
|
)
|
||||||
|
|
||||||
|
FetchContent_MakeAvailable(googletest)
|
||||||
|
|
||||||
file(GLOB SRC_FILES "src/*.c")
|
file(GLOB SRC_FILES "src/*.c")
|
||||||
|
|
||||||
foreach(SRC_FILE ${SRC_FILES})
|
set(EXECUTABLE_SOURCES
|
||||||
get_filename_component(EXE_NAME ${SRC_FILE} NAME_WE)
|
"${CMAKE_SOURCE_DIR}/src/13.c"
|
||||||
add_executable(${EXE_NAME} ${SRC_FILE})
|
"${CMAKE_SOURCE_DIR}/src/88.c"
|
||||||
endforeach()
|
"${CMAKE_SOURCE_DIR}/src/100.c"
|
||||||
|
"${CMAKE_SOURCE_DIR}/src/101.c"
|
||||||
|
"${CMAKE_SOURCE_DIR}/src/104.c"
|
||||||
|
"${CMAKE_SOURCE_DIR}/src/108.c"
|
||||||
|
"${CMAKE_SOURCE_DIR}/src/110.c"
|
||||||
|
"${CMAKE_SOURCE_DIR}/src/111.c"
|
||||||
|
"${CMAKE_SOURCE_DIR}/src/112.c"
|
||||||
|
"${CMAKE_SOURCE_DIR}/src/136.c"
|
||||||
|
"${CMAKE_SOURCE_DIR}/src/141.c"
|
||||||
|
"${CMAKE_SOURCE_DIR}/src/144.c"
|
||||||
|
"${CMAKE_SOURCE_DIR}/src/145.c"
|
||||||
|
"${CMAKE_SOURCE_DIR}/src/160.c"
|
||||||
|
"${CMAKE_SOURCE_DIR}/src/168.c"
|
||||||
|
"${CMAKE_SOURCE_DIR}/src/169.c"
|
||||||
|
"${CMAKE_SOURCE_DIR}/src/171.c"
|
||||||
|
"${CMAKE_SOURCE_DIR}/src/190.c"
|
||||||
|
"${CMAKE_SOURCE_DIR}/src/191.c"
|
||||||
|
"${CMAKE_SOURCE_DIR}/src/202.c"
|
||||||
|
"${CMAKE_SOURCE_DIR}/src/219.c"
|
||||||
|
"${CMAKE_SOURCE_DIR}/src/225.c"
|
||||||
|
"${CMAKE_SOURCE_DIR}/src/228.c"
|
||||||
|
"${CMAKE_SOURCE_DIR}/src/231.c"
|
||||||
|
"${CMAKE_SOURCE_DIR}/src/263.c"
|
||||||
|
"${CMAKE_SOURCE_DIR}/src/268.c"
|
||||||
|
"${CMAKE_SOURCE_DIR}/src/278.c"
|
||||||
|
"${CMAKE_SOURCE_DIR}/src/338.c"
|
||||||
|
"${CMAKE_SOURCE_DIR}/src/374.c"
|
||||||
|
"${CMAKE_SOURCE_DIR}/src/2786.c"
|
||||||
|
"${CMAKE_SOURCE_DIR}/src/3110.c"
|
||||||
|
)
|
||||||
|
|
||||||
|
foreach (src_file IN LISTS EXECUTABLE_SOURCES)
|
||||||
|
get_filename_component(exe_name "${src_file}" NAME_WE)
|
||||||
|
add_executable(${exe_name} ${src_file})
|
||||||
|
endforeach ()
|
||||||
|
|
||||||
|
set(LIBRARY_SOURCES)
|
||||||
|
|
||||||
|
foreach (SRC_FILE ${SRC_FILES})
|
||||||
|
if (NOT SRC_FILE IN_LIST EXECUTABLE_SOURCES)
|
||||||
|
list(APPEND LIBRARY_SOURCES ${SRC_FILE})
|
||||||
|
endif ()
|
||||||
|
endforeach ()
|
||||||
|
|
||||||
|
add_library(leetcode STATIC ${LIBRARY_SOURCES})
|
||||||
|
|
||||||
|
add_subdirectory(tests)
|
||||||
|
|||||||
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
|
||||||
11
include/solution/1233.h
Normal file
11
include/solution/1233.h
Normal 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
|
||||||
12
include/solution/1290.h
Normal file
12
include/solution/1290.h
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
#ifndef INC_1290_H
|
||||||
|
#define INC_1290_H
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
#include <ListNode.h>
|
||||||
|
int getDecimalValue(struct ListNode *head);
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
17
include/solution/1353.h
Normal file
17
include/solution/1353.h
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
//
|
||||||
|
// Created by gint on 2025/7/7.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef INC_1353_H
|
||||||
|
#define INC_1353_H
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int maxEvents(int **events, int eventsSize, int *eventsColSize);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif // INC_1353_H
|
||||||
15
include/solution/1534.h
Normal file
15
include/solution/1534.h
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
//
|
||||||
|
// Created by xfj12 on 2025/4/14.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef INC_1534_H
|
||||||
|
#define INC_1534_H
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
int countGoodTriplets(int *arr, int arrSize, int a, int b, int c);
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif // INC_1534_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
|
||||||
11
include/solution/1717.h
Normal file
11
include/solution/1717.h
Normal 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/1900.h
Normal file
11
include/solution/1900.h
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
#ifndef INC_1900_H
|
||||||
|
#define INC_1900_H
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
int *earliestAndLatest(int n, int firstPlayer, int secondPlayer, int *returnSize);
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
11
include/solution/1957.h
Normal file
11
include/solution/1957.h
Normal 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
11
include/solution/2163.h
Normal 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
|
||||||
36
include/solution/232.h
Normal file
36
include/solution/232.h
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
#ifndef INC_232_H
|
||||||
|
#define INC_232_H
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
int *base;
|
||||||
|
int ptr;
|
||||||
|
int size;
|
||||||
|
} stack;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
stack *s1;
|
||||||
|
stack *s2;
|
||||||
|
} MyQueue;
|
||||||
|
|
||||||
|
MyQueue *myQueueCreate();
|
||||||
|
|
||||||
|
void myQueuePush(MyQueue *obj, int x);
|
||||||
|
|
||||||
|
int myQueuePop(MyQueue *obj);
|
||||||
|
|
||||||
|
int myQueuePeek(MyQueue *obj);
|
||||||
|
|
||||||
|
bool myQueueEmpty(MyQueue *obj);
|
||||||
|
|
||||||
|
void myQueueFree(MyQueue *obj);
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
11
include/solution/2410.h
Normal file
11
include/solution/2410.h
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
#ifndef INC_2410_H
|
||||||
|
#define INC_2410_H
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
int matchPlayersAndTrainers(int *players, int playersSize, int *trainers, int trainersSize);
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
11
include/solution/258.h
Normal file
11
include/solution/258.h
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
#ifndef INC_258_H
|
||||||
|
#define INC_258_H
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
int addDigits(int num);
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
16
include/solution/2711.h
Normal file
16
include/solution/2711.h
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
//
|
||||||
|
// Created by xfj12 on 2025/3/25.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef INC_2711_H
|
||||||
|
#define INC_2711_H
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
int **differenceOfDistinctValues(int **grid, int gridSize, int *gridColSize, int *returnSize,
|
||||||
|
int **returnColumnSizes);
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif // INC_2711_H
|
||||||
15
include/solution/2712.h
Normal file
15
include/solution/2712.h
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
//
|
||||||
|
// Created by xfj12 on 2025/3/27.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef INC_2712_H
|
||||||
|
#define INC_2712_H
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
long long minimumCost(char *s);
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif // INC_2712_H
|
||||||
15
include/solution/2716.h
Normal file
15
include/solution/2716.h
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
//
|
||||||
|
// Created by xfj12 on 2025/3/28.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef INC_2716_H
|
||||||
|
#define INC_2716_H
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
int minimizedStringLength(char *s);
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif // INC_2716_H
|
||||||
15
include/solution/2829.h
Normal file
15
include/solution/2829.h
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
//
|
||||||
|
// Created by xfj12 on 2025/3/26.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef INC_2829_H
|
||||||
|
#define INC_2829_H
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
int minimumSum(int n, int k);
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif // INC_2829_H
|
||||||
12
include/solution/290.h
Normal file
12
include/solution/290.h
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
#ifndef INC_290_H
|
||||||
|
#define INC_290_H
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
#include <stdbool.h>
|
||||||
|
bool wordPattern(char *pattern, char *s);
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
18
include/solution/2974.h
Normal file
18
include/solution/2974.h
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
//
|
||||||
|
// Created by xfj12 on 2025/3/25.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef INC_2974_H
|
||||||
|
#define INC_2974_H
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
/**
|
||||||
|
* Note: The returned array must be malloced, assume caller calls free().
|
||||||
|
*/
|
||||||
|
int *numberGame(int *nums, int numsSize, int *returnSize);
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif // INC_2974_H
|
||||||
21
include/solution/303.h
Normal file
21
include/solution/303.h
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
#ifndef INC_303_H
|
||||||
|
#define INC_303_H
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
int *data;
|
||||||
|
int size;
|
||||||
|
} NumArray;
|
||||||
|
|
||||||
|
NumArray *numArrayCreate(int *nums, int numsSize);
|
||||||
|
|
||||||
|
int numArraySumRange(NumArray *obj, int left, int right);
|
||||||
|
|
||||||
|
void numArrayFree(NumArray *obj);
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
12
include/solution/3136.h
Normal file
12
include/solution/3136.h
Normal 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
|
||||||
15
include/solution/3169.h
Normal file
15
include/solution/3169.h
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
//
|
||||||
|
// Created by gint on 2025/7/11.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef INC_3169_H
|
||||||
|
#define INC_3169_H
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
int countDays(int days, int **meetings, int meetingsSize, int *meetingsColSize);
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif // INC_3169_H
|
||||||
11
include/solution/3201.h
Normal file
11
include/solution/3201.h
Normal 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
11
include/solution/3202.h
Normal 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
12
include/solution/326.h
Normal 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
|
||||||
28
include/solution/338.h
Normal file
28
include/solution/338.h
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
//
|
||||||
|
// Created by aurora on 2024/9/16.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef LEETCODE_338_H
|
||||||
|
#define LEETCODE_338_H
|
||||||
|
// 给你一个整数 n ,对于 0 <= i <= n 中的每个 i ,计算其二进制表示中 1 的个数 ,返回一个长度为 n + 1 的数组 ans
|
||||||
|
// 作为答案。 示例 1: 输入:n = 2 输出:[0,1,1] 解释: 0 --> 0 1 --> 1 2 --> 10
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
// 输入:n = 5
|
||||||
|
// 输出:[0,1,1,2,1,2]
|
||||||
|
// 解释:
|
||||||
|
// 0 --> 0
|
||||||
|
// 1 --> 1
|
||||||
|
// 2 --> 10
|
||||||
|
// 3 --> 11
|
||||||
|
// 4 --> 100
|
||||||
|
// 5 --> 101
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
// 0 <= n <= 105
|
||||||
|
//
|
||||||
|
// 进阶:
|
||||||
|
// 很容易就能实现时间复杂度为 O(n log n) 的解决方案,你可以在线性时间复杂度 O(n) 内用一趟扫描解决此问题吗?
|
||||||
|
// 你能不使用任何内置函数解决此问题吗?(如,C++ 中的 __builtin_popcount )
|
||||||
|
int *countBits(int n, int *returnSize);
|
||||||
|
#endif // LEETCODE_338_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
|
||||||
15
include/solution/3427.h
Normal file
15
include/solution/3427.h
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
//
|
||||||
|
// Created by xfj12 on 2025/3/25.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef INC_3427_H
|
||||||
|
#define INC_3427_H
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
int subarraySum(int *nums, int numsSize);
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif // INC_3427_H
|
||||||
11
include/solution/345.h
Normal file
11
include/solution/345.h
Normal 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
|
||||||
16
include/solution/367.h
Normal file
16
include/solution/367.h
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
//
|
||||||
|
// Created by xfj12 on 2025/3/25.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef INC_367_H
|
||||||
|
#define INC_367_H
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
#include <stdbool.h>
|
||||||
|
bool isPerfectSquare(int num);
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif // INC_367_H
|
||||||
11
include/solution/387.h
Normal file
11
include/solution/387.h
Normal 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
12
include/solution/392.h
Normal 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
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
|
||||||
16
include/solution/lcp_67.h
Normal file
16
include/solution/lcp_67.h
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
//
|
||||||
|
// Created by xfj12 on 2025/3/25.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef LCP_67_H
|
||||||
|
#define LCP_67_H
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
#include <TreeNode.h>
|
||||||
|
struct TreeNode *expandBinaryTree(struct TreeNode *root);
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif // LCP_67_H
|
||||||
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()
|
||||||
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];
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
#include "include/TreeNode.h"
|
#include "TreeNode.h"
|
||||||
|
|
||||||
int max(const int a, const int b)
|
int max(const int a, const int b)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#include "include/TreeNode.h"
|
#include "TreeNode.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
struct TreeNode *sortedArrayToBST(int *nums, int numsSize)
|
struct TreeNode *sortedArrayToBST(int *nums, int numsSize)
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#include "include/TreeNode.h"
|
#include "TreeNode.h"
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#include "include/TreeNode.h"
|
#include "TreeNode.h"
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
int minDepth(struct TreeNode *root)
|
int minDepth(struct TreeNode *root)
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#include "include/TreeNode.h"
|
#include "TreeNode.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
bool in_order(struct TreeNode *root, int target, int sum)
|
bool in_order(struct TreeNode *root, int target, int sum)
|
||||||
|
|||||||
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];
|
||||||
|
}
|
||||||
25
src/119.c
Normal file
25
src/119.c
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Note: The returned array must be malloced, assume caller calls free().
|
||||||
|
*/
|
||||||
|
int *getRow(int rowIndex, int *returnSize)
|
||||||
|
{
|
||||||
|
int *ret = NULL;
|
||||||
|
|
||||||
|
*returnSize = sizeof(int) * rowIndex + 1;
|
||||||
|
ret = (int *)malloc(*returnSize);
|
||||||
|
|
||||||
|
if (rowIndex == 0)
|
||||||
|
ret[0] = 1;
|
||||||
|
else if (rowIndex == 1)
|
||||||
|
ret[0] = ret[1] = 1;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
}
|
||||||
30
src/1233.c
Normal file
30
src/1233.c
Normal 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;
|
||||||
|
}
|
||||||
12
src/1290.c
Normal file
12
src/1290.c
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
#include <solution/1290.h>
|
||||||
|
|
||||||
|
int getDecimalValue(struct ListNode *head)
|
||||||
|
{
|
||||||
|
int x = 0;
|
||||||
|
while (head)
|
||||||
|
{
|
||||||
|
x = (x << 1) + head->val;
|
||||||
|
head = head->next;
|
||||||
|
}
|
||||||
|
return x;
|
||||||
|
}
|
||||||
91
src/1353.c
Normal file
91
src/1353.c
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
//
|
||||||
|
// Created by gint on 2025/7/7.
|
||||||
|
//
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int cmpEvent(const void *a, const void *b)
|
||||||
|
{
|
||||||
|
int *eventA = *(int **)a;
|
||||||
|
int *eventB = *(int **)b;
|
||||||
|
return eventA[0] - eventB[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
int compare(const void *a, const void *b)
|
||||||
|
{
|
||||||
|
return *(int *)a - *(int *)b;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 简易小根堆操作
|
||||||
|
void push(int *heap, int *size, int val)
|
||||||
|
{
|
||||||
|
int i = (*size)++;
|
||||||
|
heap[i] = val;
|
||||||
|
while (i > 0 && heap[(i - 1) / 2] > heap[i])
|
||||||
|
{
|
||||||
|
int tmp = heap[i];
|
||||||
|
heap[i] = heap[(i - 1) / 2];
|
||||||
|
heap[(i - 1) / 2] = tmp;
|
||||||
|
i = (i - 1) / 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int pop(int *heap, int *size)
|
||||||
|
{
|
||||||
|
int result = heap[0];
|
||||||
|
heap[0] = heap[--(*size)];
|
||||||
|
int i = 0;
|
||||||
|
while (i * 2 + 1 < *size)
|
||||||
|
{
|
||||||
|
int j = i * 2 + 1;
|
||||||
|
if (j + 1 < *size && heap[j + 1] < heap[j])
|
||||||
|
j++;
|
||||||
|
if (heap[i] <= heap[j])
|
||||||
|
break;
|
||||||
|
int tmp = heap[i];
|
||||||
|
heap[i] = heap[j];
|
||||||
|
heap[j] = tmp;
|
||||||
|
i = j;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
int maxEvents(int **events, int eventsSize, int *eventsColSize)
|
||||||
|
{
|
||||||
|
qsort(events, eventsSize, sizeof(int *), cmpEvent);
|
||||||
|
|
||||||
|
int minDay = events[0][0], maxDay = 0;
|
||||||
|
for (int i = 0; i < eventsSize; i++)
|
||||||
|
{
|
||||||
|
if (events[i][1] > maxDay)
|
||||||
|
maxDay = events[i][1];
|
||||||
|
}
|
||||||
|
|
||||||
|
int heap[eventsSize];
|
||||||
|
int heapSize = 0;
|
||||||
|
|
||||||
|
int day = minDay, idx = 0, res = 0;
|
||||||
|
|
||||||
|
for (day = minDay; day <= maxDay; day++)
|
||||||
|
{
|
||||||
|
// 把今天开始的会议加进堆
|
||||||
|
while (idx < eventsSize && events[idx][0] == day)
|
||||||
|
{
|
||||||
|
push(heap, &heapSize, events[idx][1]);
|
||||||
|
idx++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 去掉已过期的会议
|
||||||
|
while (heapSize > 0 && heap[0] < day)
|
||||||
|
{
|
||||||
|
pop(heap, &heapSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 选择今天可以开的最早结束的会议
|
||||||
|
if (heapSize > 0)
|
||||||
|
{
|
||||||
|
pop(heap, &heapSize);
|
||||||
|
res++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
#include "include/ListNode.h"
|
#include "ListNode.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
bool hasCycle(struct ListNode *head)
|
bool hasCycle(struct ListNode *head)
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#include "include/TreeNode.h"
|
#include "TreeNode.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
struct list_t
|
struct list_t
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#include "include/TreeNode.h"
|
#include "TreeNode.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
struct list_t
|
struct list_t
|
||||||
|
|||||||
17
src/1534.c
Normal file
17
src/1534.c
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
//
|
||||||
|
// Created by xfj12 on 2025/4/14.
|
||||||
|
//
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <solution/1534.h>
|
||||||
|
int countGoodTriplets(int *arr, int arrSize, int a, int b, int c)
|
||||||
|
{
|
||||||
|
int count = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < arrSize; i++)
|
||||||
|
for (int j = i + 1; j < arrSize; j++)
|
||||||
|
for (int k = j + 1; k < arrSize; k++)
|
||||||
|
if (abs(arr[i] - arr[j]) <= a && abs(arr[j] - arr[k]) <= b && abs(arr[i] - arr[k]) <= c)
|
||||||
|
count++;
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
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;
|
||||||
|
}
|
||||||
38
src/168.c
Normal file
38
src/168.c
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
char *convertToTitle(int columnNumber)
|
||||||
|
{
|
||||||
|
char *ret = malloc(2);
|
||||||
|
int size = 1;
|
||||||
|
|
||||||
|
while (columnNumber > 0)
|
||||||
|
{
|
||||||
|
columnNumber--;
|
||||||
|
const int mod = columnNumber % 26;
|
||||||
|
columnNumber /= 26;
|
||||||
|
ret[size - 1] = 'A' + mod;
|
||||||
|
|
||||||
|
size++;
|
||||||
|
ret = realloc(ret, size + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
ret[size - 1] = '\0';
|
||||||
|
|
||||||
|
for (int i = 0, j = size - 2; i < j; i++, j--)
|
||||||
|
{
|
||||||
|
const char tmp = ret[i];
|
||||||
|
ret[i] = ret[j];
|
||||||
|
ret[j] = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
char *ans = convertToTitle(2147483647);
|
||||||
|
printf("%s\n", ans);
|
||||||
|
free(ans);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
25
src/171.c
Normal file
25
src/171.c
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
#include <assert.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
|
||||||
|
int titleToNumber(char *columnTitle)
|
||||||
|
{
|
||||||
|
int colnum = 0;
|
||||||
|
int csize = strlen(columnTitle);
|
||||||
|
for (int i = 0; i < csize; i++)
|
||||||
|
{
|
||||||
|
colnum += (columnTitle[i] - 'A' + 1) * pow(26, csize - i - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return colnum;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
assert(titleToNumber("A") == 1);
|
||||||
|
assert(titleToNumber("AB") == 28);
|
||||||
|
assert(titleToNumber("ZY") == 701);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
91
src/1717.c
Normal file
91
src/1717.c
Normal 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;
|
||||||
|
}
|
||||||
6
src/1900.c
Normal file
6
src/1900.c
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#include <solution/1900.h>
|
||||||
|
|
||||||
|
int *earliestAndLatest(int n, int firstPlayer, int secondPlayer, int *returnSize)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
13
src/1957.c
Normal file
13
src/1957.c
Normal 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
4
src/2163.c
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
#include <solution/2163.h>
|
||||||
|
long long minimumDifference(int *nums, int numsSize)
|
||||||
|
{
|
||||||
|
}
|
||||||
32
src/219.c
Normal file
32
src/219.c
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
//
|
||||||
|
// Created by xfj12 on 2025/3/24.
|
||||||
|
//
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
bool containsNearbyDuplicate(int *nums, int numsSize, int k)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < numsSize; i++)
|
||||||
|
{
|
||||||
|
for (int j = i; j < numsSize; j++)
|
||||||
|
{
|
||||||
|
if (abs(i - j) > k)
|
||||||
|
continue;
|
||||||
|
if (nums[i] == nums[j] && i != j)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int nums1[] = {1, 2, 3, 1};
|
||||||
|
assert(containsNearbyDuplicate(nums1, sizeof(nums1) / sizeof(int), 3) == true);
|
||||||
|
int nums2[] = {1, 0, 1, 1};
|
||||||
|
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);
|
||||||
|
}
|
||||||
@@ -65,4 +65,4 @@ int main()
|
|||||||
assert(myStackEmpty(s) == false);
|
assert(myStackEmpty(s) == false);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,4 +24,4 @@ int main()
|
|||||||
assert(isPowerOfTwo(2) == true);
|
assert(isPowerOfTwo(2) == true);
|
||||||
assert(isPowerOfTwo(0) == false);
|
assert(isPowerOfTwo(0) == false);
|
||||||
assert(isPowerOfTwo(1) == true);
|
assert(isPowerOfTwo(1) == true);
|
||||||
}
|
}
|
||||||
|
|||||||
72
src/232.c
Normal file
72
src/232.c
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
#include <solution/232.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
MyQueue *myQueueCreate()
|
||||||
|
{
|
||||||
|
MyQueue *q = (MyQueue *)malloc(sizeof(MyQueue));
|
||||||
|
q->s1 = (stack *)malloc(sizeof(stack));
|
||||||
|
q->s2 = (stack *)malloc(sizeof(stack));
|
||||||
|
|
||||||
|
q->s1->size = q->s2->size = 10;
|
||||||
|
q->s1->ptr = q->s2->ptr = 0;
|
||||||
|
|
||||||
|
q->s1->base = (int *)malloc(sizeof(int) * 10);
|
||||||
|
q->s2->base = (int *)malloc(sizeof(int) * 10);
|
||||||
|
|
||||||
|
return q;
|
||||||
|
}
|
||||||
|
|
||||||
|
void myQueuePush(MyQueue *obj, int x)
|
||||||
|
{
|
||||||
|
if (obj->s1->ptr > obj->s1->size)
|
||||||
|
{
|
||||||
|
obj->s1->base = (int *)realloc(obj->s1->base, sizeof(int) * (obj->s1->size + 10));
|
||||||
|
obj->s2->base = (int *)realloc(obj->s2->base, sizeof(int) * (obj->s1->size + 10));
|
||||||
|
obj->s1->size = obj->s2->size = obj->s1->size + 10;
|
||||||
|
}
|
||||||
|
obj->s1->base[(obj->s1->ptr)++] = x;
|
||||||
|
}
|
||||||
|
|
||||||
|
int myQueuePop(MyQueue *obj)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
while (obj->s1->ptr)
|
||||||
|
obj->s2->base[(obj->s2->ptr)++] = obj->s1->base[--(obj->s1->ptr)];
|
||||||
|
|
||||||
|
ret = obj->s2->base[--(obj->s2->ptr)];
|
||||||
|
|
||||||
|
while (obj->s2->ptr)
|
||||||
|
obj->s1->base[(obj->s1->ptr)++] = obj->s2->base[--(obj->s2->ptr)];
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int myQueuePeek(MyQueue *obj)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
while (obj->s1->ptr)
|
||||||
|
obj->s2->base[(obj->s2->ptr)++] = obj->s1->base[--(obj->s1->ptr)];
|
||||||
|
|
||||||
|
ret = obj->s2->base[obj->s2->ptr - 1];
|
||||||
|
|
||||||
|
obj->s1->ptr = obj->s2->ptr;
|
||||||
|
obj->s2->ptr = 0;
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool myQueueEmpty(MyQueue *obj)
|
||||||
|
{
|
||||||
|
return !obj->s1->ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void myQueueFree(MyQueue *obj)
|
||||||
|
{
|
||||||
|
free(obj->s1->base);
|
||||||
|
free(obj->s2->base);
|
||||||
|
free(obj->s1);
|
||||||
|
free(obj->s2);
|
||||||
|
free(obj);
|
||||||
|
}
|
||||||
30
src/2410.c
Normal file
30
src/2410.c
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
#include <solution/2410.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int cmp(const void *a, const void *b)
|
||||||
|
{
|
||||||
|
return *(int *)a - *(int *)b;
|
||||||
|
}
|
||||||
|
|
||||||
|
int matchPlayersAndTrainers(int *players, int playersSize, int *trainers, int trainersSize)
|
||||||
|
{
|
||||||
|
qsort(trainers, trainersSize, sizeof(int), cmp);
|
||||||
|
qsort(players, playersSize, sizeof(int), cmp);
|
||||||
|
|
||||||
|
int match = 0;
|
||||||
|
for (int i = 0, j = 0; i < playersSize && j < trainersSize; i++)
|
||||||
|
{
|
||||||
|
for (; j < trainersSize; j++)
|
||||||
|
{
|
||||||
|
if (trainers[j] >= players[i])
|
||||||
|
{
|
||||||
|
match++;
|
||||||
|
j++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return match;
|
||||||
|
}
|
||||||
16
src/258.c
Normal file
16
src/258.c
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
#include <solution/258.h>
|
||||||
|
|
||||||
|
int addDigits(int num)
|
||||||
|
{
|
||||||
|
do
|
||||||
|
{
|
||||||
|
int n = 0;
|
||||||
|
while (num)
|
||||||
|
{
|
||||||
|
n += num % 10;
|
||||||
|
num /= 10;
|
||||||
|
}
|
||||||
|
num = n;
|
||||||
|
} while (num >= 10);
|
||||||
|
return num;
|
||||||
|
}
|
||||||
@@ -28,4 +28,4 @@ int main()
|
|||||||
assert(isUgly(14) == 0);
|
assert(isUgly(14) == 0);
|
||||||
assert(isUgly(8) == 1);
|
assert(isUgly(8) == 1);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
76
src/2711.c
Normal file
76
src/2711.c
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
#include <assert.h>
|
||||||
|
#include <solution/2711.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int differentElementNum(int *elem, int size)
|
||||||
|
{
|
||||||
|
int sum = 0;
|
||||||
|
int *temp = NULL;
|
||||||
|
for (int i = 0; i < size; i++)
|
||||||
|
{
|
||||||
|
bool flag = true;
|
||||||
|
// check same elem
|
||||||
|
for (int j = 0; j < sum; j++)
|
||||||
|
if (elem[i] == temp[j])
|
||||||
|
flag = false;
|
||||||
|
// if not in temp, add to temp
|
||||||
|
if (flag)
|
||||||
|
{
|
||||||
|
temp = realloc(temp, ++sum * sizeof(int));
|
||||||
|
temp[sum - 1] = elem[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
free(temp);
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return an array of arrays of size *returnSize.
|
||||||
|
* The sizes of the arrays are returned as *returnColumnSizes array.
|
||||||
|
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
|
||||||
|
*/
|
||||||
|
int **differenceOfDistinctValues(int **grid, int gridSize, int *gridColSize, int *returnSize, int **returnColumnSizes)
|
||||||
|
{
|
||||||
|
int **answer = (int **)malloc(sizeof(int *) * gridSize);
|
||||||
|
*returnColumnSizes = (int *)malloc(gridSize * sizeof(int));
|
||||||
|
for (int r = 0; r < gridSize; r++)
|
||||||
|
{
|
||||||
|
answer[r] = (int *)malloc(sizeof(int) * gridColSize[r]);
|
||||||
|
(*returnColumnSizes)[r] = gridColSize[r];
|
||||||
|
for (int c = 0; c < gridColSize[r]; c++)
|
||||||
|
{
|
||||||
|
// calc topLeft array
|
||||||
|
int *topLeftElements = NULL;
|
||||||
|
int topLeftSize = 0;
|
||||||
|
int topLeft = 0;
|
||||||
|
for (int x = r - 1, y = c - 1; x >= 0 && y >= 0; x--, y--)
|
||||||
|
{
|
||||||
|
// add element to array
|
||||||
|
topLeftElements = realloc(topLeftElements, ++topLeftSize * sizeof(int));
|
||||||
|
topLeftElements[topLeftSize - 1] = grid[x][y];
|
||||||
|
}
|
||||||
|
topLeft = differentElementNum(topLeftElements, topLeftSize);
|
||||||
|
|
||||||
|
// calc bottomRight array
|
||||||
|
int *bottomRightElements = NULL;
|
||||||
|
int bottomRightSize = 0;
|
||||||
|
int bottomRight = 0;
|
||||||
|
for (int x = r + 1, y = c + 1; x < gridSize && y < gridColSize[r]; x++, y++)
|
||||||
|
{
|
||||||
|
bottomRightElements = realloc(bottomRightElements, ++bottomRightSize * sizeof(int));
|
||||||
|
bottomRightElements[bottomRightSize - 1] = grid[x][y];
|
||||||
|
}
|
||||||
|
bottomRight = differentElementNum(bottomRightElements, bottomRightSize);
|
||||||
|
|
||||||
|
answer[r][c] = abs(topLeft - bottomRight);
|
||||||
|
free(bottomRightElements);
|
||||||
|
free(topLeftElements);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
*returnSize = gridSize;
|
||||||
|
return answer;
|
||||||
|
}
|
||||||
18
src/2712.c
Normal file
18
src/2712.c
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
//
|
||||||
|
// Created by xfj12 on 2025/3/27.
|
||||||
|
//
|
||||||
|
#include <solution/2712.h>
|
||||||
|
#include <string.h>
|
||||||
|
#define min(a, b) (a < b) ? (a) : (b)
|
||||||
|
|
||||||
|
long long minimumCost(char *s)
|
||||||
|
{
|
||||||
|
int n = strlen(s);
|
||||||
|
long long ans = 0;
|
||||||
|
|
||||||
|
for (int i = 1; i < n; i++)
|
||||||
|
if (s[i] != s[i - 1])
|
||||||
|
ans += min(i, n - i);
|
||||||
|
|
||||||
|
return ans;
|
||||||
|
}
|
||||||
26
src/2716.c
Normal file
26
src/2716.c
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
//
|
||||||
|
// Created by xfj12 on 2025/3/28.
|
||||||
|
//
|
||||||
|
#include <solution/2716.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
int minimizedStringLength(char *s)
|
||||||
|
{
|
||||||
|
int n = strlen(s);
|
||||||
|
int size = 0;
|
||||||
|
char *temp = malloc(n);
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
bool flag = true;
|
||||||
|
for (int j = 0; j < size && flag; j++)
|
||||||
|
if (s[i] == temp[j])
|
||||||
|
flag = false;
|
||||||
|
if (flag)
|
||||||
|
temp[size++] = s[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
free(temp);
|
||||||
|
return size;
|
||||||
|
}
|
||||||
@@ -45,4 +45,4 @@ int main()
|
|||||||
return 0;
|
return 0;
|
||||||
fail:
|
fail:
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|||||||
31
src/2829.c
Normal file
31
src/2829.c
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
//
|
||||||
|
// Created by xfj12 on 2025/3/26.
|
||||||
|
//
|
||||||
|
#include <solution/2829.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int minimumSum(int n, int k)
|
||||||
|
{
|
||||||
|
int *KAvoid = malloc(sizeof(int) * n);
|
||||||
|
int idx = 1;
|
||||||
|
KAvoid[0] = 1;
|
||||||
|
|
||||||
|
int sum = 0;
|
||||||
|
|
||||||
|
for (int i = 2; idx < n; i++)
|
||||||
|
{
|
||||||
|
bool flag = true;
|
||||||
|
for (int j = 0; j < idx && flag; j++)
|
||||||
|
if (KAvoid[j] + i == k)
|
||||||
|
flag = false;
|
||||||
|
if (flag)
|
||||||
|
KAvoid[idx++] = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
sum += KAvoid[i];
|
||||||
|
|
||||||
|
free(KAvoid);
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
73
src/290.c
Normal file
73
src/290.c
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
#include <solution/290.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
char *foundValue(char **d, char k)
|
||||||
|
{
|
||||||
|
int idx = k - 'a';
|
||||||
|
return d[idx];
|
||||||
|
}
|
||||||
|
|
||||||
|
int foundKeyCount(char **d, char *v)
|
||||||
|
{
|
||||||
|
int count = 0;
|
||||||
|
for (int i = 0; i < 26; i++)
|
||||||
|
if (d[i] && strcmp(v, d[i]) == 0)
|
||||||
|
count++;
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
void freeDict(char **d)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < 26; i++)
|
||||||
|
free(d[i]);
|
||||||
|
free(d);
|
||||||
|
}
|
||||||
|
|
||||||
|
int insertValue(char **d, char k, char *v)
|
||||||
|
{
|
||||||
|
int idx = k - 'a';
|
||||||
|
if (d[idx] != NULL)
|
||||||
|
return -1;
|
||||||
|
d[idx] = malloc(strlen(v) + 1);
|
||||||
|
memcpy(d[idx], v, strlen(v) + 1);
|
||||||
|
|
||||||
|
return foundKeyCount(d, v);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool preCheck(char *p, char *s)
|
||||||
|
{
|
||||||
|
int s_count = 0;
|
||||||
|
for (; *s != '\0'; s++)
|
||||||
|
if (*s == ' ')
|
||||||
|
s_count++;
|
||||||
|
return s_count + 1 == strlen(p);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool wordPattern(char *pattern, char *s)
|
||||||
|
{
|
||||||
|
if (!preCheck(pattern, s))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
char **d = calloc(26, sizeof(char *));
|
||||||
|
char *token = strtok(s, " ");
|
||||||
|
|
||||||
|
for (int i = 0; i < strlen(pattern) && token != NULL; i++, token = strtok(NULL, " "))
|
||||||
|
{
|
||||||
|
switch (insertValue(d, pattern[i], token))
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
break;
|
||||||
|
case -1:
|
||||||
|
if (strcmp(token, foundValue(d, pattern[i])) == 0)
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
freeDict(d);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
freeDict(d);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
52
src/2974.c
Normal file
52
src/2974.c
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
//
|
||||||
|
// Created by xfj12 on 2025/3/25.
|
||||||
|
//
|
||||||
|
#include <solution/2974.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int findMinElement(int *nums, int numsSize, int *returnIndex)
|
||||||
|
{
|
||||||
|
int min = nums[0];
|
||||||
|
*returnIndex = 0;
|
||||||
|
for (int i = 1; i < numsSize; ++i)
|
||||||
|
{
|
||||||
|
if (nums[i] < min)
|
||||||
|
{
|
||||||
|
min = nums[i];
|
||||||
|
*returnIndex = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return min;
|
||||||
|
}
|
||||||
|
|
||||||
|
void removeElement(int *nums, int numsSize, int idx)
|
||||||
|
{
|
||||||
|
for (int i = idx; i < numsSize - 1; ++i)
|
||||||
|
nums[i] = nums[i + 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
int *numberGame(int *nums, int numsSize, int *returnSize)
|
||||||
|
{
|
||||||
|
int *result = malloc(sizeof(int) * numsSize);
|
||||||
|
*returnSize = 0;
|
||||||
|
|
||||||
|
while (numsSize > 0)
|
||||||
|
{
|
||||||
|
int aliceIndex;
|
||||||
|
const int alice = findMinElement(nums, numsSize, &aliceIndex);
|
||||||
|
removeElement(nums, numsSize--, aliceIndex);
|
||||||
|
|
||||||
|
int bobIndex;
|
||||||
|
const int bob = findMinElement(nums, numsSize, &bobIndex);
|
||||||
|
removeElement(nums, numsSize--, bobIndex);
|
||||||
|
|
||||||
|
result[(*returnSize)++] = bob;
|
||||||
|
result[(*returnSize)++] = alice;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < numsSize; ++i)
|
||||||
|
printf("%d ", result[i]);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
44
src/303.c
Normal file
44
src/303.c
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
#define PREFIX
|
||||||
|
|
||||||
|
#include <solution/303.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#ifdef PREFIX
|
||||||
|
#else
|
||||||
|
#include <string.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
NumArray *numArrayCreate(int *nums, int numsSize)
|
||||||
|
{
|
||||||
|
NumArray *ret = malloc(sizeof(NumArray));
|
||||||
|
ret->size = numsSize;
|
||||||
|
#ifdef PREFIX
|
||||||
|
ret->data = malloc(sizeof(int) * (numsSize + 1));
|
||||||
|
ret->data[0] = 0;
|
||||||
|
for (int i = 0; i < numsSize; i++)
|
||||||
|
ret->data[i + 1] = ret->data[i] + nums[i];
|
||||||
|
#else
|
||||||
|
ret->data = malloc(sizeof(int) * numsSize);
|
||||||
|
memcpy(ret->data, nums, sizeof(int) * numsSize);
|
||||||
|
#endif
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int numArraySumRange(NumArray *obj, int left, int right)
|
||||||
|
{
|
||||||
|
#ifdef PREFIX
|
||||||
|
return obj->data[right + 1] - obj->data[left];
|
||||||
|
#else
|
||||||
|
int sum = 0;
|
||||||
|
for (int i = left; i <= right; i++)
|
||||||
|
sum += obj->data[i];
|
||||||
|
|
||||||
|
return sum;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void numArrayFree(NumArray *obj)
|
||||||
|
{
|
||||||
|
free(obj->data);
|
||||||
|
free(obj);
|
||||||
|
}
|
||||||
21
src/3110.c
Normal file
21
src/3110.c
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
#include <assert.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
//
|
||||||
|
// Created by xfj12 on 2025/3/24.
|
||||||
|
//
|
||||||
|
|
||||||
|
int scoreOfString(char *s)
|
||||||
|
{
|
||||||
|
int score = 0;
|
||||||
|
for (int i = 0; i < strlen(s) - 1; i++)
|
||||||
|
score += abs(s[i] - s[i + 1]);
|
||||||
|
|
||||||
|
return score;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
assert(scoreOfString("hello") == 13);
|
||||||
|
assert(scoreOfString("zaz") == 50);
|
||||||
|
}
|
||||||
33
src/3136.c
Normal file
33
src/3136.c
Normal 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;
|
||||||
|
}
|
||||||
42
src/3169.c
Normal file
42
src/3169.c
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
//
|
||||||
|
// Created by gint on 2025/7/11.
|
||||||
|
//
|
||||||
|
#include <solution/3169.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
int cmpMeetings(const void *a, const void *b)
|
||||||
|
{
|
||||||
|
int *ma = *(int **)a;
|
||||||
|
int *mb = *(int **)b;
|
||||||
|
if (ma[0] != mb[0])
|
||||||
|
return ma[0] - mb[0];
|
||||||
|
return ma[1] - mb[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
void mergeMeeting(int *meetingA, int *meetingB)
|
||||||
|
{
|
||||||
|
meetingA[0] = meetingA[0] < meetingB[0] ? meetingA[0] : meetingB[0];
|
||||||
|
meetingA[1] = meetingA[1] > meetingB[1] ? meetingA[1] : meetingB[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
int countDays(int days, int **meetings, int meetingsSize, int *meetingsColSize)
|
||||||
|
{
|
||||||
|
int count = 0;
|
||||||
|
qsort(meetings, meetingsSize, sizeof(int *), cmpMeetings);
|
||||||
|
|
||||||
|
for (int i = 1; i < meetingsSize; i++)
|
||||||
|
if (meetings[i - 1][1] >= meetings[i][0])
|
||||||
|
mergeMeeting(meetings[i], meetings[i - 1]);
|
||||||
|
|
||||||
|
for (int i = 1; i < meetingsSize; i++)
|
||||||
|
if (meetings[i - 1][0] != meetings[i][0])
|
||||||
|
count += meetings[i][0] - meetings[i - 1][1] - 1;
|
||||||
|
|
||||||
|
if (meetings[0][0] != 1)
|
||||||
|
count += meetings[0][0] - 1;
|
||||||
|
|
||||||
|
if (meetings[meetingsSize - 1][1] < days)
|
||||||
|
return count + days - meetings[meetingsSize - 1][1];
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
13
src/3201.c
Normal file
13
src/3201.c
Normal 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
5
src/3202.c
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
#include <solution/3202.h>
|
||||||
|
|
||||||
|
int maximumLength(int *nums, int numsSize, int k)
|
||||||
|
{
|
||||||
|
}
|
||||||
23
src/326.c
Normal file
23
src/326.c
Normal 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
|
||||||
|
}
|
||||||
44
src/338.c
Normal file
44
src/338.c
Normal file
@@ -0,0 +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)
|
||||||
|
{
|
||||||
|
*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 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;
|
||||||
|
}
|
||||||
15
src/3427.c
Normal file
15
src/3427.c
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
//
|
||||||
|
// Created by xfj12 on 2025/3/25.
|
||||||
|
//
|
||||||
|
#include <solution/3427.h>
|
||||||
|
|
||||||
|
#define max(a, b) ((a) > (b) ? (a) : (b))
|
||||||
|
|
||||||
|
int subarraySum(int *nums, int numsSize)
|
||||||
|
{
|
||||||
|
int sum = 0;
|
||||||
|
for (int i = 0; i < numsSize; i++)
|
||||||
|
for (int j = max(0, i - nums[i]); j <= i; j++)
|
||||||
|
sum += nums[j];
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
32
src/345.c
Normal file
32
src/345.c
Normal 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;
|
||||||
|
}
|
||||||
16
src/367.c
Normal file
16
src/367.c
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
//
|
||||||
|
// Created by xfj12 on 2025/3/25.
|
||||||
|
//
|
||||||
|
#include <solution/367.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
bool isPerfectSquare(int num)
|
||||||
|
{
|
||||||
|
for (int64_t i = 1;; i++)
|
||||||
|
{
|
||||||
|
if (i * i > num)
|
||||||
|
return false;
|
||||||
|
if (i * i == num)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
17
src/387.c
Normal file
17
src/387.c
Normal 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
10
src/392.c
Normal 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
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;
|
||||||
|
}
|
||||||
37
src/lcp_67.c
Normal file
37
src/lcp_67.c
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
//
|
||||||
|
// Created by xfj12 on 2025/3/25.
|
||||||
|
//
|
||||||
|
#include <solution/lcp_67.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
void inorder(struct TreeNode *root)
|
||||||
|
{
|
||||||
|
if (root == NULL)
|
||||||
|
return;
|
||||||
|
inorder(root->left);
|
||||||
|
if (root->left != NULL && root->val != -1)
|
||||||
|
{
|
||||||
|
struct TreeNode *p = root->left;
|
||||||
|
root->left = malloc(sizeof(struct TreeNode));
|
||||||
|
root->left->left = p;
|
||||||
|
root->left->right = NULL;
|
||||||
|
root->left->val = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (root->right != NULL && root->val != -1)
|
||||||
|
{
|
||||||
|
struct TreeNode *p = root->right;
|
||||||
|
root->right = malloc(sizeof(struct TreeNode));
|
||||||
|
root->right->right = p;
|
||||||
|
root->right->left = NULL;
|
||||||
|
root->right->val = -1;
|
||||||
|
}
|
||||||
|
inorder(root->right);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct TreeNode *expandBinaryTree(struct TreeNode *root)
|
||||||
|
{
|
||||||
|
inorder(root);
|
||||||
|
return root;
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user