链表复制
This commit is contained in:
parent
b1260a488c
commit
404bf78158
@ -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 {
|
||||
@ -34,4 +36,44 @@ public:
|
||||
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;
|
||||
};
|
||||
|
||||
}
|
||||
25
src/jianzhi_offer/min_stack.cpp
Normal file
25
src/jianzhi_offer/min_stack.cpp
Normal 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();
|
||||
}
|
||||
}
|
||||
32
src/jianzhi_offer/node.cpp
Normal file
32
src/jianzhi_offer/node.cpp
Normal 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];
|
||||
}
|
||||
}
|
||||
@ -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;
|
||||
|
||||
14
tests/jianzhi_offer/min_stack_test.cpp
Normal file
14
tests/jianzhi_offer/min_stack_test.cpp
Normal 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());
|
||||
}
|
||||
14
tests/jianzhi_offer/node_test.cpp
Normal file
14
tests/jianzhi_offer/node_test.cpp
Normal 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;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user