Compare commits

...

2 Commits

Author SHA1 Message Date
79b6fbe7e3 367 2025-03-25 18:08:26 +08:00
e0f249d1d9 2711 2025-03-25 18:02:30 +08:00
6 changed files with 66 additions and 4 deletions

View File

@ -70,5 +70,6 @@ add_executable(2786 src/2786.c)
add_executable(3110 src/3110.c)
add_library(2711 STATIC src/2711.c)
add_library(367 STATIC src/367.c)
add_subdirectory(tests)

16
include/solution/367.h Normal file
View 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

View File

@ -13,7 +13,7 @@ int differentElementNum(int *elem, int size)
bool flag = true;
// check same elem
for (int j = 0; j < sum; j++)
if (elem[i] == elem[j])
if (elem[i] == temp[j])
flag = false;
// if not in temp, add to temp
if (flag)
@ -35,9 +35,11 @@ int differentElementNum(int *elem, int size)
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
@ -70,7 +72,5 @@ int **differenceOfDistinctValues(int **grid, int gridSize, int *gridColSize, int
}
*returnSize = gridSize;
*returnColumnSizes = gridColSize;
return answer;
}

16
src/367.c Normal file
View 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;
}
}

View File

@ -1,5 +1,8 @@
add_executable(test_2711 test_2711.cpp)
target_link_libraries(test_2711 PUBLIC gtest_main 2711)
add_executable(test_367 test_367.cpp)
target_link_libraries(test_367 PUBLIC gtest_main 367)
enable_testing()
add_test(NAME test_2711 COMMAND test_2711)
add_test(NAME test_2711 COMMAND test_2711)
add_test(NAME test_367 COMMAND test_367)

26
tests/test_367.cpp Normal file
View File

@ -0,0 +1,26 @@
//
// Created by xfj12 on 2025/3/25.
//
#include <gtest/gtest.h>
#include <solution/367.h>
TEST(IsPerfectSquareTest, PerfectSquareCase)
{
// [IN] 16
// [OUT] true
EXPECT_TRUE(isPerfectSquare(16)); // 验证完全平方数
}
TEST(IsPerfectSquareTest, NonPerfectSquareCase)
{
// [IN] 14
// [OUT] false
EXPECT_FALSE(isPerfectSquare(14)); // 验证非完全平方数
}
TEST(IsPerfectSquareTest, BigIntNonPerfectSquareCase)
{
// [IN] 14
// [OUT] false
EXPECT_FALSE(isPerfectSquare(2147483647)); // 验证非完全平方数
}