1957
This commit is contained in:
11
include/solution/1957.h
Normal file
11
include/solution/1957.h
Normal 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
|
||||||
13
src/1957.c
Normal file
13
src/1957.c
Normal 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;
|
||||||
|
}
|
||||||
37
tests/test_1957.cpp
Normal file
37
tests/test_1957.cpp
Normal 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);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 示例 1:leeetcode → leetcode
|
||||||
|
TEST_F(MakeFancyStringTest, Example1)
|
||||||
|
{
|
||||||
|
AssertFancyString("leeetcode", "leetcode");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 示例 2:aaabaaaa → aabaa
|
||||||
|
TEST_F(MakeFancyStringTest, Example2)
|
||||||
|
{
|
||||||
|
AssertFancyString("aaabaaaa", "aabaa");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 示例 3:aab → aab
|
||||||
|
TEST_F(MakeFancyStringTest, Example3)
|
||||||
|
{
|
||||||
|
AssertFancyString("aab", "aab");
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user