leetcode/tests/queue_with_two_stacks_test.cpp
2023-05-18 19:37:28 +08:00

66 lines
2.7 KiB
C++
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <gtest/gtest.h>
#include "queue_vec.h"
/**
* 用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 appendTail 和 deleteHead ,分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素deleteHead 操作返回 -1 )
* 作者Krahets
* 链接https://leetcode.cn/leetbook/read/illustration-of-algorithm/5d3i87/
* 来源力扣LeetCode
* 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
*/
TEST(QueueWithTwoStackTest, BasicAssertions) {
/**
* 示例 1
* 输入:
* ["CQueue","appendTail","deleteHead","deleteHead","deleteHead"]
* [[],[3],[],[],[]]
* 输出:[null,null,3,-1,-1]
*/
yaha::CQueue c_queue_1;
c_queue_1.append_tail(3);
ASSERT_EQ(c_queue_1.delete_head(), 3);
ASSERT_EQ(c_queue_1.delete_head(), -1);
ASSERT_EQ(c_queue_1.delete_head(), -1);
/**
* 输入:
* ["CQueue","deleteHead","appendTail","appendTail","deleteHead","deleteHead"]
* [[],[],[5],[2],[],[]]
* 输出:[null,-1,null,null,5,2]
*/
yaha::CQueue c_queue_2;
ASSERT_EQ(c_queue_2.delete_head(), -1);
c_queue_2.append_tail(5);
c_queue_2.append_tail(2);
ASSERT_EQ(c_queue_2.delete_head(), 5);
ASSERT_EQ(c_queue_2.delete_head(), 2);
}
TEST(MoreQueueWithTwoStackTest, BasicAssertions) {
yaha::CQueue c_queue;
// 输入示例
std::vector<int> inputs =
{-1, -1, 97, -1, -1, -1, -1, 15, -1, 1, 43, -1, -1, -1, 18, -1, -1, -1, -1, 36, 69, 21, 91, -1, -1, 22, 40, -1,
-1, -1, 81, 65, -1, 77, -1, 63, 96, 5, -1, -1, 35, 90, -1, -1, -1, -1, 77, 83, -1, -1, 52, -1, 2, 66, 87, 90,
-1, 2, -1, -1, 33, 16, 72, -1, -1, 14, 78, 8, -1, -1, -1, -1, 3, 83, -1, -1, 13, -1, 79, 44, -1, -1, 33, -1,
55, 76, 12, -1, 91, 24, 49, 47, -1, -1, -1, 85, -1, 69, -1, 94, 52};
// 预期结果示例
std::vector<int> expected_results =
{-1, -1, 97, -1, -1, -1, -1, 15, -1, 1, 43, -1, -1, -1, 18, -1, -1, -1, -1, 36, 69, 21, 91, -1, -1, 22, 40, -1,
-1, -1, 81, 65, -1, 77, -1, 63, 96, 5, -1, -1, 35, 90, -1, -1, -1, -1, 77, 83, -1, -1, 52, -1, 2, 66, 87, 90,
-1, 2, -1, -1, 33, 16, 72, -1, -1, 14, 78, 8, -1, -1, -1, -1, 3, 83, -1, -1, 13, -1, 79, 44, -1, -1, 33, -1,
55, 76, 12, -1, 91, 24, 49, 47, -1, -1, -1, 85, -1, 69, -1, 94, 52};
// 处理每个操作序列
for (const auto &op : inputs) {
if (op==-1) {
// 删除头部并验证结果为-1
std::cout << c_queue.delete_head() << std::endl;
} else {
// 添加尾部
c_queue.append_tail(op);
}
}
}