leetcode/include/jianzhi_offer.h

153 lines
3.2 KiB
C++

#pragma once
#include <iostream>
#include <string>
#include <queue>
#include <stack>
namespace yaha {
class CQueue {
public:
void append_tail(int value);
int delete_head();
private:
std::stack<int> _stk_tail;
std::stack<int> _stk_head;
};
class String {
public:
/**
* 剑指 Offer 09. 用两个栈实现队列
*
* @url https://leetcode.cn/leetbook/read/illustration-of-algorithm/5d3i87/
* @param s
* @return
*/
std::string replace_space(std::string s);
/**
* 剑指 Offer 20. 表示数值的字符串
*
* @url https://leetcode.cn/leetbook/read/illustration-of-algorithm/5d6vi6/
* @param s
* @return
*/
bool is_number(std::string s);
/**
* 剑指 Offer 58 - II. 左旋转字符串
*
* @url https://leetcode.cn/leetbook/read/illustration-of-algorithm/589fz2/
* @param s
* @param n
* @return
*/
std::string reverse_left_words(std::string s, int n);
/**
* 剑指 Offer 67. 把字符串转换成整数
*
* @url https://leetcode.cn/leetbook/read/illustration-of-algorithm/58pq8g/
* @param str
* @return
*/
int str_to_int(std::string str);
};
class ListNode {
public:
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
/**
* 剑指 Offer 06. 从尾到头打印链表
*
* @url https://leetcode.cn/leetbook/read/illustration-of-algorithm/5dt66m/
* @param head
* @return
*/
std::vector<int> reverse_print(ListNode *head);
void reverse_stack(ListNode *head, std::vector<int> &res);
void reverse(ListNode *head, std::vector<int> &res);
/**
* 剑指 Offer 24. 反转链表
*
* @url https://leetcode.cn/leetbook/read/illustration-of-algorithm/9pdjbm/
* @param head
* @return
*/
ListNode *reverse_list(ListNode *head);
ListNode *reverse_list(ListNode *cur, ListNode *pre);
};
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;
};
/**
* 剑指 Offer 59 - II. 队列的最大值
*
* @url: https://leetcode.cn/leetbook/read/illustration-of-algorithm/e2t5ug/
*/
class MaxQueue {
public:
MaxQueue() = default;
int max();
void push(int value);
int pop();
private:
std::queue<int> _que;
std::deque<int> _max_queue;
};
/**
* 剑指 Offer 59 - I. 滑动窗口的最大值
*
* @url https://leetcode.cn/leetbook/read/illustration-of-algorithm/58o46i/
* @param nums
* @param k
* @return
*/
std::vector<int> max_sliding_window(std::vector<int> &nums, int k);
}