当前位置:网站首页>Sword finger offer 𞓜: stack and queue (simple)
Sword finger offer 𞓜: stack and queue (simple)
2022-06-27 02:13:00 【_ Soren】
List of articles
- 【 Stack and queue 】
List of articles
Preface
This column records leetcode Record of writing questions , With a sword finger Offer The first 2 Version based
1. Simulate the queue with two stacks
Original link : The finger of the sword Offer 09. Queues are implemented with two stacks
subject :
Use two stacks to implement a queue . The declaration of the queue is as follows , Please implement its two functions appendTail and deleteHead , The functions of inserting integers at the end of the queue and deleting integers at the head of the queue are respectively completed .( If there are no elements in the queue ,deleteHead Operation return -1 )
Example :
Input :
[“CQueue”,“appendTail”,“deleteHead”,“deleteHead”]
[[],[3],[],[]]
Output :[null,null,3,-1]
1 <= values <= 10000
At most appendTail、deleteHead Conduct 10000 Secondary call
Code
class CQueue {
private:
stack<int> s1;
stack<int> s2;
void push_In_S2() {
while (!s1.empty()) {
s2.push(s1.top());
s1.pop();
}
}
public:
CQueue() {
}
// Queue in operation , Push s1
void appendTail(int value) {
s1.push(value);
}
// The stack,
int deleteHead() {
if (s2.empty()) {
if (s1.empty()) {
return -1;
}
push_In_S2(); // This function is responsible for s1 Element of is pushed to s2
}
int value = s2.top();
s2.pop();
return value;
}
};
/** * Your CQueue object will be instantiated and called as such: * CQueue* obj = new CQueue(); * obj->appendTail(value); * int param_2 = obj->deleteHead(); */
2. contain min Function of the stack
subject :
Defines the data structure of the stack , Please implement a in this type that can get the minimum elements of the stack min The function is in the stack , call min、push And pop The time complexity of O(1).
Original link : contain min Function of the stack
Code :
With the help of auxiliary stack , Record the minimum value every time you stack , Keep the minimum at the top of the auxiliary stack .
class MinStack {
private:
stack<int> stk;
stack<int> min_stk;
public:
/** initialize your data structure here. */
MinStack() {
min_stk.push(INT_MAX);
}
void push(int x) {
stk.push(x);
min_stk.push(std::min(min_stk.top(), x));
}
void pop() {
stk.pop();
min_stk.pop();
}
int top() {
return stk.top();
}
int min() {
return min_stk.top();
}
};
边栏推荐
猜你喜欢
Parameter estimation -- Chapter 7 study report of probability theory and mathematical statistics (point estimation)
lottie.js创意开关按钮动物头像
图论知识及其应用初步调研
STM32入门介绍
Look! In June, 2022, the programming language ranking list was released! The first place is awesome
Cvpr2022 | pointdistiller: structured knowledge distillation for efficient and compact 3D detection
Sample development of WiFi IOT Hongmeng development kit
Flink learning 2: application scenarios
Flink學習2:應用場景
平均风向风速计算(单位矢量法)
随机推荐
Sample development of WiFi IOT Hongmeng development kit
Oracle/PLSQL: Replace Function
Oracle/PLSQL: Length Function
按键控制LED状态翻转
Google began to roll itself, AI architecture pathways was blessed, and 20billion generation models were launched
How does the C # TCP server limit the number of connections to the same IP?
LeetCode 785:判断二分图
Memcached basics 15
Memcached Foundation 12
Flink学习5:工作原理
Memcached foundation 9
Oracle/PLSQL: Lower Function
memcached基础14
STM32入门介绍
XSS攻击(笔记)
Microsoft365 developer request
YaLM 100B:来自俄罗斯Yandex的1000亿参数开源大模型,允许商业用途
paddlepaddle 21 基于dropout实现用4行代码dropblock
达梦数据库安装
WiFi-IoT 鸿蒙开发套件样例开发