diff --git a/include/jianzhi_offer.h b/include/jianzhi_offer.h index e05565d..190e0f0 100644 --- a/include/jianzhi_offer.h +++ b/include/jianzhi_offer.h @@ -22,6 +22,8 @@ class String { public: std::string replace_space(std::string s); bool is_number(std::string s); + + std::string reverse_left_words(std::string s, int n); }; class ListNode { @@ -30,8 +32,48 @@ 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); + void reverse_stack(ListNode *head, std::vector &res); + void reverse(ListNode *head, std::vector &res); +}; + + +class Node { +public: + int val; + Node *next; + Node *random; + + Node(int _val) { + val = _val; + next = NULL; + random = NULL; + } + + /** + * 剑指 Offer 35. 复杂链表的复制 + * + * @url https://leetcode.cn/leetbook/read/illustration-of-algorithm/9p0yy1/ + * @param head + * @return + */ + Node* copy_random_list(Node* head); +}; + +/** + * 剑指 Offer 30. 包含 min 函数的栈 + * + * @url: https://leetcode.cn/leetbook/read/illustration-of-algorithm/50bp33/ + */ +class MinStack { +public: + MinStack() = default; + void push(int x); + void pop(); + int top(); + int min(); +private: + std::stack _stk; + std::stack _min_stk; }; } \ No newline at end of file diff --git a/src/jianzhi_offer/min_stack.cpp b/src/jianzhi_offer/min_stack.cpp new file mode 100644 index 0000000..d894453 --- /dev/null +++ b/src/jianzhi_offer/min_stack.cpp @@ -0,0 +1,25 @@ +#include "jianzhi_offer.h" +namespace yaha { +void MinStack::push(int x) { + _stk.push(x); + + if (_min_stk.empty() || _min_stk.top() >= x) { + _min_stk.push(x); + } +} + +void MinStack::pop() { + if (_stk.top() == _min_stk.top()) { + _min_stk.pop(); + } + _stk.pop(); +} + +int MinStack::top() { + return _stk.top(); +} + +int MinStack::min() { + return _min_stk.top(); +} +} \ No newline at end of file diff --git a/src/jianzhi_offer/node.cpp b/src/jianzhi_offer/node.cpp new file mode 100644 index 0000000..c95334c --- /dev/null +++ b/src/jianzhi_offer/node.cpp @@ -0,0 +1,32 @@ +/** + * 剑指 Offer 35. 复杂链表的复制 + * + * @url: https://leetcode.cn/leetbook/read/illustration-of-algorithm/9p0yy1/ + */ + +#include "jianzhi_offer.h" + +namespace yaha { +Node *Node::copy_random_list(Node *head) { + if (head == nullptr) { + return nullptr; + } + + // 创建一个新的变量用于存储head + Node *cur = head; + std::unordered_map map; + while (cur) { + map[cur] = new Node(cur->val); + cur = cur->next; + } + + cur = head; + while (cur) { + map[cur]->next = map[cur->next]; + map[cur]->random = map[cur->random]; + cur = cur->next; + } + + return map[head]; +} +} diff --git a/src/jianzhi_offer/string.cpp b/src/jianzhi_offer/string.cpp index d610edb..e437620 100644 --- a/src/jianzhi_offer/string.cpp +++ b/src/jianzhi_offer/string.cpp @@ -4,7 +4,7 @@ namespace yaha { std::string String::replace_space(std::string s) { std::string result; for (int i = 0; i < s.length(); ++i) { - if (s[i]!=' ') { + if (s[i] != ' ') { result += s[i]; } else { result += "%20"; @@ -14,6 +14,10 @@ std::string String::replace_space(std::string s) { } bool String::is_number(std::string s) { + // 1. 判断是不是数字? + // 2. 判断小数 + // 3. 判断指数 + for (int i = 0; i < s.length(); ++i) { if (s[i] >= '0' && s[i] <= '9') { return true; diff --git a/tests/jianzhi_offer/min_stack_test.cpp b/tests/jianzhi_offer/min_stack_test.cpp new file mode 100644 index 0000000..6f61291 --- /dev/null +++ b/tests/jianzhi_offer/min_stack_test.cpp @@ -0,0 +1,14 @@ +#include + +#include "jianzhi_offer.h" + +TEST(MinStackTest, BasicAssertions) { + yaha::MinStack min_stack; + min_stack.push(-2); + min_stack.push(0); + min_stack.push(-3); + ASSERT_EQ(-3, min_stack.min()); + ASSERT_EQ(-1, min_stack.top()); + min_stack.pop(); + ASSERT_EQ(-2, min_stack.min()); +} diff --git a/tests/jianzhi_offer/node_test.cpp b/tests/jianzhi_offer/node_test.cpp new file mode 100644 index 0000000..6783a0c --- /dev/null +++ b/tests/jianzhi_offer/node_test.cpp @@ -0,0 +1,14 @@ +#include + +#include "jianzhi_offer.h" +TEST(NodeTest, TestCopyRandomList) { + yaha::Node *head = new yaha::Node(1); + head->next = new yaha::Node(2); + head->next->next = new yaha::Node(3); + head->next->next->next = new yaha::Node(4); + + yaha::Node *copy = head->copy_random_list(head); + ASSERT_EQ(copy->val, 4); + delete copy; + delete head; +} \ No newline at end of file