当前位置:网站首页>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();
}
};
边栏推荐
- Reading a book in idea is too much!
- Some exception handling for idea plug-in development
- Google began to roll itself, AI architecture pathways was blessed, and 20billion generation models were launched
- STM32入门介绍
- Memcached Foundation 12
- Precautions for using sneakemake
- Oracle/PLSQL: To_ Clob Function
- Parameter estimation -- Chapter 7 study report of probability theory and mathematical statistics (point estimation)
- Oracle/PLSQL: Lower Function
- Flink学习4:flink技术栈
猜你喜欢
随机推荐
Flink学习1:简介
TopoLVM: 基于LVM的Kubernetes本地持久化方案,容量感知,动态创建PV,轻松使用本地磁盘
Oracle/PLSQL: Rpad Function
snakemake 使用的注意事项
Oracle/PLSQL: Upper Function
Installing the Damon database using the command line
Oracle/PLSQL: Replace Function
Oracle/PLSQL: Rtrim Function
mmdetection 用yolox训练自己的coco数据集
pytorch 23 hook的使用与介绍 及基于hook实现即插即用的DropBlock
Hibernate generates SQL based on Dialect
Some exception handling for idea plug-in development
SQLite Reader 插件测试SQLite语法
Reading a book in idea is too much!
CVPR2022 | PointDistiller:面向高效紧凑3D检测的结构化知识蒸馏
Memcached foundation 9
Oracle/PLSQL: To_ Clob Function
three.js多米诺骨牌js特效
numpy 数组运算机制浅探
Introduction to stm32







