Compare commits
2 Commits
f6e998beff
...
050164b61b
| Author | SHA1 | Date | |
|---|---|---|---|
| 050164b61b | |||
| af0fe8a168 |
@@ -1 +1 @@
|
|||||||
BasedOnStyle: Microsoft
|
BasedOnStyle: Microsoft
|
||||||
|
|||||||
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
|
||||||
@@ -22,4 +22,4 @@ int *getRow(int rowIndex, int *returnSize)
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,4 +27,4 @@ char **removeSubfolders(char **folder, int folderSize, int *returnSize)
|
|||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,4 +9,4 @@ int getDecimalValue(struct ListNode *head)
|
|||||||
head = head->next;
|
head = head->next;
|
||||||
}
|
}
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,4 +14,4 @@ int countGoodTriplets(int *arr, int arrSize, int a, int b, int c)
|
|||||||
count++;
|
count++;
|
||||||
|
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,4 +35,4 @@ int main()
|
|||||||
printf("%s\n", ans);
|
printf("%s\n", ans);
|
||||||
free(ans);
|
free(ans);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,4 +22,4 @@ int main()
|
|||||||
assert(titleToNumber("ZY") == 701);
|
assert(titleToNumber("ZY") == 701);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -88,4 +88,4 @@ int maximumGain(char *s, int x, int y)
|
|||||||
del(del(s, 'b', 'a', &score, y), 'a', 'b', &score, x);
|
del(del(s, 'b', 'a', &score, y), 'a', 'b', &score, x);
|
||||||
|
|
||||||
return score;
|
return score;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,4 +3,4 @@
|
|||||||
int *earliestAndLatest(int n, int firstPlayer, int secondPlayer, int *returnSize)
|
int *earliestAndLatest(int n, int firstPlayer, int secondPlayer, int *returnSize)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,4 +10,4 @@ char *makeFancyString(char *s)
|
|||||||
for (int j = i; j < len - 1; j++)
|
for (int j = i; j < len - 1; j++)
|
||||||
s[j] = s[j + 1];
|
s[j] = s[j + 1];
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#include <solution/2163.h>
|
#include <solution/2163.h>
|
||||||
long long minimumDifference(int *nums, int numsSize)
|
long long minimumDifference(int *nums, int numsSize)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,4 +29,4 @@ int main()
|
|||||||
assert(containsNearbyDuplicate(nums2, sizeof(nums2) / sizeof(int), 1) == true);
|
assert(containsNearbyDuplicate(nums2, sizeof(nums2) / sizeof(int), 1) == true);
|
||||||
int nums3[] = {1, 2, 3, 1, 2, 3};
|
int nums3[] = {1, 2, 3, 1, 2, 3};
|
||||||
assert(containsNearbyDuplicate(nums3, sizeof(nums3) / sizeof(int), 2) == false);
|
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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -69,4 +69,4 @@ void myQueueFree(MyQueue *obj)
|
|||||||
free(obj->s1);
|
free(obj->s1);
|
||||||
free(obj->s2);
|
free(obj->s2);
|
||||||
free(obj);
|
free(obj);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,4 +27,4 @@ int matchPlayersAndTrainers(int *players, int playersSize, int *trainers, int tr
|
|||||||
}
|
}
|
||||||
|
|
||||||
return match;
|
return match;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,4 +15,4 @@ long long minimumCost(char *s)
|
|||||||
ans += min(i, n - i);
|
ans += min(i, n - i);
|
||||||
|
|
||||||
return ans;
|
return ans;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,4 +45,4 @@ int main()
|
|||||||
return 0;
|
return 0;
|
||||||
fail:
|
fail:
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,4 +70,4 @@ bool wordPattern(char *pattern, char *s)
|
|||||||
|
|
||||||
freeDict(d);
|
freeDict(d);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,4 +49,4 @@ int *numberGame(int *nums, int numsSize, int *returnSize)
|
|||||||
printf("%d ", result[i]);
|
printf("%d ", result[i]);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,4 +41,4 @@ void numArrayFree(NumArray *obj)
|
|||||||
{
|
{
|
||||||
free(obj->data);
|
free(obj->data);
|
||||||
free(obj);
|
free(obj);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,4 +18,4 @@ int main()
|
|||||||
{
|
{
|
||||||
assert(scoreOfString("hello") == 13);
|
assert(scoreOfString("hello") == 13);
|
||||||
assert(scoreOfString("zaz") == 50);
|
assert(scoreOfString("zaz") == 50);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,4 +30,4 @@ bool isValid(char *word)
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
return haveVowel && haveConsonant;
|
return haveVowel && haveConsonant;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,4 +39,4 @@ int countDays(int days, int **meetings, int meetingsSize, int *meetingsColSize)
|
|||||||
return count + days - meetings[meetingsSize - 1][1];
|
return count + days - meetings[meetingsSize - 1][1];
|
||||||
|
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,4 +10,4 @@ int maximumLength(int *nums, int numsSize)
|
|||||||
nums[i] & 1 ? (odd = even + 1, odd_cnt++) : (even = odd + 1);
|
nums[i] & 1 ? (odd = even + 1, odd_cnt++) : (even = odd + 1);
|
||||||
|
|
||||||
return max(max(even, odd), max(odd_cnt, numsSize - odd_cnt));
|
return max(max(even, odd), max(odd_cnt, numsSize - odd_cnt));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,4 +2,4 @@
|
|||||||
|
|
||||||
int maximumLength(int *nums, int numsSize, int k)
|
int maximumLength(int *nums, int numsSize, int k)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,4 +20,4 @@ bool isPowerOfThree(int n)
|
|||||||
// 我们只需要判断 n 是否是 3^19 的约数即可。
|
// 我们只需要判断 n 是否是 3^19 的约数即可。
|
||||||
return n > 0 && 1162261467 % n == 0;
|
return n > 0 && 1162261467 % n == 0;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,4 +10,4 @@ int *countBits(int n, int *returnSize)
|
|||||||
int main() {
|
int main() {
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,4 +12,4 @@ int subarraySum(int *nums, int numsSize)
|
|||||||
for (int j = max(0, i - nums[i]); j <= i; j++)
|
for (int j = max(0, i - nums[i]); j <= i; j++)
|
||||||
sum += nums[j];
|
sum += nums[j];
|
||||||
return sum;
|
return sum;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,4 +13,4 @@ bool isPerfectSquare(int num)
|
|||||||
if (i * i == num)
|
if (i * i == num)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,4 +14,4 @@ int firstUniqChar(char *s)
|
|||||||
return idx;
|
return idx;
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,4 +7,4 @@ bool isSubsequence(char *s, char *t)
|
|||||||
s++;
|
s++;
|
||||||
|
|
||||||
return !*s;
|
return !*s;
|
||||||
}
|
}
|
||||||
|
|||||||
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;
|
||||||
|
}
|
||||||
@@ -34,4 +34,4 @@ struct TreeNode *expandBinaryTree(struct TreeNode *root)
|
|||||||
{
|
{
|
||||||
inorder(root);
|
inorder(root);
|
||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|||||||
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