976
This commit is contained in:
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
|
||||||
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;
|
||||||
|
}
|
||||||
65
tests/test_976.cpp
Normal file
65
tests/test_976.cpp
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <solution/976.h>
|
||||||
|
// 测试类定义
|
||||||
|
class LargestPerimeterTest : public ::testing::Test
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
void AssertLargestPerimeter(std::vector<int> input, int expected)
|
||||||
|
{
|
||||||
|
int *nums = new int[input.size()];
|
||||||
|
std::copy(input.begin(), input.end(), nums);
|
||||||
|
|
||||||
|
int result = largestPerimeter(nums, input.size());
|
||||||
|
ASSERT_EQ(result, expected);
|
||||||
|
|
||||||
|
delete[] nums;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 示例 1
|
||||||
|
TEST_F(LargestPerimeterTest, Example1)
|
||||||
|
{
|
||||||
|
// 输入: nums = [2,1,2]
|
||||||
|
// 输出: 5
|
||||||
|
AssertLargestPerimeter({2, 1, 2}, 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 示例 2
|
||||||
|
TEST_F(LargestPerimeterTest, Example2)
|
||||||
|
{
|
||||||
|
// 输入: nums = [1,2,1,10]
|
||||||
|
// 输出: 0
|
||||||
|
AssertLargestPerimeter({1, 2, 1, 10}, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 边界测试:最小输入
|
||||||
|
TEST_F(LargestPerimeterTest, SmallestInput)
|
||||||
|
{
|
||||||
|
// 输入: nums = [3,4,5]
|
||||||
|
// 输出: 12
|
||||||
|
AssertLargestPerimeter({3, 4, 5}, 12);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 边界测试:无法组成三角形的情况
|
||||||
|
TEST_F(LargestPerimeterTest, CannotFormTriangle)
|
||||||
|
{
|
||||||
|
// 输入: nums = [1,1,2]
|
||||||
|
// 输出: 0
|
||||||
|
AssertLargestPerimeter({1, 1, 2}, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 边界测试:重复元素
|
||||||
|
TEST_F(LargestPerimeterTest, RepeatedElements)
|
||||||
|
{
|
||||||
|
// 输入: nums = [3,3,3,3,3]
|
||||||
|
// 输出: 9
|
||||||
|
AssertLargestPerimeter({3, 3, 3, 3, 3}, 9);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 边界测试:无有效三角形
|
||||||
|
TEST_F(LargestPerimeterTest, NoValidTriangle)
|
||||||
|
{
|
||||||
|
// 输入: nums = [1,1,1,10]
|
||||||
|
// 输出: 3
|
||||||
|
AssertLargestPerimeter({1, 1, 1, 10}, 3);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user