Compare commits
2 Commits
58a56e3926
...
321cb6c439
| Author | SHA1 | Date | |
|---|---|---|---|
| 321cb6c439 | |||
| 98b0294f80 |
11
include/solution/1900.h
Normal file
11
include/solution/1900.h
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
#ifndef INC_1900_H
|
||||||
|
#define INC_1900_H
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
int *earliestAndLatest(int n, int firstPlayer, int secondPlayer, int *returnSize);
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
36
include/solution/232.h
Normal file
36
include/solution/232.h
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
#ifndef INC_232_H
|
||||||
|
#define INC_232_H
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
int *base;
|
||||||
|
int ptr;
|
||||||
|
int size;
|
||||||
|
} stack;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
stack *s1;
|
||||||
|
stack *s2;
|
||||||
|
} MyQueue;
|
||||||
|
|
||||||
|
MyQueue *myQueueCreate();
|
||||||
|
|
||||||
|
void myQueuePush(MyQueue *obj, int x);
|
||||||
|
|
||||||
|
int myQueuePop(MyQueue *obj);
|
||||||
|
|
||||||
|
int myQueuePeek(MyQueue *obj);
|
||||||
|
|
||||||
|
bool myQueueEmpty(MyQueue *obj);
|
||||||
|
|
||||||
|
void myQueueFree(MyQueue *obj);
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
6
src/1900.c
Normal file
6
src/1900.c
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#include <solution/1900.h>
|
||||||
|
|
||||||
|
int *earliestAndLatest(int n, int firstPlayer, int secondPlayer, int *returnSize)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
72
src/232.c
Normal file
72
src/232.c
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
#include <solution/232.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
MyQueue *myQueueCreate()
|
||||||
|
{
|
||||||
|
MyQueue *q = (MyQueue *)malloc(sizeof(MyQueue));
|
||||||
|
q->s1 = (stack *)malloc(sizeof(stack));
|
||||||
|
q->s2 = (stack *)malloc(sizeof(stack));
|
||||||
|
|
||||||
|
q->s1->size = q->s2->size = 10;
|
||||||
|
q->s1->ptr = q->s2->ptr = 0;
|
||||||
|
|
||||||
|
q->s1->base = (int *)malloc(sizeof(int) * 10);
|
||||||
|
q->s2->base = (int *)malloc(sizeof(int) * 10);
|
||||||
|
|
||||||
|
return q;
|
||||||
|
}
|
||||||
|
|
||||||
|
void myQueuePush(MyQueue *obj, int x)
|
||||||
|
{
|
||||||
|
if (obj->s1->ptr > obj->s1->size)
|
||||||
|
{
|
||||||
|
obj->s1->base = (int *)realloc(obj->s1->base, sizeof(int) * (obj->s1->size + 10));
|
||||||
|
obj->s2->base = (int *)realloc(obj->s2->base, sizeof(int) * (obj->s1->size + 10));
|
||||||
|
obj->s1->size = obj->s2->size = obj->s1->size + 10;
|
||||||
|
}
|
||||||
|
obj->s1->base[(obj->s1->ptr)++] = x;
|
||||||
|
}
|
||||||
|
|
||||||
|
int myQueuePop(MyQueue *obj)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
while (obj->s1->ptr)
|
||||||
|
obj->s2->base[(obj->s2->ptr)++] = obj->s1->base[--(obj->s1->ptr)];
|
||||||
|
|
||||||
|
ret = obj->s2->base[--(obj->s2->ptr)];
|
||||||
|
|
||||||
|
while (obj->s2->ptr)
|
||||||
|
obj->s1->base[(obj->s1->ptr)++] = obj->s2->base[--(obj->s2->ptr)];
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int myQueuePeek(MyQueue *obj)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
while (obj->s1->ptr)
|
||||||
|
obj->s2->base[(obj->s2->ptr)++] = obj->s1->base[--(obj->s1->ptr)];
|
||||||
|
|
||||||
|
ret = obj->s2->base[obj->s2->ptr - 1];
|
||||||
|
|
||||||
|
obj->s1->ptr = obj->s2->ptr;
|
||||||
|
obj->s2->ptr = 0;
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool myQueueEmpty(MyQueue *obj)
|
||||||
|
{
|
||||||
|
return !obj->s1->ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void myQueueFree(MyQueue *obj)
|
||||||
|
{
|
||||||
|
free(obj->s1->base);
|
||||||
|
free(obj->s2->base);
|
||||||
|
free(obj->s1);
|
||||||
|
free(obj->s2);
|
||||||
|
free(obj);
|
||||||
|
}
|
||||||
34
tests/test_1900.cpp
Normal file
34
tests/test_1900.cpp
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <solution/1900.h>
|
||||||
|
|
||||||
|
class EarliestAndLatestTest : public ::testing::Test
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
void AssertResult(int *result, int resultSize, const std::vector<int> &expected)
|
||||||
|
{
|
||||||
|
ASSERT_EQ(resultSize, expected.size());
|
||||||
|
for (int i = 0; i < resultSize; ++i)
|
||||||
|
{
|
||||||
|
EXPECT_EQ(result[i], expected[i]) << "Mismatch at index " << i;
|
||||||
|
}
|
||||||
|
free(result); // 符合题意:假设函数使用 malloc 分配内存
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Test 1: 输入:n = 11, firstPlayer = 2, secondPlayer = 4,输出:[3,4]
|
||||||
|
TEST_F(EarliestAndLatestTest, Test1)
|
||||||
|
{
|
||||||
|
int returnSize = 0;
|
||||||
|
int *result = earliestAndLatest(11, 2, 4, &returnSize);
|
||||||
|
std::vector<int> expected = {3, 4};
|
||||||
|
AssertResult(result, returnSize, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 2: 输入:n = 5, firstPlayer = 1, secondPlayer = 5,输出:[1,1]
|
||||||
|
TEST_F(EarliestAndLatestTest, Test2)
|
||||||
|
{
|
||||||
|
int returnSize = 0;
|
||||||
|
int *result = earliestAndLatest(5, 1, 5, &returnSize);
|
||||||
|
std::vector<int> expected = {1, 1};
|
||||||
|
AssertResult(result, returnSize, expected);
|
||||||
|
}
|
||||||
49
tests/test_232.cpp
Normal file
49
tests/test_232.cpp
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <solution/232.h>
|
||||||
|
|
||||||
|
class MyQueueTest : public ::testing::Test
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
MyQueue *queue;
|
||||||
|
|
||||||
|
void SetUp() override
|
||||||
|
{
|
||||||
|
queue = myQueueCreate();
|
||||||
|
ASSERT_NE(queue, nullptr); // 确保创建成功
|
||||||
|
}
|
||||||
|
|
||||||
|
void TearDown() override
|
||||||
|
{
|
||||||
|
myQueueFree(queue);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Test1 对应你提供的操作序列
|
||||||
|
TEST_F(MyQueueTest, BasicOperations)
|
||||||
|
{
|
||||||
|
// ["MyQueue", "push", "push", "peek", "pop", "empty"]
|
||||||
|
// [[], [1], [2], [], [], []]
|
||||||
|
|
||||||
|
myQueuePush(queue, 1); // queue: [1]
|
||||||
|
myQueuePush(queue, 2); // queue: [1, 2]
|
||||||
|
|
||||||
|
int peeked = myQueuePeek(queue); // should return 1
|
||||||
|
EXPECT_EQ(peeked, 1);
|
||||||
|
|
||||||
|
int popped = myQueuePop(queue); // should return 1, queue becomes [2]
|
||||||
|
EXPECT_EQ(popped, 1);
|
||||||
|
|
||||||
|
bool isEmpty = myQueueEmpty(queue); // should return false
|
||||||
|
EXPECT_FALSE(isEmpty);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(MyQueueTest, PushPeekEmpty)
|
||||||
|
{
|
||||||
|
myQueuePush(queue, 1); // queue: [1]
|
||||||
|
|
||||||
|
int peeked = myQueuePeek(queue); // should return 1
|
||||||
|
EXPECT_EQ(peeked, 1);
|
||||||
|
|
||||||
|
bool isEmpty = myQueueEmpty(queue); // should return false
|
||||||
|
EXPECT_FALSE(isEmpty);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user