1534
This commit is contained in:
parent
5f09521d43
commit
3d6aa12717
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
|
17
src/1534.c
Normal file
17
src/1534.c
Normal file
@ -0,0 +1,17 @@
|
||||
//
|
||||
// Created by xfj12 on 2025/4/14.
|
||||
//
|
||||
#include <math.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;
|
||||
}
|
23
tests/test_1534.cpp
Normal file
23
tests/test_1534.cpp
Normal file
@ -0,0 +1,23 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <solution/1534.h>
|
||||
|
||||
TEST(CountGoodTripletsTest, Test1)
|
||||
{
|
||||
int arr[] = {3, 0, 1, 1, 9, 7};
|
||||
int arrSize = sizeof(arr) / sizeof(arr[0]);
|
||||
int a = 7, b = 2, c = 3;
|
||||
int expected = 4;
|
||||
|
||||
EXPECT_EQ(countGoodTriplets(arr, arrSize, a, b, c), expected);
|
||||
}
|
||||
|
||||
// 测试用例 2
|
||||
TEST(CountGoodTripletsTest, Test2)
|
||||
{
|
||||
int arr[] = {1, 1, 2, 2, 3};
|
||||
int arrSize = sizeof(arr) / sizeof(arr[0]);
|
||||
int a = 0, b = 0, c = 1;
|
||||
int expected = 0;
|
||||
|
||||
EXPECT_EQ(countGoodTriplets(arr, arrSize, a, b, c), expected);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user