From 29ddbd5a3e6cb6d1ef7300a5b25259f4945b255a Mon Sep 17 00:00:00 2001 From: yaha <1143990204@qq.com> Date: Thu, 18 May 2023 19:37:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=A4=E4=B8=AA=E9=98=9F=E5=88=97=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E4=B8=80=E4=B8=AA=E6=A0=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 2 +- cmake/tests.cmake | 4 +- include/queue_vec.h | 16 +++++++ src/queue/queue_with_two_stacks.cpp | 22 ++++++++++ tests/queue_with_two_stacks_test.cpp | 66 ++++++++++++++++++++++++++++ 5 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 include/queue_vec.h create mode 100644 src/queue/queue_with_two_stacks.cpp create mode 100644 tests/queue_with_two_stacks_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 2919a3f..2659bc9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,7 @@ file(GLOB_RECURSE SOURCES "src/*/*.cpp") add_library(${APP_LIB_NAME} ${SOURCES}) include(cmake/googletest.cmake) -option(WITH_PROFILE ON) +option(WITH_PROFILE OFF) if (${WITH_PROFILE}) add_definitions(-DENABLE_PROFILING) include(cmake/gperftools.cmake) diff --git a/cmake/tests.cmake b/cmake/tests.cmake index 90bd72c..1b93091 100644 --- a/cmake/tests.cmake +++ b/cmake/tests.cmake @@ -2,8 +2,10 @@ set(APP_TEST_NAME leetcode_test) message(STATUS "=========== start testing APP_TEST_NAME: ${APP_TEST_NAME} ==========") set(TEST_SOURCES) +string(REGEX REPLACE ";" "\t\n" TMEP_SOURCES "${SOURCES}") file(GLOB_RECURSE TEST_SOURCES "tests/*.cpp") -message(STATUS "=========== test_file: ${TEST_SOURCES}") +message(STATUS "=========== test_file =========== \n${TMEP_SOURCES}") +message(STATUS "=========== test_file =========== \n") include(GoogleTest) diff --git a/include/queue_vec.h b/include/queue_vec.h new file mode 100644 index 0000000..00d1bdf --- /dev/null +++ b/include/queue_vec.h @@ -0,0 +1,16 @@ +#pragma once +#include +namespace yaha { +class CQueue { +public: + CQueue() { + } + + void append_tail(int value); + int delete_head(); + +private: + std::stack _stk_tail; + std::stack _stk_head; +}; +} \ No newline at end of file diff --git a/src/queue/queue_with_two_stacks.cpp b/src/queue/queue_with_two_stacks.cpp new file mode 100644 index 0000000..e8c1f2b --- /dev/null +++ b/src/queue/queue_with_two_stacks.cpp @@ -0,0 +1,22 @@ +#include "queue_vec.h" + +namespace yaha { +void CQueue::append_tail(int value) { + _stk_tail.push(value); +} + +int CQueue::delete_head() { + if(_stk_head.empty()){ + while (!_stk_tail.empty()) { + _stk_head.push(_stk_tail.top()); + _stk_tail.pop(); + } + } + if (_stk_head.empty()) { + return -1; + } + int tmp = _stk_head.top(); + _stk_head.pop(); + return tmp; +} +} \ No newline at end of file diff --git a/tests/queue_with_two_stacks_test.cpp b/tests/queue_with_two_stacks_test.cpp new file mode 100644 index 0000000..98c3087 --- /dev/null +++ b/tests/queue_with_two_stacks_test.cpp @@ -0,0 +1,66 @@ +#include + +#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 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 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); + } + } +} \ No newline at end of file