From ff0816083113dd12137db5ac250bf6c44141aad5 Mon Sep 17 00:00:00 2001 From: yaha <1143990204@qq.com> Date: Fri, 19 May 2023 13:46:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E9=93=BE=E8=A1=A8=E6=89=93?= =?UTF-8?q?=E5=8D=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmake/tests.cmake | 6 +- include/jianzhi_offer.h | 35 ++++++++++ include/queue_vec.h | 16 ----- src/jianzhi_offer/list_node.cpp | 19 ++++++ src/jianzhi_offer/queue_with_two_stacks.cpp | 37 +++++++++++ src/jianzhi_offer/string.cpp | 15 +++++ src/queue/queue_with_two_stacks.cpp | 22 ------- tests/jianzhi_offer/list_node_test.cpp | 13 ++++ .../queue_with_two_stacks_test.cpp | 37 +++++++++++ tests/jianzhi_offer/string_test.cpp | 10 +++ tests/queue_with_two_stacks_test.cpp | 66 ------------------- tests/test_main.cpp | 4 +- 12 files changed, 172 insertions(+), 108 deletions(-) create mode 100644 include/jianzhi_offer.h delete mode 100644 include/queue_vec.h create mode 100644 src/jianzhi_offer/list_node.cpp create mode 100644 src/jianzhi_offer/queue_with_two_stacks.cpp create mode 100644 src/jianzhi_offer/string.cpp delete mode 100644 src/queue/queue_with_two_stacks.cpp create mode 100644 tests/jianzhi_offer/list_node_test.cpp create mode 100644 tests/jianzhi_offer/queue_with_two_stacks_test.cpp create mode 100644 tests/jianzhi_offer/string_test.cpp delete mode 100644 tests/queue_with_two_stacks_test.cpp diff --git a/cmake/tests.cmake b/cmake/tests.cmake index 1b93091..5fcc2a8 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") + +file(GLOB_RECURSE TEST_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/tests/*.cpp") +string(REGEX REPLACE ";" "\t\n" TMEP_SOURCES "${TEST_SOURCES}") + message(STATUS "=========== test_file =========== \n${TMEP_SOURCES}") message(STATUS "=========== test_file =========== \n") diff --git a/include/jianzhi_offer.h b/include/jianzhi_offer.h new file mode 100644 index 0000000..a59a238 --- /dev/null +++ b/include/jianzhi_offer.h @@ -0,0 +1,35 @@ +#pragma once + +#include +#include +#include + +namespace yaha { +class CQueue { +public: + CQueue() { + } + + void append_tail(int value); + int delete_head(); + +private: + std::stack _stk_tail; + std::stack _stk_head; +}; + +class String { +public: + std::string replace_space(std::string s); +}; + +class ListNode { +public: + int val; + ListNode *next; + ListNode(int x) : val(x), next(NULL) {} + std::vector reverse_print(ListNode *head); + void reverse(ListNode *head, std::vector& res); +}; + +} \ No newline at end of file diff --git a/include/queue_vec.h b/include/queue_vec.h deleted file mode 100644 index 00d1bdf..0000000 --- a/include/queue_vec.h +++ /dev/null @@ -1,16 +0,0 @@ -#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/jianzhi_offer/list_node.cpp b/src/jianzhi_offer/list_node.cpp new file mode 100644 index 0000000..f9eaadf --- /dev/null +++ b/src/jianzhi_offer/list_node.cpp @@ -0,0 +1,19 @@ +#include "jianzhi_offer.h" + +namespace yaha { +void ListNode::reverse(ListNode *head, std::vector& res) { + if (head==nullptr) { + return; + } + + reverse(head->next, res); + res.push_back(head->val); +} + +std::vector ListNode::reverse_print(ListNode *head) { + std::vector res; + reverse(head, res); + return res; +} + +} \ No newline at end of file diff --git a/src/jianzhi_offer/queue_with_two_stacks.cpp b/src/jianzhi_offer/queue_with_two_stacks.cpp new file mode 100644 index 0000000..e132539 --- /dev/null +++ b/src/jianzhi_offer/queue_with_two_stacks.cpp @@ -0,0 +1,37 @@ +#include "jianzhi_offer.h" + +namespace yaha { +void CQueue::append_tail(int value) { + _stk_tail.push(value); +} + +/** + * 1. 如果双栈都为空,则返回 -1 + * 2. 如果当栈 _stk_head 不为空,直接返回_stk_head + * 3. 当_stk_tail不为空,_stk_head为空,则需要完成重排列并返回_stk_head + * @return + */ +int CQueue::delete_head() { + + if (_stk_tail.empty() && _stk_head.empty()) { + return -1; + } + + if (!_stk_head.empty()) { + int tmp = _stk_head.top(); + _stk_head.pop(); + return tmp; + } + + if (_stk_head.empty()) { + while (!_stk_tail.empty()) { + _stk_head.push(_stk_tail.top()); + _stk_tail.pop(); + } + + } + int tmp = _stk_head.top(); + _stk_head.pop(); + return tmp; +} +} \ No newline at end of file diff --git a/src/jianzhi_offer/string.cpp b/src/jianzhi_offer/string.cpp new file mode 100644 index 0000000..4e08d19 --- /dev/null +++ b/src/jianzhi_offer/string.cpp @@ -0,0 +1,15 @@ +#include "jianzhi_offer.h" + +namespace yaha { +std::string String::replace_space(std::string s) { + std::string result; + for (int i = 0; i < s.length(); ++i) { + if (s[i]!=' ') { + result += s[i]; + } else { + result += "%20"; + } + } + return result; +} +} \ No newline at end of file diff --git a/src/queue/queue_with_two_stacks.cpp b/src/queue/queue_with_two_stacks.cpp deleted file mode 100644 index e8c1f2b..0000000 --- a/src/queue/queue_with_two_stacks.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#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/jianzhi_offer/list_node_test.cpp b/tests/jianzhi_offer/list_node_test.cpp new file mode 100644 index 0000000..ca950ac --- /dev/null +++ b/tests/jianzhi_offer/list_node_test.cpp @@ -0,0 +1,13 @@ +#include + +#include "jianzhi_offer.h" + +TEST(NodeListTest, BasicAssertions) { + yaha::ListNode* node = new yaha::ListNode(1); + node->next = new yaha::ListNode(3); + node->next->next = new yaha::ListNode(2); + auto res = node->reverse_print(node); + ASSERT_EQ(2, res[0]); + ASSERT_EQ(3, res[1]); + ASSERT_EQ(1, res[2]); +} \ No newline at end of file diff --git a/tests/jianzhi_offer/queue_with_two_stacks_test.cpp b/tests/jianzhi_offer/queue_with_two_stacks_test.cpp new file mode 100644 index 0000000..a717223 --- /dev/null +++ b/tests/jianzhi_offer/queue_with_two_stacks_test.cpp @@ -0,0 +1,37 @@ +#include + +#include "jianzhi_offer.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); +} diff --git a/tests/jianzhi_offer/string_test.cpp b/tests/jianzhi_offer/string_test.cpp new file mode 100644 index 0000000..ab0ef54 --- /dev/null +++ b/tests/jianzhi_offer/string_test.cpp @@ -0,0 +1,10 @@ +#include "gtest/gtest.h" +#include "jianzhi_offer.h" + +TEST(ReplaceSpaceTest, BasicAssertions) { + std::string input = "We are happy."; + std::string expected = "We%20are%20happy."; + + yaha::String s; + EXPECT_EQ(expected, s.replace_space(input)); +} \ No newline at end of file diff --git a/tests/queue_with_two_stacks_test.cpp b/tests/queue_with_two_stacks_test.cpp deleted file mode 100644 index 98c3087..0000000 --- a/tests/queue_with_two_stacks_test.cpp +++ /dev/null @@ -1,66 +0,0 @@ -#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 diff --git a/tests/test_main.cpp b/tests/test_main.cpp index 638b7e6..cf13503 100644 --- a/tests/test_main.cpp +++ b/tests/test_main.cpp @@ -25,12 +25,12 @@ int main(int argc, char *argv[]) { HeapProfilerStart(heap_name.c_str()); #endif - int result = RUN_ALL_TESTS(); + auto ret = RUN_ALL_TESTS(); #ifdef ENABLE_PROFILING ProfilerStop(); HeapProfilerStop(); #endif - return result; + return ret; } \ No newline at end of file