From b1260a488c3fc7e3f08318fe7b88b5cad8a597a0 Mon Sep 17 00:00:00 2001 From: yaha <1143990204@qq.com> Date: Fri, 19 May 2023 18:26:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E6=95=B0=E5=80=BC=E6=A3=80?= =?UTF-8?q?=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/jianzhi_offer.h | 2 ++ src/jianzhi_offer/list_node.cpp | 17 +++++++++++++++-- src/jianzhi_offer/string.cpp | 10 ++++++++++ tests/jianzhi_offer/list_node_test.cpp | 13 ++++++++++++- tests/jianzhi_offer/string_test.cpp | 11 ++++++++++- 5 files changed, 49 insertions(+), 4 deletions(-) diff --git a/include/jianzhi_offer.h b/include/jianzhi_offer.h index a59a238..e05565d 100644 --- a/include/jianzhi_offer.h +++ b/include/jianzhi_offer.h @@ -21,6 +21,7 @@ private: class String { public: std::string replace_space(std::string s); + bool is_number(std::string s); }; class ListNode { @@ -29,6 +30,7 @@ public: ListNode *next; ListNode(int x) : val(x), next(NULL) {} std::vector reverse_print(ListNode *head); + void reverse_stack(ListNode *head, std::vector& res); void reverse(ListNode *head, std::vector& res); }; diff --git a/src/jianzhi_offer/list_node.cpp b/src/jianzhi_offer/list_node.cpp index f9eaadf..8a3b9f5 100644 --- a/src/jianzhi_offer/list_node.cpp +++ b/src/jianzhi_offer/list_node.cpp @@ -1,8 +1,8 @@ #include "jianzhi_offer.h" namespace yaha { -void ListNode::reverse(ListNode *head, std::vector& res) { - if (head==nullptr) { +void ListNode::reverse(ListNode *head, std::vector &res) { + if (head == nullptr) { return; } @@ -10,6 +10,19 @@ void ListNode::reverse(ListNode *head, std::vector& res) { res.push_back(head->val); } +void ListNode::reverse_stack(ListNode *head, std::vector &res) { + std::stack s; + while (head != nullptr) { + s.push(head->val); + head = head->next; + } + + while (!s.empty()) { + res.push_back(s.top()); + s.pop(); + } +} + std::vector ListNode::reverse_print(ListNode *head) { std::vector res; reverse(head, res); diff --git a/src/jianzhi_offer/string.cpp b/src/jianzhi_offer/string.cpp index 4e08d19..d610edb 100644 --- a/src/jianzhi_offer/string.cpp +++ b/src/jianzhi_offer/string.cpp @@ -12,4 +12,14 @@ std::string String::replace_space(std::string s) { } return result; } + +bool String::is_number(std::string s) { + for (int i = 0; i < s.length(); ++i) { + if (s[i] >= '0' && s[i] <= '9') { + return true; + } + } + + return false; +} } \ No newline at end of file diff --git a/tests/jianzhi_offer/list_node_test.cpp b/tests/jianzhi_offer/list_node_test.cpp index ca950ac..a05ed8c 100644 --- a/tests/jianzhi_offer/list_node_test.cpp +++ b/tests/jianzhi_offer/list_node_test.cpp @@ -3,11 +3,22 @@ #include "jianzhi_offer.h" TEST(NodeListTest, BasicAssertions) { - yaha::ListNode* node = new yaha::ListNode(1); + 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]); +} + +TEST(NodeListStackTest, BasicAssertions) { + yaha::ListNode *node = new yaha::ListNode(1); + node->next = new yaha::ListNode(3); + node->next->next = new yaha::ListNode(2); + std::vector res; + node->reverse_stack(node, res); + 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/string_test.cpp b/tests/jianzhi_offer/string_test.cpp index ab0ef54..a5d7224 100644 --- a/tests/jianzhi_offer/string_test.cpp +++ b/tests/jianzhi_offer/string_test.cpp @@ -6,5 +6,14 @@ TEST(ReplaceSpaceTest, BasicAssertions) { std::string expected = "We%20are%20happy."; yaha::String s; - EXPECT_EQ(expected, s.replace_space(input)); + ASSERT_EQ(expected, s.replace_space(input)); +} + +TEST(IsNumberTest, BasicAssertions) { + yaha::String s; + ASSERT_TRUE(s.is_number("0")); + ASSERT_TRUE(s.is_number(" .1 ")); + ASSERT_FALSE(s.is_number(".")); + ASSERT_FALSE(s.is_number("e")); + ASSERT_FALSE(s.is_number("e9")); } \ No newline at end of file