Compare commits
2 Commits
aabf07d9ec
...
c4ad87ecea
Author | SHA1 | Date | |
---|---|---|---|
c4ad87ecea | |||
59f589894b |
@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.30)
|
||||
project(leetcode C CXX)
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||
|
||||
include_directories(include)
|
||||
@ -35,11 +36,39 @@ FetchContent_Declare(
|
||||
|
||||
FetchContent_MakeAvailable(googletest)
|
||||
|
||||
file(GLOB SRC_FILES "src/*.c")
|
||||
add_executable(13 src/13.c)
|
||||
add_executable(88 src/88.c)
|
||||
add_executable(100 src/100.c)
|
||||
add_executable(101 src/101.c)
|
||||
add_executable(104 src/104.c)
|
||||
add_executable(108 src/108.c)
|
||||
add_executable(110 src/110.c)
|
||||
add_executable(111 src/111.c)
|
||||
add_executable(112 src/119.c)
|
||||
add_executable(136 src/136.c)
|
||||
add_executable(141 src/141.c)
|
||||
add_executable(144 src/144.c)
|
||||
add_executable(145 src/145.c)
|
||||
add_executable(160 src/160.c)
|
||||
add_executable(168 src/168.c)
|
||||
add_executable(169 src/169.c)
|
||||
add_executable(171 src/171.c)
|
||||
add_executable(190 src/190.c)
|
||||
add_executable(191 src/191.c)
|
||||
add_executable(202 src/202.c)
|
||||
add_executable(219 src/219.c)
|
||||
add_executable(225 src/225.c)
|
||||
add_executable(228 src/228.c)
|
||||
add_executable(231 src/231.c)
|
||||
add_executable(258 src/258.c)
|
||||
add_executable(263 src/263.c)
|
||||
add_executable(268 src/268.c)
|
||||
add_executable(278 src/278.c)
|
||||
add_executable(338 src/338.c)
|
||||
add_executable(374 src/374.c)
|
||||
add_executable(2786 src/2786.c)
|
||||
add_executable(3110 src/3110.c)
|
||||
|
||||
foreach (SRC_FILE ${SRC_FILES})
|
||||
get_filename_component(EXE_NAME ${SRC_FILE} NAME_WE)
|
||||
add_executable(${EXE_NAME} ${SRC_FILE})
|
||||
endforeach ()
|
||||
add_library(2711 STATIC src/2711.c)
|
||||
|
||||
add_subdirectory(tests)
|
||||
|
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
|
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] == elem[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);
|
||||
for (int r = 0; r < gridSize; r++)
|
||||
{
|
||||
answer[r] = (int *)malloc(sizeof(int) * 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;
|
||||
*returnColumnSizes = gridColSize;
|
||||
|
||||
return answer;
|
||||
}
|
@ -1,7 +1,5 @@
|
||||
file(GLOB SRC_FILES "*.cpp")
|
||||
add_executable(test_2711 test_2711.cpp)
|
||||
target_link_libraries(test_2711 PUBLIC gtest_main 2711)
|
||||
|
||||
foreach(SRC_FILE ${SRC_FILES})
|
||||
get_filename_component(EXE_NAME ${SRC_FILE} NAME_WE)
|
||||
add_executable(${EXE_NAME} ${SRC_FILE})
|
||||
target_link_libraries(${EXE_NAME} gtest gmock)
|
||||
endforeach()
|
||||
enable_testing()
|
||||
add_test(NAME test_2711 COMMAND test_2711)
|
92
tests/test_2711.cpp
Normal file
92
tests/test_2711.cpp
Normal file
@ -0,0 +1,92 @@
|
||||
//
|
||||
// Created by xfj12 on 2025/3/25.
|
||||
//
|
||||
#include <gtest/gtest.h>
|
||||
#include <solution/2711.h>
|
||||
|
||||
class DifferenceOfDistinctValuesTest : public ::testing::Test
|
||||
{
|
||||
protected:
|
||||
void SetUp() override
|
||||
{
|
||||
}
|
||||
void TearDown() override
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(DifferenceOfDistinctValuesTest, HandlesExpectedOutput)
|
||||
{
|
||||
int gridSize = 6;
|
||||
int gridColSize[] = {10, 10, 10, 10, 10, 10};
|
||||
int *grid[6];
|
||||
int data[6][10] = {{6, 28, 16, 15, 35, 19, 38, 42, 32, 27}, {8, 49, 38, 16, 6, 9, 12, 11, 10, 6},
|
||||
{16, 32, 46, 17, 15, 29, 5, 47, 48, 33}, {25, 12, 34, 21, 8, 15, 3, 3, 7, 38},
|
||||
{32, 23, 9, 30, 35, 27, 10, 5, 12, 18}, {11, 2, 48, 22, 43, 26, 25, 1, 20, 19}};
|
||||
|
||||
for (int i = 0; i < gridSize; i++)
|
||||
{
|
||||
grid[i] = data[i];
|
||||
}
|
||||
|
||||
int returnSize;
|
||||
int *returnColumnSizes;
|
||||
int **result = differenceOfDistinctValues(grid, gridSize, gridColSize, &returnSize, &returnColumnSizes);
|
||||
|
||||
int expected[6][10] = {{5, 5, 4, 5, 5, 4, 3, 2, 1, 0}, {4, 3, 3, 2, 3, 3, 2, 1, 0, 1},
|
||||
{3, 2, 1, 1, 2, 1, 1, 0, 1, 2}, {2, 1, 0, 1, 1, 0, 1, 1, 2, 3},
|
||||
{1, 0, 1, 2, 3, 3, 1, 3, 3, 4}, {0, 1, 2, 3, 4, 5, 5, 3, 5, 5}};
|
||||
|
||||
ASSERT_EQ(returnSize, gridSize);
|
||||
for (int i = 0; i < gridSize; i++)
|
||||
{
|
||||
ASSERT_EQ(returnColumnSizes[i], 10);
|
||||
for (int j = 0; j < 10; j++)
|
||||
{
|
||||
ASSERT_EQ(result[i][j], expected[i][j]);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < returnSize; i++)
|
||||
{
|
||||
free(result[i]);
|
||||
}
|
||||
free(result);
|
||||
free(returnColumnSizes);
|
||||
}
|
||||
|
||||
TEST_F(DifferenceOfDistinctValuesTest, HandlesSmallGrid)
|
||||
{
|
||||
int gridSize = 3;
|
||||
int gridColSize[] = {3, 3, 3};
|
||||
int *grid[3];
|
||||
int data[3][3] = {{1, 2, 3}, {3, 1, 5}, {3, 2, 1}};
|
||||
|
||||
for (int i = 0; i < gridSize; i++)
|
||||
{
|
||||
grid[i] = data[i];
|
||||
}
|
||||
|
||||
int returnSize;
|
||||
int *returnColumnSizes;
|
||||
int **result = differenceOfDistinctValues(grid, gridSize, gridColSize, &returnSize, &returnColumnSizes);
|
||||
|
||||
int expected[3][3] = {{1, 1, 0}, {1, 0, 1}, {0, 1, 1}};
|
||||
|
||||
ASSERT_EQ(returnSize, gridSize);
|
||||
for (int i = 0; i < gridSize; i++)
|
||||
{
|
||||
ASSERT_EQ(returnColumnSizes[i], 3);
|
||||
for (int j = 0; j < 3; j++)
|
||||
{
|
||||
ASSERT_EQ(result[i][j], expected[i][j]);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < returnSize; i++)
|
||||
{
|
||||
free(result[i]);
|
||||
}
|
||||
free(result);
|
||||
free(returnColumnSizes);
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
//
|
||||
// Created by aurora on 2024/9/16.
|
||||
//
|
||||
#include <gtest/gtest.h>
|
||||
#include <solution/338.h>
|
||||
|
||||
TEST(CountingBits, 1)
|
||||
{
|
||||
int size = 0;
|
||||
int ans[] = {0, 1, 1};
|
||||
EXPECT_EQ(ans, ans);//countBits(2, &size));
|
||||
}
|
||||
|
||||
int main(){return 0;}
|
Loading…
x
Reference in New Issue
Block a user