Compare commits

...

2 Commits

Author SHA1 Message Date
f54c320ebf 392 2025-07-18 22:37:04 +08:00
6df3e30818 387 2025-07-18 22:23:57 +08:00
6 changed files with 167 additions and 0 deletions

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

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

12
include/solution/392.h Normal file
View File

@@ -0,0 +1,12 @@
#ifndef INC_392_H
#define INC_392_H
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdbool.h>
bool isSubsequence(char *s, char *t);
#ifdef __cplusplus
}
#endif
#endif

17
src/387.c Normal file
View File

@@ -0,0 +1,17 @@
#include <solution/387.h>
#include <string.h>
int firstUniqChar(char *s)
{
int hash[26];
memset(hash, 0, sizeof(int) * 26);
for (int idx = 0; s[idx]; idx++)
hash[s[idx] - 'a']++;
for (int idx = 0; s[idx]; idx++)
if (hash[s[idx] - 'a'] == 1)
return idx;
return -1;
}

10
src/392.c Normal file
View File

@@ -0,0 +1,10 @@
#include <solution/392.h>
bool isSubsequence(char *s, char *t)
{
for (; *s && *t; t++)
if (*s == *t)
s++;
return !*s;
}

51
tests/test_387.cpp Normal file
View File

@@ -0,0 +1,51 @@
#include <gtest/gtest.h>
#include <solution/387.h>
class FirstUniqCharTest : public ::testing::Test
{
protected:
void AssertFirstUniqChar(const std::string &input, int expected)
{
char buf[1001];
strncpy(buf, input.c_str(), sizeof(buf));
buf[1000] = '\0'; // 防止越界
int result = firstUniqChar(buf);
EXPECT_EQ(result, expected);
}
};
// 示例 1leetcode → 'l' 是第一个唯一字符,索引 0
TEST_F(FirstUniqCharTest, Example1)
{
AssertFirstUniqChar("leetcode", 0);
}
// 示例 2loveleetcode → 'v' 是第一个唯一字符,索引 2
TEST_F(FirstUniqCharTest, Example2)
{
AssertFirstUniqChar("loveleetcode", 2);
}
// 示例 3aabb → 没有唯一字符,返回 -1
TEST_F(FirstUniqCharTest, Example3)
{
AssertFirstUniqChar("aabb", -1);
}
// 边界测试:空字符串
TEST_F(FirstUniqCharTest, EmptyString)
{
AssertFirstUniqChar("", -1);
}
// 边界测试:单字符
TEST_F(FirstUniqCharTest, SingleChar)
{
AssertFirstUniqChar("z", 0);
}
// 更多测试:末尾唯一字符
TEST_F(FirstUniqCharTest, UniqueAtEnd)
{
AssertFirstUniqChar("aabbc", 4); // 'c' 是唯一字符
}

66
tests/test_392.cpp Normal file
View File

@@ -0,0 +1,66 @@
#include <gtest/gtest.h>
#include <solution/392.h>
class IsSubsequenceTest : public ::testing::Test
{
protected:
void AssertIsSubsequence(const std::string &s, const std::string &t, bool expected)
{
char s_buf[1001], t_buf[1001];
strncpy(s_buf, s.c_str(), sizeof(s_buf));
s_buf[1000] = '\0';
strncpy(t_buf, t.c_str(), sizeof(t_buf));
t_buf[1000] = '\0';
bool result = isSubsequence(s_buf, t_buf);
EXPECT_EQ(result, expected);
}
};
// 示例 1s = "abc", t = "ahbgdc" → true
TEST_F(IsSubsequenceTest, Example1)
{
AssertIsSubsequence("abc", "ahbgdc", true);
}
// 示例 2s = "axc", t = "ahbgdc" → false
TEST_F(IsSubsequenceTest, Example2)
{
AssertIsSubsequence("axc", "ahbgdc", false);
}
// 边界测试s 为空 → 总是 true
TEST_F(IsSubsequenceTest, EmptyS)
{
AssertIsSubsequence("", "anystring", true);
}
// 边界测试t 为空 → 只有 s 也为空才为 true
TEST_F(IsSubsequenceTest, EmptyT)
{
AssertIsSubsequence("a", "", false);
AssertIsSubsequence("", "", true);
}
// s 比 t 长 → false
TEST_F(IsSubsequenceTest, SLongerThanT)
{
AssertIsSubsequence("abcdefg", "abc", false);
}
// s 与 t 完全相同 → true
TEST_F(IsSubsequenceTest, SameString)
{
AssertIsSubsequence("abcde", "abcde", true);
}
// 非连续但顺序正确 → true
TEST_F(IsSubsequenceTest, SparseSubsequence)
{
AssertIsSubsequence("ace", "abcde", true);
}
// 非连续顺序错误 → false
TEST_F(IsSubsequenceTest, WrongOrder)
{
AssertIsSubsequence("aec", "abcde", false);
}