链表复制

This commit is contained in:
yaha 2023-05-24 21:07:52 +08:00
parent b1260a488c
commit 404bf78158
6 changed files with 134 additions and 3 deletions

View File

@ -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<int> reverse_print(ListNode *head);
void reverse_stack(ListNode *head, std::vector<int>& res);
void reverse(ListNode *head, std::vector<int>& res);
void reverse_stack(ListNode *head, std::vector<int> &res);
void reverse(ListNode *head, std::vector<int> &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<int> _stk;
std::stack<int> _min_stk;
};
}

View File

@ -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();
}
}

View File

@ -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<Node*, Node*> 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];
}
}

View File

@ -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;

View File

@ -0,0 +1,14 @@
#include <gtest/gtest.h>
#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());
}

View File

@ -0,0 +1,14 @@
#include <gtest/gtest.h>
#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;
}