Compare commits

..

3 Commits

Author SHA1 Message Date
5f7ef7b880 1957 2025-07-23 20:19:00 +08:00
b9a758a025 1233 2025-07-21 18:14:17 +08:00
cb718130f7 345 2025-07-20 11:06:49 +08:00
9 changed files with 226 additions and 0 deletions

11
include/solution/1233.h Normal file
View File

@@ -0,0 +1,11 @@
#ifndef INC_1233_H
#define INC_1233_H
#ifdef __cplusplus
extern "C"
{
#endif
char **removeSubfolders(char **folder, int folderSize, int *returnSize);
#ifdef __cplusplus
}
#endif
#endif

11
include/solution/1957.h Normal file
View File

@@ -0,0 +1,11 @@
#ifndef INC_1957_H
#define INC_1957_H
#ifdef __cplusplus
extern "C"
{
#endif
char *makeFancyString(char *s);
#ifdef __cplusplus
}
#endif
#endif

11
include/solution/345.h Normal file
View File

@@ -0,0 +1,11 @@
#ifndef INC_345_H
#define INC_345_H
#ifdef __cplusplus
extern "C"
{
#endif
char *reverseVowels(char *s);
#ifdef __cplusplus
}
#endif
#endif

30
src/1233.c Normal file
View File

@@ -0,0 +1,30 @@
#include <solution/1233.h>
#include <stdbool.h>
#include <stdlib.h>
bool startwith(char *p, char *s)
{
for (; *p && *s; p++, s++)
if (*p != *s)
return false;
return (!*p && *s) || (!*p && !*s);
}
char **removeSubfolders(char **folder, int folderSize, int *returnSize)
{
char **ret = malloc(sizeof(char *));
*returnSize = 1;
ret[0] = folder[0];
for (int i = 1; i < folderSize; i++)
{
if (startwith(ret[*(returnSize)-1], folder[i]))
continue;
ret = realloc(ret, sizeof(char *) * ++(*returnSize));
ret[*returnSize - 1] = folder[i];
}
return ret;
}

13
src/1957.c Normal file
View File

@@ -0,0 +1,13 @@
#include <solution/1957.h>
#include <string.h>
char *makeFancyString(char *s)
{
int d[26] = {0};
int len = strlen(s);
for (int i = len - 1; i >= 0; i--)
if (++(d[s[i] - 'a']) > 3)
for (int j = i; j < len - 1; j++)
s[j] = s[j + 1];
return s;
}

32
src/345.c Normal file
View File

@@ -0,0 +1,32 @@
#include <solution/345.h>
#include <string.h>
#define equal(x) \
((x) == 'a' || (x) == 'A' || (x) == 'e' || (x) == 'E' || (x) == 'i' || (x) == 'I' || (x) == 'o' || (x) == 'O' || \
(x) == 'u' || (x) == 'U')
#define swap(x, y, cb) \
do \
{ \
char t = x; \
x = y; \
y = t; \
cb; \
} while (0)
char *reverseVowels(char *s)
{
for (int i = 0, j = strlen(s) - 1, f = 1; i < j;)
{
if (f && equal(s[i]))
f = 0;
else if (f)
i++;
else if (!f && equal(s[j]))
swap(s[i], s[j], (f = 1, i++, j--));
else
j--;
}
return s;
}

52
tests/test_1233.cpp Normal file
View File

@@ -0,0 +1,52 @@
#include <algorithm>
#include <gtest/gtest.h>
#include <solution/1233.h>
#include <string>
#include <vector>
bool ArrayEquals(char **actual, int actualSize, const std::vector<std::string> &expected)
{
if (actualSize != expected.size())
return false;
std::vector<std::string> actualVec(actual, actual + actualSize);
return std::is_permutation(actualVec.begin(), actualVec.end(), expected.begin());
}
// 测试类
class RemoveSubfoldersTest : public ::testing::Test
{
protected:
void AssertFolders(std::vector<std::string> input, std::vector<std::string> expected)
{
int returnSize = 0;
std::vector<char *> c_input;
for (auto &s : input)
c_input.push_back(const_cast<char *>(s.c_str()));
char **result = removeSubfolders(c_input.data(), (int)c_input.size(), &returnSize);
ASSERT_TRUE(ArrayEquals(result, returnSize, expected));
// 可选释放 result 内存如果实现中是动态分配的free
// 例如for (int i = 0; i < returnSize; ++i) free(result[i]);
// free(result);
}
};
// 示例 1
TEST_F(RemoveSubfoldersTest, Example1)
{
AssertFolders({"/a", "/a/b", "/c/d", "/c/d/e", "/c/f"}, {"/a", "/c/d", "/c/f"});
}
// 示例 2
TEST_F(RemoveSubfoldersTest, Example2)
{
AssertFolders({"/a", "/a/b/c", "/a/b/d"}, {"/a"});
}
// 示例 3
TEST_F(RemoveSubfoldersTest, Example3)
{
AssertFolders({"/a/b/c", "/a/b/ca", "/a/b/d"}, {"/a/b/c", "/a/b/ca", "/a/b/d"});
}

37
tests/test_1957.cpp Normal file
View File

@@ -0,0 +1,37 @@
#include <gtest/gtest.h>
#include <solution/1957.h>
#include <string>
class MakeFancyStringTest : public ::testing::Test
{
protected:
void AssertFancyString(const std::string &input, const std::string &expected)
{
char *c_input = strdup(input.c_str()); // 复制一份以防原始修改
char *result = makeFancyString(c_input);
ASSERT_STREQ(result, expected.c_str());
// 如函数返回的是 malloc 分配的,则应释放
// free(result);
free(c_input);
}
};
// 示例 1leeetcode → leetcode
TEST_F(MakeFancyStringTest, Example1)
{
AssertFancyString("leeetcode", "leetcode");
}
// 示例 2aaabaaaa → aabaa
TEST_F(MakeFancyStringTest, Example2)
{
AssertFancyString("aaabaaaa", "aabaa");
}
// 示例 3aab → aab
TEST_F(MakeFancyStringTest, Example3)
{
AssertFancyString("aab", "aab");
}

29
tests/test_345.cpp Normal file
View File

@@ -0,0 +1,29 @@
#include <gtest/gtest.h>
#include <solution/345.h>
class ReverseVowelsTest : public ::testing::Test
{
protected:
// 测试包装器
void AssertReverseVowels(const std::string &input, const std::string &expected)
{
char buffer[1024];
strncpy(buffer, input.c_str(), sizeof(buffer));
buffer[sizeof(buffer) - 1] = '\0'; // 保证结尾
char *result = reverseVowels(buffer);
EXPECT_STREQ(result, expected.c_str());
}
};
// 示例 1
TEST_F(ReverseVowelsTest, Example1)
{
AssertReverseVowels("IceCreAm", "AceCreIm");
}
// 示例 2
TEST_F(ReverseVowelsTest, Example2)
{
AssertReverseVowels("leetcode", "leotcede");
}