Compare commits
2 Commits
b3ea983215
...
59586d0eb6
| Author | SHA1 | Date | |
|---|---|---|---|
| 59586d0eb6 | |||
| 404bf78158 |
@ -34,6 +34,8 @@ public:
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
bool is_number(std::string s);
|
bool is_number(std::string s);
|
||||||
|
|
||||||
|
std::string reverse_left_words(std::string s, int n);
|
||||||
};
|
};
|
||||||
|
|
||||||
class ListNode {
|
class ListNode {
|
||||||
@ -53,6 +55,49 @@ public:
|
|||||||
|
|
||||||
void reverse_stack(ListNode *head, std::vector<int> &res);
|
void reverse_stack(ListNode *head, std::vector<int> &res);
|
||||||
void reverse(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;
|
||||||
|
|
||||||
|
void reverse_stack(ListNode *head, std::vector<int> &res);
|
||||||
|
void reverse(ListNode *head, std::vector<int> &res);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 剑指 Offer 24. 反转链表
|
* 剑指 Offer 24. 反转链表
|
||||||
|
|||||||
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];
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -4,7 +4,7 @@ namespace yaha {
|
|||||||
std::string String::replace_space(std::string s) {
|
std::string String::replace_space(std::string s) {
|
||||||
std::string result;
|
std::string result;
|
||||||
for (int i = 0; i < s.length(); ++i) {
|
for (int i = 0; i < s.length(); ++i) {
|
||||||
if (s[i]!=' ') {
|
if (s[i] != ' ') {
|
||||||
result += s[i];
|
result += s[i];
|
||||||
} else {
|
} else {
|
||||||
result += "%20";
|
result += "%20";
|
||||||
@ -14,6 +14,10 @@ std::string String::replace_space(std::string s) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool String::is_number(std::string s) {
|
bool String::is_number(std::string s) {
|
||||||
|
// 1. 判断是不是数字?
|
||||||
|
// 2. 判断小数
|
||||||
|
// 3. 判断指数
|
||||||
|
|
||||||
for (int i = 0; i < s.length(); ++i) {
|
for (int i = 0; i < s.length(); ++i) {
|
||||||
if (s[i] >= '0' && s[i] <= '9') {
|
if (s[i] >= '0' && s[i] <= '9') {
|
||||||
return true;
|
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